I have got a text file with a list of first names, and I am trying to replace newline with ,.
Here is the command I am using
sed 's/\ /,/g' file
while the reverse is working
sed 's/,/\ /g' file
, with newline.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
sed is not able to eat newlines in a simple way.
sed works on a line-by-line basis, so it can only add newlines, not remove them. But you can effectively do some sed gymnastics by storing two consecutive lines. You may also want to have a look at
tr, which is able to replace characters, for example:
tr '\n' , < file
Also, with many tools you can use
'\n' to represent the line feed character (used for UNIX line breaks).
Like Stephane pointed,
tr will replace all the line feeds (including the last one). You may prefer to use
paste, which will join the lines using a specified delimitier:
paste -sd, file