INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Jobs from Indeed

Add a new line after a match

Add a new line after a match

(OP)
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

RE: Add a new line after a match

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

... is this what you need?


Kind Regards
Duncan

RE: Add a new line after a match

sorry - cock up!

CODE

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


Kind Regards
Duncan

RE: Add a new line after a match

(OP)
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?
 

 
 

RE: Add a new line after a match

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)/$1\n/g


Kind Regards
Duncan

RE: Add a new line after a match

all the above regexp have unmatched parenthesis in them, looks like none of them will work as posted.

RE: Add a new line after a match

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

#!/usr/bin/perl

$_ = '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

RE: Add a new line after a match

or this way - focusing only on the phone number:-

CODE

#!/usr/bin/perl

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

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

print;


Kind Regards
Duncan

RE: Add a new line after a match

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close