Replace newline with comma.

I have output from a file like this:

15,01,11,14:06
235

I would like to change this to:

15,01,11,14:06,235

Removing newline and change to ","
I now this can be done with tr
cat OUT | tr '\n' ',''

My problem is that tr is not implemented in this shell. sed is, show it should be possible using sed, but how?

---------- Post updated at 03:09 PM ---------- Previous update was at 02:27 PM ----------

After some more investigating, I did find this:

cat OUT | sed -n -e 'H;${x;s/\n/,/g;s/^,//;p;}'

It gives me desire result, but have no clue what it does :slight_smile:

If you want all the lines joined up, like with the sed you showed, then simpler is:

$ paste -sd, file
15,01,11,14:06,235

Thanks, but paste is not implemented in this shell.

sed isn't implemented in the shell either. What do you mean by "shell"?

And what OS are you using?

I put your two lines of output into a file called OUT and joined them with sed:

sed 'N;s/\n/,/' OUT

Results in:

 
15,01,11,14:06,235

Thanks for feedback

Os is BusyBox v1.01

Ah, OK :slight_smile:

Anyway, you have the sed you need, so that's good!

You have cat on BusyBox, which is also good, but you don't need it here:

sed -n -e 'H;${x;s/\n/,/g;s/^,//;p;}' OUT

As you asked, the sed appends every line from the file it's reading to the "hold space" (H), and on the last line ($) swaps it with the "pattern space" (x), and then, since the entire file is effectively in the pattern space, can go about replacing all the newlines with commas (s/\n/,/g). Finally it tidies up the leading comma (s/^,//) left by H before printing the whole lot (p), that was initially prevented by the -n option.

awk, for behaviour like tr:

awk 1 ORS=, file

or

awk 'END{printf RS}1' ORS=, 

if you want the linefeed at the end of the output...