Solved Removing Ctrl M characters using sed

June 16, 2011 at 08:15:42
Specs: Linux Red Hat
Hi Everyone,
I am using sed to remove ^M characters that appear in the file. The ^M are not at the end of the line but within the middle of some of the fixed length rows.

I am using the following command, which removes the Control M and replaces it with a space because I need to keep the positions of the fixed length file accurate. However it is also adding one space to each of the records in the file.

sed -e 's/^M/ /g' old_file > new file

Does anyone have any ideas how to remove the Ctrl M without adding a space on the end of the row?

Thanks,
Robb


See More: Removing Ctrl M characters using sed

Report •

#1
June 16, 2011 at 11:46:11
✔ Best Answer
I suggest after executing your sed command that leaves the space at the end of the line:

sed -e 's/^M/ /g' old_file > newfile

that you execute another sed command that removes the trailing space:

sed 's/ $//g' newfile > anothernewfile


Report •

#2
June 16, 2011 at 14:15:44
This seems to have worked great

Thanks


Report •
Related Solutions


Ask Question