Computing.Net > Forums > Unix > Remove ^M and line feeds

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Remove ^M and line feeds

Reply to Message Icon

Name: stslim
Date: May 23, 2005 at 18:49:26 Pacific
OS: HP Unix
CPU/Ram: HP 4 gb
Comment:

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?



Sponsored Link
Ads by Google

Response Number 1
Name: stslim
Date: May 23, 2005 at 19:08:33 Pacific
Reply:

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?


0

Response Number 2
Name: anupam
Date: May 23, 2005 at 21:11:55 Pacific
Reply:

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.


0

Response Number 3
Name: vibhor_agarwalin
Date: May 23, 2005 at 21:28:58 Pacific
Reply:

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


0

Response Number 4
Name: nails
Date: May 24, 2005 at 09:22:33 Pacific
Reply:

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



0

Response Number 5
Name: anupam
Date: May 24, 2005 at 21:14:02 Pacific
Reply:

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.


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Remove ^M and line feeds

sed/awk - line feeds? www.computing.net/answers/unix/sedawk-line-feeds/5237.html

remove ^M from a file www.computing.net/answers/unix/remove-m-from-a-file/7537.html

remove ^M from file www.computing.net/answers/unix/remove-m-from-file/6031.html