System Administration

Re: Removing ^M characters from the files using sed etc..

 
SOLVED
Go to solution
Shivkumar
Super Advisor

Removing ^M characters from the files using sed etc..

Hi,

How to remove ^M characters from the end of the lines within file/s with
the help of sed/awk or from inside the file openend in vi editor ?

I had noted down earlier somewhere but now it is not handy with me.

Thanks,
Shiv
12 REPLIES 12
James R. Ferguson
Acclaimed Contributor
Solution

Re: Removing ^M characters from the files using sed etc..

Hi Shiv:

I think you are remembering this discussion:

https://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=974207

In that thread, you will see a Perl offering from me. It works on non-HP platforms in the event that you don't have a 'dos2ux' command as you do on HP-UX.

Regards!

...JRF...
Shivkumar
Super Advisor

Re: Removing ^M characters from the files using sed etc..

Thanks James!!

I had stored this during my previous work place.

Now working for a different company.

Warm Regards,
Shiv

Devesh Pant_1
Esteemed Contributor

Re: Removing ^M characters from the files using sed etc..

Siv,
James has already provided the relevant link to the discussion
but nevertheless i can't stop myself from writing something down:
Just save the following script as for example dos2unix.sh
--------------------------------------------
#!/bin/bash
# To replace dos linebreaks for Unix compatibility

echo "This script will replace the ^M line breaks from dos."

echo -n "Enter filename without extension: "
read file
echo -n "Enter extension: "
read ext
sed 's/\r//' $file.$ext > $file2.$ext
cp -f $file2.$ext $file.$ext
rm -f $file2.$ext
------------------------------------------

to run it, type ./dos2unix.sh

basically, you can just
replace the ^M with sed
sed 's/\r//' xxx.ext > xxx2.ext
then copy xxx2.ext back to the original file xxx.ext
and then remove the file xxx2.ext

thanks
DP
Fred Richardett
Occasional Advisor

Re: Removing ^M characters from the files using sed etc..

Shiv,

From within the file using vi you can enter the following:

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

For clarification that's VM
Patrick Wallek
Honored Contributor

Re: Removing ^M characters from the files using sed etc..

Why make it so complicated? Why not just use the 'dos2ux' command?

# dos2ux file1 > file2

This removes the ^M characters that are usually leftovers from a file transfer between Windows and Unix.
vinod_25
Valued Contributor

Re: Removing ^M characters from the files using sed etc..

Hi Shiv,

sed 's/^M$//'

One line == 10 points

regds,
Vinod
Arturo Galbiati
Esteemed Contributor

Re: Removing ^M characters from the files using sed etc..

Hi Shiv,
just another way:

cp file file.bak
tr -d "^M" file

to obtain ^M type ctrl-v, ctrl-m

HTH,
Art
Raj D.
Honored Contributor

Re: Removing ^M characters from the files using sed etc..

Shiv ,

# dos2ux soruce_file > result_file

for further:

# man dos2ux

Cheers,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "
Frank de Vries
Respected Contributor

Re: Removing ^M characters from the files using sed etc..

I pasted this from my sed handy one-liners
I always keep handy.
I will upload the full list:

# IN UNIX ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format
sed 's/.$//' # assumes that all lines end with CR/LF
sed 's/^M$//' # in bash/tcsh, press Ctrl-V then Ctrl-M
sed 's/\x0D$//' # gsed 3.02.80, but top script is easier
Look before you leap