• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Grep question: Search for second iteration of letter and replace

Explorer ,
Jan 22, 2017 Jan 22, 2017

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

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)

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?

TOPICS
Scripting

Views

475

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jan 22, 2017 Jan 22, 2017

Hi pixxxel, 

As I see:

^P[\s\d().-]+\KP

(^/)

Votes

Translate

Translate
Community Expert ,
Jan 22, 2017 Jan 22, 2017

Copy link to clipboard

Copied

Hi Troutmagnet​,

try this one:

search

P[\(\d\) -]+\rKP

or

P \([\d\) -]+\rKP

replace with

F

Have fun

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 22, 2017 Jan 22, 2017

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 22, 2017 Jan 22, 2017

Copy link to clipboard

Copied

Hi pixxxel, 

As I see:

^P[\s\d().-]+\KP

(^/)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 22, 2017 Jan 22, 2017

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 22, 2017 Jan 22, 2017

Copy link to clipboard

Copied

LATEST

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

P_inSecondLine1.png

P_inSecondLine2.png

Have fun

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 22, 2017 Jan 22, 2017

Copy link to clipboard

Copied

Alternative solution

find: ^(?i)P\h*\(?\h*\d.+\r\K^(?i)P

change: F

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines