Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I eliminate ^M

Shell Scripts

How do I eliminate ^M

by  olded  Posted    (Edited  )
If you need to eliminate control-M, ^M, and your unix version doesn't posses a dos2unix style command, within vi, as suggested by pcunix:

1,$s/^V^M//g

(you type Control V and then Control M- only the control M will show up)

If you decide to write a script, try one of these suggestions:


1) The following replacement sed command works by deleting the last character of each line:

sed 's/.$//' $i.tmp > $i

Be careful! The above sed command clobbers the last character in the line, whether it's a control character or not.

2) A better solution is replacing the last character if the octal value is 15 or 32, Control-m and Control-z respectively:

sed 's/'"$(printf '\015')"'$//
s/'"$(printf '\032')"'$//' $i.tmp > $i


3) Perhaps the best solution is executing the tr command to delete all octal 15 and 32 characters. Replace the sed command with the following:

tr -d '\015\032' < $i.tmp > $i

The above command deletes all ^M and ^Z -- not just the ones at the end of the line.

Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top