find and replace in a text file

P

petermichaux

Hi,

I would like to learn how to find and replace in a text file. I use MS
Word for this because I know how to type white spaces in there. Often I
want to get rid of a trailing space or double paragraph returns so I do
things like

Find: " ^p"
Replace: "^p"

Find: "^p^p"
Replace: "^p"

What I would really like to be able to do is replace carriage returns
with line feeds for conversion from Linux to Mac.

Can anyone tell me how to start doing this in perl?

Thanks,
Peter
 
L

Lars Eighner

In our last episode,
the lovely and talented (e-mail address removed)
broadcast on comp.lang.perl.misc:
I would like to learn how to find and replace in a text file. I use MS
Word for this because I know how to type white spaces in there. Often I
want to get rid of a trailing space or double paragraph returns so I do
things like
Find: " ^p"
Replace: "^p"
Find: "^p^p"
Replace: "^p"
What I would really like to be able to do is replace carriage returns
with line feeds for conversion from Linux to Mac.
Can anyone tell me how to start doing this in perl?


perl -pi -e "s/\r//" <insert your filespec here>
 
J

John W. Krahn

I would like to learn how to find and replace in a text file. I use MS
Word for this because I know how to type white spaces in there. Often I
want to get rid of a trailing space

perl -i.bak -lpe"s/ +$//" yourfile

or double paragraph returns

perl -i.bak -l -00pe1 yourfile

so I do things like

Find: " ^p"
Replace: "^p"

Find: "^p^p"
Replace: "^p"

What I would really like to be able to do is replace carriage returns
with line feeds for conversion from Linux to Mac.

perl -i.bak -pe"tr/\r/\n/" yourfile



John
 
P

petermichaux

Thanks for the suggestions! I will start playing with those commands.

-Peter
 
A

axel

John W. Krahn said:
perl -i.bak -lpe"s/ +$//" yourfile



perl -i.bak -l -00pe1 yourfile



perl -i.bak -pe"tr/\r/\n/" yourfile

I think Linux to Mac would be 'tr/\n/\r/'.

Axel
 
P

petermichaux

From some things i read on the internet I'm not sure why tr/// is
suggested here instead of s///. I would not want any "/r" becoming "/n"

And why the -l flag?

Thanks,
Peter
 
J

John W. Krahn

I think Linux to Mac would be 'tr/\n/\r/'.

Yes *I* know that. However the OP says "replace carriage returns with line
feeds" so that is what I wrote. :)


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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top