Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Changing the contents of a file

by perlNinny (Beadle)
on Feb 02, 2006 at 22:48 UTC ( #527483=perlquestion: print w/replies, xml ) Need Help??

perlNinny has asked for the wisdom of the Perl Monks concerning the following question:

Let us say that I have a text file with the following:

elephant * -ugly /leave/this/alone
elephant * /only/change/->/turkey
elephant * turkey

I would like to change that file to say:
elephant * -ugly /leave/this/alone
elephant * /only/change/->/fox
elephant * fox

Or a file with:
elephant * -ugly /leave/this/alone
elephant * /only/change/->/turkey

to say:
elephant * -ugly /leave/this/alone
elephant * /only/change/->/fox

Or a file with:
elephant * -ugly /leave/this/alone
elephant * turkey

to say:
elephant * -ugly /leave/this/alone
elephant * fox

As you can see, I need it to change after only an elephant * with either a / or [a-z|A-Z] and only change the last word.

Suggestions?

Replies are listed 'Best First'.
Re: Changing the contents of a file
by Tanktalus (Canon) on Feb 02, 2006 at 23:13 UTC

    What have you tried?

    Not knowing which part has got you down, the quick break down is: open the input and output files, loop through them, change each line based on a regular expression that does what you want, print the changed line to the output file, and then close both file handles before exiting.

    Most of this is taken care of if you use something like perl -pie 's/regexp/change/' filename. So the real problem is - what is the regular expression?

    That depends on the spec. "As you can see" - I thought you just wanted to change "turkey" to "fox" until the last line - so it wasn't very obvious to me.

    In your case, I would suggest code like this:

    s/\S+(\s*)$/new-word$1/ if /elephant \* [/a-zA-Z]/
    (the "last word" may not actually end the line - spaces after the last word doesn't change the fact it's the last word) The reason I didn't go with:
    s/(elephant \* [/a-zA-Z].*?\s)\S+(\s*)$/$1new-word$2/
    is because you don't always have more than one word following the *, and I couldn't think of a quick, easy, obvious way to do it in a single regex. Since everything is anchored, this shouldn't be much different in speed, but, more importantly, it should be correct.

Re: Changing the contents of a file
by kwaping (Priest) on Feb 02, 2006 at 23:32 UTC
    Here's my shot at it, assuming "turkey" isn't always going to be the same pattern/word.
    while(<DATA>) { s#(elephant\s+\*\s+)(.*?)\w+$#$1$2fox# if (!/-ugly/); print; } __DATA__ elephant * -ugly /leave/this/alone elephant * /only/change/->/turkey elephant * turkey elephant * -ugly /leave/this/alone elephant * /only/change/->/turkey elephant * -ugly /leave/this/alone elephant * turkey

    This is somewhat of a kludge, as I wanted to use a negative look-behind on the "-ugly", but I couldn't get it to work.

    If "turkey" is always going to be "turkey", then the solution is a lot easier: s/turkey$/fox/
Re: Changing the contents of a file
by l3v3l (Monk) on Feb 02, 2006 at 23:16 UTC
    Since what you are substituting always occurs at the end of a line, the easiest way to do this seems to me to be something like:
    perl -pi -e 's/turkey$/fox/' file_full_of_turkeys_that_you_want_to_be_ +foxes

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://527483]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2022-08-19 04:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?