Operating System - HP-UX
1820117 Members
3162 Online
109619 Solutions
New Discussion юеВ

Re: How to use "regular expression" to delete "^M" character

 
SOLVED
Go to solution
Jiangshanshan
Occasional Contributor

How to use "regular expression" to delete "^M" character

I use "vi" to open a .txt file from NT, and there are a lot of "^M" at the end of each line, how can i use regular expression to delete them.
jss
9 REPLIES 9
Denver Osborn
Honored Contributor
Solution

Re: How to use "regular expression" to delete "^M" character

The best thing for this situation would be to use the dos2ux command.

# dos2ux nt_file.txt > new_file.txt

new_file.txt will not contain the ^M

-hope this helps.
denver
Dan Hetzel
Honored Contributor

Re: How to use "regular expression" to delete "^M" character

Hi,

It's easy ! First of all hit ':' to go in 'ex' mode.
Then type the following:
1,$s///

That's it !

Dan

PS: allows you to type any kind of non-printable character in ex mode

Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com
Steven Sim Kok Leong
Honored Contributor

Re: How to use "regular expression" to delete "^M" character

Hi,

Alternatively,

$ cat with_ctrl_M.txt | col -b > without_ctrl_M.txt

Hope this helps. Regards.

Steven Sim
Brainbench MVP for Unix Admin
http://www.brainbench.com
Suhas_2
Regular Advisor

Re: How to use "regular expression" to delete "^M" character

My two cents ...
In addition to all the above ways...
You can do the following...

# strings nt_file.txt >new_file.txt

Hope this helps..;-)
Suhas Oka.
Never say "Die"
Joseph C. Denman
Honored Contributor

Re: How to use "regular expression" to delete "^M" character

This is what I would do!!!!

FTP the file as ASCII from the NT box to the UNIX box. The ^M is a . Unix uses newlines. If you FTP the file as ASCII, conversion is automatic.

Hope this helps....
If I had only read the instructions first??
Andreas Voss
Honored Contributor

Re: How to use "regular expression" to delete "^M" character

Hi,

within vi you can remove ^M with:

1G
!G!dos2ux

1G will go to the first line of the document (entering is not visible)
!G will select the document form top to bottom (entering is not visible, after the G a ! occours at the bottom line)
dos2ux will remove the ^M from the selected lines.

Regards
Stefan Schulz
Honored Contributor

Re: How to use "regular expression" to delete "^M" character

The easiest thing is to use the dos2ux command as Denver Osborn mentioned. Of course you should use the ux2dos command if you copy this file to a Windowsbox again.

If for some reason you can't use dos2ux i would prefer the vi with substitiution like Dan posted. In addition to the post from Dan i would suggest using the following substitution:

1,$s///g

With an additional "g" at the end. Which means "global". If the String you want to replace can be found several times in a line this global option will replace them all. Without this option only the first apperance in this line will be replaced.

Hope this helps and my english was good enough .

Stefan
No Mouse found. System halted. Press Mousebutton to continue.

Re: How to use "regular expression" to delete "^M" character

And what about this (in vi):

%s/.$//

Laszlo
Andre Lattier
New Member

Re: How to use "regular expression" to delete "^M" character

you don't need a regular expression, you can use tr :
cat myfile | tr -d '\015'