SR xtimes

J

julia

How to make this command search and replace xtimes.
perl -p -i -e 's/oldstring/newstring/g' file.txt

Thanks
 
J

julia

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

Thanks for your help
 
J

Jim Gibson

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.
 
J

Jim Gibson

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.

In the latter case the following should work:

perl -p -e 's/oldstring/(++$n<5?"newstring1":"newstring2")/e;' file.txt
 
K

Keith Thompson

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?
 
H

houda

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"

I am sorry not being clear.
This RE takes only the first part of RE (Search)
perl -p -e 's/\{\}\&\{\}/($.<5?"{newstring1}":"{newstring2}")/e;'
text.txt

My old string can appear more than one time.
(oldstring)(oldstring)(oldstring) replaced by (newstring1)(newstring2)
(newstring1)
Many Thanks
 

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,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top