Operating System - HP-UX
1820390 Members
3468 Online
109623 Solutions
New Discussion юеВ

Delete control M character using tr command.

 
uform
Frequent Advisor

Delete control M character using tr command.

1) How to do it ? following is not working for me ..

tr -d '\r' test.dat > test1.dat

2) How to get only the records which has control M in it ?
9 REPLIES 9
Pete Randall
Outstanding Contributor

Re: Delete control M character using tr command.

The simpler way would be to use the dos2ux command.


Pete

Pete
IT_2007
Honored Contributor

Re: Delete control M character using tr command.

you edit file with vi and do the following after pressing "Esc" key.

:%s///g

it will remove all ^M character in that file.
Peter Godron
Honored Contributor

Re: Delete control M character using tr command.

Hi,
how about:
tr -d "\015" < test.dat > test1.dat

Don't forget, to get the ^M in UNIX command you have to use CTRL+V CTRL+M.
James R. Ferguson
Acclaimed Contributor

Re: Delete control M character using tr command.

Hi:

Yet another way is:

# perl -pe 's/\r\n/\n/;s/\032//' file

This handles not only carriage-return characters (^M) but also any end-of-file character (^Z).

Regards!

...JRF...
Ralph Grothe
Honored Contributor

Re: Delete control M character using tr command.

You simply need to redirect your stdin from the file with the superfluous CRs as Peter showed you.
Unlike its bigger brother sed the tr command requires this.
Madness, thy name is system administration
uform
Frequent Advisor

Re: Delete control M character using tr command.

Thanks all...

i just left '<' before test.dat
tr -d '\r' test.dat > test1.dat

tr -d '\r' < test.dat > test1.dat
this worked for me...

Thanks again.

Geoff Wild
Honored Contributor

Re: Delete control M character using tr command.

You can also use the col command:

cat test.dat |col -b > test.dat.1

or

col -bx test.dat test.dat.1

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Bill Hassell
Honored Contributor

Re: Delete control M character using tr command.

Actually, you just need to transfer the files correctly. CTRL-M is a PC artifact. Or more accurately, you are transferring a file from a very different computer without proper translation. If you used FTP, you need to set the ASCII translator for these files. Note that BINARY does no translation so you get the file as it appears inside the PC -- which is not compatible with Unix (Unix uses LF for end-of-line, PCs use CR+LF, other systems use other characters). Every line has CTRL-M at the end but FTP knows how to convert between different systems.

Now if you are sharing directories between dissimilar systems, the same rule holds true. The PC files appear exactly as they exist on each system. A Unix file will have black squares at the end-of-line when viewed in Notepad, and a PC file will have CTRL-M's when viewed in Unix.

What to do? Well, there's no way to get t5he two different OS's to change so you'll have to translate whichever way you need. The 2 commands are:

dos2ux
and
ux2dos

(there's no similar commands on a PC).


Bill Hassell, sysadmin
James R. Ferguson
Acclaimed Contributor

Re: Delete control M character using tr command.

Hi:

To add to Bill's comments, while 'dos2ux' and 'ux2dos' are UNIX commands, Perl runs on virtually any platform. Hence on a UNIX or on a Microsoft Windows server the following are true:

In lieu of 'dos2ux' use:

# perl -pe 's!\r\n!\n!;s!\032!! if eof' file

...and in lieu of 'ux2dos' use:

# perl -pe 's!\n!\r\n!s;END{print "\032"}' file

Regards!

...JRF...