How to remove ^M using perl ?

A

Abhinav

Hi,

I have a sscript which reads in a set of lines from a windows text file,
and concatenates it and stores in a string.

I chomp the string after reading it, and do

$_ =~ s/^V<^Enter>//g; # i.e., I press cntrl -V and then cntrl - Enter

This does not seem to work and I still find ^M charactes when i wtite
the lines to a file.

What is the way to remove them ?

Thanks
AB
 
G

Gunnar Hjalmarsson

Abhinav said:
I have a sscript which reads in a set of lines from a windows text
file, and concatenates it and stores in a string.

I chomp the string after reading it, and do

$_ =~ s/^V<^Enter>//g; # i.e., I press cntrl -V and then cntrl - Enter

This does not seem to work and I still find ^M charactes when i
wtite the lines to a file.

What is the way to remove them ?

Well, I don't understand the above code, but to remove the Windows CR
characters you can simply try

tr/\r//d;
 
A

Abhinav

Gunnar said:
Well, I don't understand the above code, but to remove the Windows CR
characters you can simply try

tr/\r//d;
Thanks, but its not working.

What i need to do (and maybe could not explain clearly above) is read a
file from windoze, and remove all the ^Ms which are visible when I open
the file in vi ...

doing
:1,$ s/^V^Enter//g
in vi removes them.

tr/\r//d;
when invoked within perl, does not.
neither does s/\r//g;

Thanks
AB
 
G

Gunnar Hjalmarsson

Abhinav said:
Thanks, but its not working.

What i need to do (and maybe could not explain clearly above) is
read a file from windoze, and remove all the ^Ms which are visible
when I open the file in vi ...

doing
:1,$ s/^V^Enter//g
in vi removes them.

tr/\r//d;
when invoked within perl, does not.
neither does s/\r//g;

In that case, if you want somebody to be able to help you, please post
a short but complete program that illustrates the problem.
 
A

Abhinav

Abhinav said:
Gunnar said:
Abhinav wrote
[SNIP]
Well, I don't understand the above code, but to remove the Windows CR
characters you can simply try

tr/\r//d;
Thanks, but its not working.

What i need to do (and maybe could not explain clearly above) is read a
file from windoze, and remove all the ^Ms which are visible when I open
the file in vi ...

doing
:1,$ s/^V^Enter//g
in vi removes them.

tr/\r//d;
This does indeed work :)
apologize for the statement above ..
a coffee break needed !
 
J

John W.

Abhinav said:
Hi,

I have a sscript which reads in a set of lines from a windows text file,
and concatenates it and stores in a string.

I chomp the string after reading it, and do

$_ =~ s/^V<^Enter>//g; # i.e., I press cntrl -V and then cntrl - Enter

This does not seem to work and I still find ^M charactes when i wtite
the lines to a file.

What is the way to remove them ?

Thanks
AB

You need to just do "ctrl+v" then Enter. Don't do ctrl+Enter. In
your script you should actually see...

$_ =~ s/^M//g;

....after doing that.

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top