Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • 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!

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

Donate Today!

Do you enjoy these
technical forums?
Donate Today! Click Here

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Add a new line after a match

spalmarez (IS/IT--Management) (OP)
25 Aug 05 18:03
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
duncdude (Programmer)
25 Aug 05 18:05
perl -pi -e "s/(\d+)\s+\d+-\d\d\d\d\n)//g

... is this what you need?


Kind Regards
Duncan

duncdude (Programmer)
25 Aug 05 18:10
sorry - cock up!

CODE

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


Kind Regards
Duncan

spalmarez (IS/IT--Management) (OP)
25 Aug 05 22:22
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?
 

 
 
duncdude (Programmer)
26 Aug 05 2:58
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

KevinADC (TechnicalUser)
26 Aug 05 3:14
all the above regexp have unmatched parenthesis in them, looks like none of them will work as posted.
duncdude (Programmer)
26 Aug 05 3:23
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

duncdude (Programmer)
26 Aug 05 3:26
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

duncdude (Programmer)
26 Aug 05 3:34
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

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!

Back To Forum

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