Shaan Shaan - 7 months ago 17
Perl Question

Unix file handling removal of Junk chars

Input file:

abc
def^M
ghi
jkl
mno^M
pqr^M


Desired output:

abc def
ghi jkl mno
pqr


I tried:

sed 's/^[\^M]$/ /g' file.txt > output.txt


ie. all lines shall be segregated with space until ^M [ctrl -m] character.

Answer

Using gnu-awk you can do:

awk -v RS='\r\n*' '{gsub(/\n/, " ")} 1' file

abc def
ghi jkl mno
pqr
Comments