raikyn raikyn - 4 months ago 6
Bash Question

Shell command to delete \n on 1 out of 2 line on a file

I'm looking for a shell command to delete the return chariot on one line out of two.
I have a file like this :

1.32640997
;;P
1.14517534
;;P
1.16120958
;;P
...


And I would like something like this:

1.32640997;;P
1.14517534;;P
1.16120958;;P
...


Is it possible?
Thanks

Answer

Using awk

$ awk '{printf "%s%s",$0,(NR%2==0?ORS:"")}' File
1.32640997;;P
1.14517534;;P
1.16120958;;P

This prints each line followed by nothing for odd lines or followed by the output record separator for even lines.

Using sed

This works by reading in lines in pairs:

$ sed 'N; s/\n//' File
1.32640997;;P
1.14517534;;P
1.16120958;;P

N reads in the next line and s/\n// removes the newline.