Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

The regular expression in perl one liner not giving the expected output

by perladdict (Chaplain)
on Feb 03, 2010 at 06:56 UTC ( #821086=perlquestion: print w/ replies, xml ) Need Help??
perladdict has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,
I have file with contents
root@phaniraj ~]# cat p1
usmat-d0209.mail.aol.com
CentOS release 4.7 (Final)
usmat-d0210.mail.aol.com
CentOS release 4.7 (Final)
usmat-d0211.mail.aol.com
CentOS release 4.7 (Final)
usmat-d0212.mail.aol.com
CentOS release 4.7 (Final)
usmat-d0213.mail.aol.com
CentOS release 4.7 (Final)

I want to modify file to get output like

usmat-d0209.mail.aol.com CentOS release 4.7 (Final)
usmat-d0210.mail.aol.com CentOS release 4.7 (Final)
usmat-d0211.mail.aol.com CentOS release 4.7 (Final)
usmat-d0212.mail.aol.com CentOS release 4.7 (Final)

[root@phaniraj ~]# cat p1 | perl -p -e 's/^(usmat.*)\n(.*)/$1 phani $2 + raj/m '
From the above code i am getting result as
usmat-d0209.mail.aol.com phani rajCentOS release 4.7 (Final)
usmat-d0210.mail.aol.com phani rajCentOS release 4.7 (Final)
usmat-d0211.mail.aol.com phani rajCentOS release 4.7 (Final)
usmat-d0212.mail.aol.com phani rajCentOS release 4.7 (Final)
usmat-d0213.mail.aol.com phani rajCentOS release 4.7 (Final)

It is not matching second line properly

Comment on The regular expression in perl one liner not giving the expected output
Download Code
Re: The regular expression in perl one liner not giving the expected output
by CountZero (Bishop) on Feb 03, 2010 at 07:01 UTC
    Do not use </br> tags. Please put your data also in <code> ... </code> tags. That makes it a lot easier to look at it or download it to test it.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: The regular expression in perl one liner not giving the expected output
by jwkrahn (Monsignor) on Feb 03, 2010 at 07:34 UTC

    The -p switch on the command line tells perl to read one line at a time therefore the contents of $2 are always empty because there is nothing after the newline for it to match.    You need something like:

    perl -lpe'$\ = /^usmat/ ? " phani " : " raj\n"' p1
Re: The regular expression in perl one liner not giving the expected output
by JavaFan (Canon) on Feb 03, 2010 at 09:06 UTC
    Untested:
    perl -pe 's/\n/ / if /^usmat/'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://821086]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2014-07-13 06:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (247 votes), past polls