Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: How to modify words in a line using perl oneliner

by ccn (Vicar)
on Sep 30, 2009 at 10:20 UTC ( #798279=note: print w/ replies, xml ) Need Help??


in reply to How to modify words in a line using perl oneliner

perl -i.bak -pe 's/first/last/g' one.txt

To make your code work you need to change the order of operators and add 'g' modifier to the regexp

perl -ni.bak -e 's/first/last/g; print' one.txt


Comment on Re: How to modify words in a line using perl oneliner
Select or Download Code
Replies are listed 'Best First'.
Re^2: How to modify words in a line using perl oneliner
by BioLion (Curate) on Sep 30, 2009 at 11:10 UTC

    Maybe also add in boundaries, just in case? Firstly, blast, etc...

    perl -ni.bak -e 's/\bfirst\b/last/g; print' one.txt
    Just a something something...
Re^2: How to modify words in a line using perl oneliner
by Bloodnok (Vicar) on Sep 30, 2009 at 11:36 UTC
    ...or even, assuming ...second line... to be a specific requirement (c/w a statement of the loaction of the string to be changed):
    perl -ni.bak -e 's/first/last/g if $. == 2; print' one.txt
    A user level that continues to overstate my experience :-))

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://798279]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (21)
As of 2015-07-07 21:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls