Deleting [CR][LF] using sed

Dear all,

I face a problem I can not solve: I have different lines in a file and some of them are ending with [CR][LF] ie "\r\n" and the others are ending with [LF] "\n".

My aim is to supress the string "\r\n" and concatenate that line with the following one.

For example:

Saying I have a file 4_test.csv with the 4 following lines:

[x004191a@xsnl11p370p tmp]$ cat -v 4_test.csv
123^M
456
789^M
0AB
[x004191a@xsnl11p370p tmp]$ od -c 4_test.csv
0000000   1   2   3  \r  \n   4   5   6  \n   7   8   9  \r  \n   0   A
0000020   B  \n
0000022
[x004191a@xsnl11p370p tmp]$

My aim is to get as a result a file with only the two following lines:

[x004191a@xsnl11p370p tmp]$ cat -v 4_test_target.csv
123456
7890AB
[x004191a@xsnl11p370p tmp]$ od -c 4_test_target.csv
0000000   1   2   3   4   5   6  \n   7   8   9   0   A   B  \n
0000016
[x004191a@xsnl11p370p tmp]$

I tried different syntax using sed but I was not able to suppress the two consecutive characters '\r\n' !

Any kind of idea would be greatly appreciated ... Thanks a lot,

Didier.

gawk '!/\r/{ORS=RS}/\r/{ORS=X}{gsub(/\r/,X)}1' filename
1 Like
mute@zbox:~$ sed '/\r$/{N;s/\r\n//}' 4_test.csv
123456
7890AB

I'm usually an awk guy myself but this seemed worthy

1 Like
awk '{ORS=sub(/\r$/,X)?X:RS}1' filename

The previous solution works with GNU(Linux) sed only. A Unix sed does not have \r .

1 Like

Many Thanks to all of you for your help,

Regards,

Didier.