Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > SR xtimes

Reply
Thread Tools

SR xtimes

 
 
julia
Guest
Posts: n/a
 
      10-01-2009
How to make this command search and replace xtimes.
perl -p -i -e 's/oldstring/newstring/g' file.txt

Thanks
 
Reply With Quote
 
 
 
 
julia
Guest
Posts: n/a
 
      10-01-2009
On 1 oct, 11:52, Tad J McClellan <ta...@seesig.invalid> wrote:
> julia <julia_2...@hotmail.com> wrote:
> > How to make this command search and replace xtimes.
> > perl -p -i -e 's/oldstring/newstring/g' file.txt

>
> That depends on what "xtimes" means.
>
> What does "xtimes" mean when you say it?
>
> xtimes per line?
>
> xtimes per file?
>
> x lines per file?
>
> ...?
>
> --
> Tad McClellan
> email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


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
 
Reply With Quote
 
 
 
 
Jim Gibson
Guest
Posts: n/a
 
      10-01-2009
In article
<b6257e51-5a88-48cb-954e->,
julia <> wrote:

> On 1 oct, 11:52, Tad J McClellan <ta...@seesig.invalid> wrote:
> > julia <julia_2...@hotmail.com> wrote:
> > > How to make this command search and replace xtimes.
> > > perl -p -i -e 's/oldstring/newstring/g' file.txt

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

--
Jim Gibson
 
Reply With Quote
 
Jim Gibson
Guest
Posts: n/a
 
      10-01-2009
In article <011020091156581241%>, Jim Gibson
<> wrote:


> 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

--
Jim Gibson
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-01-2009
julia <> writes:
> On 1 oct, 11:52, Tad J McClellan <ta...@seesig.invalid> wrote:
>> julia <julia_2...@hotmail.com> wrote:
>> > How to make this command search and replace xtimes.
>> > perl -p -i -e 's/oldstring/newstring/g' file.txt

>>
>> That depends on what "xtimes" means.
>>
>> What does "xtimes" mean when you say it?
>>
>> xtimes per line?
>>
>> xtimes per file?
>>
>> x lines per file?
>>
>> ...?
>>
>> --
>> Tad McClellan
>> email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

>
> 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) kst- <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"
 
Reply With Quote
 
houda
Guest
Posts: n/a
 
      10-01-2009
On 1 oct, 16:08, Keith Thompson <ks...@mib.org> wrote:
> julia <julia_2...@hotmail.com> writes:
> > On 1 oct, 11:52, Tad J McClellan <ta...@seesig.invalid> wrote:
> >> julia <julia_2...@hotmail.com> wrote:
> >> > How to make this command search and replace xtimes.
> >> > perl -p -i -e 's/oldstring/newstring/g' file.txt

>
> >> That depends on what "xtimes" means.

>
> >> What does "xtimes" mean when you say it?

>
> >> xtimes per line?

>
> >> xtimes per file?

>
> >> x lines per file?

>
> >> ...?

>
> >> --
> >> Tad McClellan
> >> email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

>
> > 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) ks...@mib.org *<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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57