I have a file of strings that are comma separated. I'm trying to replace the commas with a new line. I've tried:
sed 's/,/\n/g' file but it is not working. What am I missing?
Use tr instead:
tr , '\n' < file tr ',' ';' < input.csv > output.csvUse an ANSI-C quoted string $'string'
You need a backslash-escaped literal newline to get to sed. In bash at least, $'' strings will replace \n with a real newline, but then you have to double the backslash that sed will see to escape the newline, e.g.
echo "a,b" | sed -e $'s/,/\\\n/g' Note this will not work on all shells, but will work on the most common ones.
echo "a,b" | sed -e $'s/,/\\n/g.man bash.sed -E $'s/<\\/br>/\\\n/g' file, no need to install gnu-sed-e part seems not necessary.fzf for debugging my $PATH in case something is missing: echo $PATH | sed -e $'s/:/\\\n/g' | fzf ^.^sed 's/,/\ /g' works on Mac OS X.
MacOS is different, there is two way to solve this problem with sed in mac
first ,use \'$'\n'' replace \n, it can work in MacOS:
sed 's/,/\'$'\n''/g' file the second, just use an empty line:
sed 's/,/\ /g' file Ps. Pay attention the range separated by '
the third, use gnu-sed replace the mac-sed
Apparently \r is the key!
$ sed 's/, /\r/g' file3.txt > file4.txt Transformed this:
ABFS, AIRM, AMED, BOSC, CALI, ECPG, FRGI, GERN, GTIV, HSON, IQNT, JRCC, LTRE, MACK, MIDD, NKTR, NPSP, PME, PTIX, REFR, RSOL, UBNT, UPI, YONG, ZEUS To this:
ABFS AIRM AMED BOSC CALI ECPG FRGI GERN GTIV HSON IQNT JRCC LTRE MACK MIDD NKTR NPSP PME PTIX REFR RSOL UBNT UPI YONG ZEUS \r is not the same as \n, and this might break further data manipulation and usage.\r!This works on MacOS Mountain Lion (10.8), Solaris 10 (SunOS 5.10) and RHE Linux (Red Hat Enterprise Linux Server release 5.3, Tikanga)...
$ sed 's/{pattern}/\^J/g' foo.txt > foo2.txt ... where the ^J is done by doing ctrl+v+j. Do mind the \ before the ^J.
PS, I know the sed in RHEL is GNU, the MacOS sed is FreeBSD based, and although I'm not sure about the Solaris sed, I believe this will work pretty much with any sed. YMMV tho'...
To make it complete, this also works:
echo "a,b" | sed "s/,/\\$(echo -e '\n\r')/" tr solution, which is already the accepted answer.\n\r instead of \r\n? I tried \r\n which is the necessary order on Windows. However, after I do this it's leaving a lot of ^M carriage return characters in vim, so I think it's supposed to be only \n but \n alone doesn't work.Though I am late to this post, just updating my findings. This answer is only for Mac OS X.
$ sed 's/new/ > /g' m1.json > m2.json sed: 1: "s/new/ /g": unescaped newline inside substitute pattern In the above command I tried with Shift+Enter to add new line which didn't work. So this time I tried with "escaping" the "unescaped newline" as told by the error.
$ sed 's/new/\ > /g' m1.json > m2.json Worked! (in Mac OS X 10.9.3)
Just to clearify: man-page of sed on OSX (10.8; Darwin Kernel Version 12.4.0) says:
[...]
The regular expressions used in sed, by default, are basic regular expressions (BREs, see re_format(7) for more information), but extended (modern) regular expressions can be used instead if the -E flag is given. In addition, sed has the following two additions to regular expressions: 1. In a context address, any character other than a backslash (``\'') or newline character may be used to delimit the regular expression. Also, putting a backslash character before the delimiting character causes the character to be treated literally. For example, in the context address \xabc\xdefx, the RE delimiter is an ``x'' and the second ``x'' stands for itself, so that the regular expression is ``abcxdef''. 2. The escape sequence \n matches a newline character embedded in the pattern space. You cannot, however, use a literal newline charac- ter in an address or in the substitute command. [...]
so I guess one have to use tr - as mentioned above - or the nifty
sed "s/,/^M /g" note: you have to type <ctrl>-v,<return> to get '^M' in vi editor
The sed on macOS Mojave was released in 2005, so one solution is to install the gnu-sed,
brew install gnu-sed then use gsed will do as you wish,
gsed 's/,/\n/g' file If you prefer sed, just sudo sh -c 'echo /usr/local/opt/gnu-sed/libexec/gnubin > /etc/paths.d/brew', which is suggested by brew info gnu-sed. Restart your term, then your sed in command line is gsed.
tr , '\n'. I guess, sed treats\nas plain text.tr , '\n' < file- no pipe.gin the end of the script for? I get the same behavior without it.