SR xtimes

Discussion in 'Perl Misc' started by julia, Oct 1, 2009.

  1. julia

    julia Guest

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

    Thanks
    julia, Oct 1, 2009
    #1
    1. Advertising

  2. julia

    julia Guest

    On 1 oct, 11:52, Tad J McClellan <> wrote:
    > julia <> 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
    julia, Oct 1, 2009
    #2
    1. Advertising

  3. julia

    Jim Gibson Guest

    In article
    <>,
    julia <> wrote:

    > On 1 oct, 11:52, Tad J McClellan <> wrote:
    > > julia <> 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
    Jim Gibson, Oct 1, 2009
    #3
  4. julia

    Jim Gibson Guest

    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
    Jim Gibson, Oct 1, 2009
    #4
  5. julia <> writes:
    > On 1 oct, 11:52, Tad J McClellan <> wrote:
    >> julia <> 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) <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"
    Keith Thompson, Oct 1, 2009
    #5
  6. julia

    houda Guest

    On 1 oct, 16:08, Keith Thompson <> wrote:
    > julia <> writes:
    > > On 1 oct, 11:52, Tad J McClellan <> wrote:
    > >> julia <> 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)  <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
    houda, Oct 1, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page