Copy link to clipboard
Copied
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
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)
P
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)
F
...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?
Hi pixxxel,
As I see:
^P[\s\d().-]+\KP
(^/)
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Thanks for that, but when I search that string it's coming back with a "Cannot find match".
The space after the P is a tab. I tried replacing that space you have in there with the /t, but it still doesn't work.
Copy link to clipboard
Copied
Hi pixxxel,
As I see:
^P[\s\d().-]+\KP
(^/)
Copy link to clipboard
Copied
BINGO!
And the winner is Obi-wan!
Thanks, pixxxel schubser, you were close.
Thanks so much guys, now I'm going to sit down and try to figure out how that search works.
Copy link to clipboard
Copied
It's ok, if Obis grep works for you. There are much more possibilies with grep.
But both greps I've posted before are working for me. Perhaps a C&Paste-mistake on your side. (The space should really be a space or \s and \r is also required.)
Have fun
Copy link to clipboard
Copied
Alternative solution
find: ^(?i)P\h*\(?\h*\d.+\r\K^(?i)P
change: F