1752782 Members
6243 Online
108789 Solutions
New Discussion юеВ

Script

 
SOLVED
Go to solution
castro_2
Regular Advisor

Script

A have a file like this
122^M
How can I do if I want to a new file whitout ^M
Thanks
9 REPLIES 9
James Murtagh
Honored Contributor

Re: Script

Hi,

You would typically use sed to do a job like this:

# cat file|sed -e 's/\^M$//' > newfile

This will go through each line in the file taking the ^M from the end.

Regards,

James.
James Murtagh
Honored Contributor
Solution

Re: Script

Hi again,

Just thinking, if that is a control character at the end you will need to use something like:

# cat file|sed -e 's/.$//' > newfile

Regards,

James.
Jon Mattatall
Esteemed Contributor

Re: Script

To remove control characters (ie ^M) from a file created in Windows using vi???.

:%s/+char(M)/ /g

--- global search/replace EOL

so,
# vi filename
Hit
:%s/+char(M)/ /g
Hit

Check to see that the control chars are gone, and save the file.
A little knowledge is dangerous - none is absolutely terrifying!!!
Jon Mattatall
Esteemed Contributor

Re: Script

Well, that even looks confusing to me, and I wrote it.

+char(M) means hit them all at once, and you'll see the ^M appear on screen. Just hitting the carat (^) and M doesn't work.

Hold down , v, and hit m (or whatever character's in your file).
A little knowledge is dangerous - none is absolutely terrifying!!!
S.K. Chan
Honored Contributor

Re: Script

You may be able to delete these control chars with dos2ux command.
$ dos2ux oldfile > newfile
James R. Ferguson
Acclaimed Contributor

Re: Script

Hi:

I suspect that you transfered your file from a Windows environment using binary transfer mode. If that's the case, in the future, whenever you have a text file toggle an ASCI transfer mode. Doing so automatically invokes the translation of linefeed/carriage return characters used for Windows end-of-line control to the newline character used for Unix (and vice versa).

Regards!

...JRF...
Bill Hassell
Honored Contributor

Re: Script

Whenever you see ^M using the vi editor, you can be sure that somone transferred an ASCII file from a PC to HP-UX (or most flavors of UNix) using a binary or untranslated method with ftp or tape/CDROM archives. The same is true for files shared between Windows and Unix using SAMBA or CIFS/9000. Windows chose to ignore Unix standards for ASCII file formats but HP-UX does indeed provide a translator: dos2ux and ux2dos. These tools are required when trying to mix operating systems and file formats.


Bill Hassell, sysadmin
John Meissner
Esteemed Contributor

Re: Script

I generally find that I get a ^M at the end if I transfer it from my PC (windows 2000) using bin mode.... ftp the file again but type ascii before you transfer it... if you don't want to bother with this you could type
cat file | sed 's/.$//g' > outfile

(it's a control character and thus ^M is actually only 1 character)
All paths lead to destiny
Rory R Hammond
Trusted Contributor

Re: Script

Looks like the usual subjects were identified by everyone.

If your logfile truly is a logfile
Piping the output through "strings" will clear out ^M as well as other unwanted control characters.

strings log.file > newlog.file

or
cat log.file|strings > newlog.file
There are a 100 ways to do things and 97 of them are right