Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Posted on
- SR xtimes
October 1, 2009, 3:46 pm
rate this thread
Re: SR xtimes
perl -p -e 's/oldstring/($.<5?"newstring1":"newstring2")/e;' file.txt
This uses the $. variable that gives the line number in the input file
and the e modifier to the substitute operator that cause the
replacement string to be evaluated as a Perl expression.
Note that this only works if oldstring appears on every line. If that
is not the case, then you have to use a counter that counts how many
times a match has occurred and modify your replacement string
- » FAQ 7.3 Do I always/never have to quote my strings or use semicolons and commas?
- — Next thread in » PERL Discussions
- » FAQ 5.39 Why do I get weird spaces when I print an array of lines?
- — Previous thread in » PERL Discussions