Substituting carriage return followed by newline character - HELP

--------------------------------------------------------------------------------

Hi All

I have a field being returned from the DB that when opened in Vi shows a ^M before the rest of the field is displayed on the next line.

I need it so that the only newline character is the end of the line since I need to transform my file into an Excel report.

Thus my idea is to substitute the combination of ^M and newline character with nothing.

I've been tryin the following

sed s/^M\n//g myFile.out

but this isn't working. Can anyone help me?

Thanks,

Darren

Your duplicated thread - Substituting carriage return follwed by newline character - HELP! . Please dont duplicate. Its against the rules.

Try dos2unix myFile.out on your file to get rid of the ^M characters.

Sorry mate. First time using the forums and i wasn't sure how to move the thread as I think I posted it in the wrong place to begin with.

I will try your suggestion.

dos2unix is not a recognised command. I already remove the ^M using the sed command but i need to remove it <b> in combination with </b> the newline character.

Any other ideas?

try using dos2ux command
I just found out that on some systems it may be dos2unix

Gaurav

dos2ux does not work either :frowning:

Post the results of uname -a

You also mentioned that you have removed ^M using sed. What is it that you are looking for now ? Remove <b>^M</b> ?

uname -a gives...

AIX carnowen 2 5 005AA72F4C00

I need to remove a combination of ^M and newline character.

i tried...

sed s/^M\n//g but this does not work :cry:

thanks for your help mate.

When you say new line character, I assume you want two lines into 1 single line, or likewise.

Try this.

sed -e 's/^M\\n//g' file

If it doesnt work, show us a sample input and how you expect the output to be.

text file is like follows (this is how it would look using vi)

line1field1 line1field2 line1field3 line1field4 line1field5
line2field1 line2field2 line2field3 line2field4 line2field5
line3field1 line3field2 line3field3^M
line3field4 line3field5
line4field1 line4field2 line4field3 line4field4 line4field5
line5field1 line5field2 line5field3 line5field4 line5field5

I need this to become

line1field1 line1field2 line1field3 line1field4 line1field5
line2field1 line2field2 line2field3 line2field4 line2field5
line3field1 line3field2 line3field3 line3field4 line3field5
line4field1 line4field2 line4field3 line4field4 line4field5
line5field1 line5field2 line5field3 line5field4 line5field5

I tried the sed command you used in your previous post but this hasn't worked. Again, thanks for your help friend.

maybe there's a different way of representing newline characters when using sed??

if i use

sed -e s/^M//g myFile.out

then the ^M is removed. All i need to do now is try and get line 3 in the above example onto one line!!

Anyone got any ideas?? This is doin' my 'ead in!!!

Got perl?

perl -00pe 's/\r\n//g' myFile.out

unfortunately i can't use perl mate. it's not available. it has to be done using a regular expression in UNIX.

I am returning results set from Sybase stored proc. Is there a function in Sybase that may remove the newline characters?

Don't know Sybase, but you should find some way to replace those characters within the stored procedure. That way you don't have to fix it afterwards. Oracle SQL has a replace() function.

Otherwise, consider using awk to remove CR+LF pairs, e.g..

awk 'sub(/\r/,""){printf $0;next}{print}' myFile.out