Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add a new line after a match

Status
Not open for further replies.

spalmarez

IS-IT--Management
Aug 17, 2005
7
US
I would like to add a new line after a match. I know I can edit a file doing a perl -pi -e "s/(\d+)\s+\d+-\d\d\d\d)//g" filename which this would replace the phone number with something but can I add a newline after my phone number or how would I do this in a script? The file is huge.

here's part of the data I am looking at in my file:

N07Y O000000 1997000.00PSIN000.00P SI 01 (111) 111-11112005082200036424
 
perl -pi -e "s/(\d+)\s+\d+-\d\d\d\d[red]\n[/red])//g

... is this what you need?


Kind Regards
Duncan
 
sorry - cock up!

Code:
perl -pi -e "s/(\d+)\s+\d+-\d\d\d\d)/[red][b]\n[/b][/red]/g


Kind Regards
Duncan
 
Sort of, After the match of the phone number I want to add the new line so this wouldn't work because it would replace the phone number with new line:

perl -pi -e "s/(\d+)\s+\d+-\d\d\d\d)/\n/g"

Any other ideas?



 
sorry - your example confused me a bit as it explicitly replaces the phone number with nothing

how about this:-

perl -pi -e "s/(\d+)\s+\d+-\d\d\d\d)/[red]$1\n[/red]/g


Kind Regards
Duncan
 
all the above regexp have unmatched parenthesis in them, looks like none of them will work as posted.
 
Thank you Kevin - i am glad you are awake! I am a dumb ass... i trusted the regex supplied when i should have checked it

Code:
[b]#!/usr/bin/perl[/b]

$_ = 'N07Y O000000  1997000.00PSIN000.00P SI 01 (111) 111-11112005082200036424';

s/^(.*) (\(\d{3}\) \d{3}-\d{4})(.*)$/$1$2\n$3/;

print;

outputs:-

N07Y O000000  1997000.00PSIN000.00P SI 01(111) 111-1111
2005082200036424


Kind Regards
Duncan
 
or this way - focusing only on the phone number:-

Code:
[b]#!/usr/bin/perl[/b]

$_ = 'N07Y O000000  1997000.00PSIN000.00P SI 01 (111) 111-11112005082200036424';

s/(\(\d{3}\) \d{3}-\d{4})(.*)$/$1\n$2/;

print;


Kind Regards
Duncan
 
Hi spalmarez

Firstly: Your regex is syntactically incorrect due to the last bracket - a bracket on its own is a capturing and you are closing this... and there is no open bracket. This causes Perl to complain

Secondly: Your regex would work if there were no brackets in the phone number around the first 3 digits - as you have not catered for them in the regex


Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top