Shaan
Shaan

Reputation: 53

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.

Upvotes: 3

Views: 185

Answers (2)

anubhava
anubhava

Reputation: 785128

Using gnu-awk you can do:

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

abc def
ghi jkl mno
pqr

Upvotes: 2

Schwern
Schwern

Reputation: 164809

^M is usually not the actual characters ^ and M. ^ indicates a special character and M means it's the 13th ASCII character (M is the 13th letter). ^M indicates the carriage return character. Looking at an ASCII table can help. Such unprintable ASCII characters are referred to as "control characters".

This usually means the file has two character Windows-style newlines, ASCII 13 and ASCII 10. Most languages represent this as \r\n.

To replace ^M you can use \r or \015 (the 15th ASCII character in base 8 which is 13 in decimal).

Upvotes: 2

Related Questions