6 Replies Latest reply on Jan 22, 2017 12:04 PM by pixxxel schubser

    Grep question: Search for second iteration of letter and replace

    Troutmagnet Level 1

      Hi Grep peeps,


      I have an interesting one for you. I have a long document containing the following fields and properties:


      Name of company

      Business type

      Address line 1

      Address line 2

      P (000) 000-0000

      P (000) 000-0000

      W www.webaddress.ca

      E name@webaddress.com

      Name of owner

      Title of owner


      The list I have is populated with personalized live data, I omitted this for privacy reasons. The version of the listing above is just as an example.


      Using Grep (I assume) I'm trying to search replace the second "P" and turn it into an "F" for Fax Number. The client who supplied the file screwed up.


      My grep command to identify the line looks like this and it works:


      P \(?(\d\d\d)\)?[-. ]?(\d\d\d)[-. ]?(\d\d\d\d)



      So essentially to target the second P, the search looks for the first P, a set of three wildcards surrounded by brackets, followed by a space, three sets of digits, a dash, four sets of digits, a carriage return and then the second P.


      The search works and it find the lines that contain what I want to change.


      I was attempting this as a replacement:


      P \(?(\d\d\d)\)?[-. ]?(\d\d\d)[-. ]?(\d\d\d\d)



      ...and it works, but it replaces the entire line it finds with the actual code copy above. I was expecting it to ignore the wildcards and just replace the part that changed (the P into an F).


      I know there a magic solution to this, but I'm not aware of it. Any clues anyone?