Remove ^M and line feeds

May 23, 2005 at 18:49:26
Specs: HP Unix, HP 4 gb

Example of the data file is pipe delimited and looks like this.^M is only scattered around certain lines in the file.

A|Data1|Data2|Data3|Data4|Data5|
A|Data1|Data2|Data3split^M
data3split|Data4|Data5|
A|Data1|Data2|Data3|Data4|Data5|
A|Data1|Data2split^M
data2split|Data3|Data4|Data5|
A|Data1|Data2|Data3|Data4|Data5|

Anyone knows a simple way (sed or awk) to remove the ^M and linefeed that splits the data line?


See More: Remove ^M and line feeds

Report •


#1
May 23, 2005 at 19:08:33

oh forgotten to add data can also split to more than 2 rows

e.g.
A|Data1|Data2|Data3|Data4|Data5|
A|Data1split^M
data1split|Data2|Data3split^M
data3split|Data4|Data5split^M
data5split|
A|Data1|Data2|Data3|Data4|Data5|

Side note:
Web applications and all these 'new' XML seems to take us a huge step backways in terms of data validation at the UI to validate users' inputs don't they?


Report •

#2
May 23, 2005 at 21:11:55

Hi..to remove ^M, give the command :
sed -e 's/^M//g' filename
you will have to write the control-M as follows : first press control-V,then control-M,this will show the correct control-M on the command line.
For removing the linefeeds,try this :
sed -e 's/.$//g' filename
this will remove the last character from the line,which happens to be the line feed.


Report •

#3
May 23, 2005 at 21:28:58

I simply use dos2unix.

Anupam, i didn't get your linefeed removal line.
Can you explain me a bit. What does here .$ signify.

Thanks

Vibhor Kumar Agarwal


Report •

Related Solutions

#4
May 24, 2005 at 09:22:33

There's other ways to delete control-m besides embedding ^M:

1) Using tr:

tr -d '\015' filename

2) Using sed:

sed 's/'"$(printf '\015')"'$//g' filename



Report •

#5
May 24, 2005 at 21:14:02

the .$ will remove the last character from each line.In your case,the last character will be the linefeed which will be removed by the sed command.In the case of converting from DOS to UNIX,the last character of the line will be the carriage return,which will be removed.


Report •


Ask Question