regular expression search replace

Discussion in 'Perl' started by piet22455, Mar 5, 2010.

  1. piet22455

    piet22455 New Member

    Joined:
    Mar 5, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0

    Introduction



    Hi all,
    I have been working the whole day on a one-liner that can do a search replace.. No results jet...

    What I need to do:
    I have a datafile called data.txt
    the content of the datafile:

    1? john
    2? paul
    3? mary
    .....
    11? stuart
    12? peter

    And so on. There are 28 lines in the file.

    Now I want to change the line that begins with "1?" to "1? Has left the company."

    It should be a oneliner that will be called from a batch script.
    something like:
    perl -p -i -e "s/search/replace" "data.txt"

    Does anyone have an idea.. I came up with several variations but ether nothing got replaced or 1 and 10 and 11, etc got replaced.

    Any help would be most welcome.

    Regards,
    Pieter
     
  2. ungalnanban

    ungalnanban New Member

    Joined:
    Feb 19, 2010
    Messages:
    45
    Likes Received:
    2
    Trophy Points:
    0
    Location:
    Chennai
    Are you looking for this.

    Code:
    perl -p -e "s/^1[?]/1? Has left the company./" data.txt
    
    Output:

    1? Has left the company. john
    2? paul
    3? mary
    .......
    11? stuart
    12? peter
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice