J
julia
How to make this command search and replace xtimes.
perl -p -i -e 's/oldstring/newstring/g' file.txt
Thanks
perl -p -i -e 's/oldstring/newstring/g' file.txt
Thanks
That depends on what "xtimes" means.
What does "xtimes" mean when you say it?
xtimes per line?
xtimes per file?
x lines per file?
...?
I want to take 4times oldstring and replace it with newstring1
I want to take 3times oldstring and replace it with newstring2
oldstring
oldstring
oldstring
oldstring
oldstring
oldstring
newstring1
newstring1
newstring1
newstring1
newstring2
newstring2
newstring2
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
accordingly.
julia said:I want to take 4times oldstring and replace it with newstring1
I want to take 3times oldstring and replace it with newstring2
oldstring
oldstring
oldstring
oldstring
oldstring
oldstring
newstring1
newstring1
newstring1
newstring1
newstring2
newstring2
newstring2
What if oldstring appears more than once on a line?
--
Keith Thompson (The_Other_Keith) (e-mail address removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
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.