views:

111

answers:

1

I'm trying to remove the carriage returns (\r) from a file with the following command on AIX, but it's also removing my last line. Any suggestions?

sed -e 's/\r\n/\n/g' ./excprule > ./excprule.tst

Command sequence:

dev1:> sed -e 's/\r\n/\n/g' ./test_file > ./test_file.tst
dev1:> diff test_file.tst test_file
diff: 0653-827 Missing newline at the end of file test_file.
26a27
> Trailer 25

Edit: Found a workaround by doing the same thing with perl, but would like to know why this doesn't work.

A: 

You can also use dos2unix if you have it in AIX, however, with sed,

sed '$!{:a;N;s/\r\n/\n/;ta}' file

sed strips off the newline whenever it process current line (see the man page), so you won't really find the \r\n.

ghostdog74
I believe removing the `\r` would be dos2unix?
Jefromi
yes you are right, got it inverted.
ghostdog74
Why not simply use`tr '\r' '' < file.in > file.out` ?
tonio
merely showing where OP went wrong with his sed command.
ghostdog74

related questions