Wednesday, October 5, 2022

[SOLVED] How do I join the previous line with the current line with sed?

Issue

I have a file with the following content.

test1
test2
test3
test4
test5

If I want to concatenate all lines into one line separated by commas, I can use vi and run the following command:

:%s/\n/,/g

I then get this, which is what I want

test1,test2,test3,test4,test5,

I'm trying to use sed to do the same thing but I'm missing some unknown command/option to make it work. When I look at the file in vi and search for "\n" or "$", it finds the newline or end of line. However, when I tell sed to look for a newline, it pretends it didn't find one.

$ cat test | sed --expression='s/\n/,/g'
test1
test2
test3
test4
test5
$

If I tell sed to look for end of line, it finds it and inserts the comma but it doesn't concatenate everything into one line.

$ cat test | sed --expression='s/$/,/g'
test1,
test2,
test3,
test4,
test5,
$

What command/option do I use with sed to make it concatenate everything into one line and replace the end of line/newline with a comma?


Solution

reads one line at a time, so, unless you're doing tricky things, there's never a newline to replace.

Here's the trickiness:

$ sed -n '1{h; n}; H; ${g; s/\n/,/gp}' test.file
test1,test2,test3,test4,test5

h, H, g documented at https://www.gnu.org/software/sed/manual/html_node/Other-Commands.html

When using a non-GNU sed, as found on MacOS, semi-colons before the closing braces are needed.

However, is really the tool for this job

$ paste -s -d, test.file
test1,test2,test3,test4,test5

If you really want the trailing comma:

printf '%s,\n' "$(paste -sd, file)"


Answered By - glenn jackman
Answer Checked By - Mary Flores (WPSolving Volunteer)