Solved

perl print question

Posted on 2006-11-29
6
550 Views
Last Modified: 2008-01-16
I have this command that give me what I need but can't get the print out right.

perl -ne 'if (/(\[.*\])/ & /calid=(.*?).dtstart=(.*?)&.*dtend=(.*?)&(summary=.*)&desc=/ && !/uid=/) {print "$1\t$2\t$3\t$4\t$5\n"}' log.txt


problem here is always prints the $1 $2.... starting with the second set of  /..../   what do I need to do if I want to print  from both  

example: print $1 and $2 from this pattern
      '/.....(....)/  &  /...(...)/'


Thanks,

0
Comment
Question by:bt707
6 Comments
 

Author Comment

by:bt707
ID: 18038063

I got it to work by doing this.

perl -ne 'if (/(\[.*0000\]).*?calid=(.*?).dtstart=(.*?)&.*dtend=(.*?)&(summary=.*)&desc=/ && !/uid=/) {print "$1\t$2\t$5\t$3\t$4\n"}'  log.txt

but is there a better way to do what I was trying to do above.


Thanks,
0
 
LVL 84

Expert Comment

by:ozo
ID: 18040333
what would you consider to be better?
The main differerence between what you were saying you were trying to do and what you did is that what you did is that what dyou did won't match if the (\[.*0000\]) doed not come before the calid, and you print in a different order
0
 

Author Comment

by:bt707
ID: 18041227
I got that part sorted out, I had just posted this as a example but had a few things out of line here.

I figure the part out I needed for the search but what I was now trying to do was to see how you could print out the
$1 and $2 if you had them in a search such as  '/.....(....)/  &  /...(...)/'  everytime I try this it always ignores one of them

works fine if I have all the $n between /......./   but when I have a command with a  /...../ &  /....../ in it I can't get it to work.
so trying to figure out how you would print out $1 and $2 in a command such as this

perl -ne 'print  "$1\t$2\n" if /someword(.*?)someword/  & /someword(.*?)someword/' file.txt


Hope that shows what I'm trying to figure out.


Thanks,

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 17

Assisted Solution

by:mjcoyne
mjcoyne earned 200 total points
ID: 18041336
The problem is that it's two regular expressions.  If you try:

perl -ne 'print  "$1\t$2\n" if /firstword(.*?)firstword/  & /secondword(.*?)secondword/' file.txt

and they both match, then $1 holds the "firstword" match for a very short time, because the contents of $1 is immediately replaced by the "secondword" match.

You need to reassign the contents of $1 from the first match to another variable before proceeding to the second match, or it is lost...
0
 
LVL 20

Accepted Solution

by:
jmcg earned 300 total points
ID: 18041376
The assignments to $1, $2, etc are temporary, that is, they will be overwritten the very next time you have a pattern match with subgroups. You must capture the values of the subgroups before the second match is attempted. Trying to do this in a perl one-liner will probably become unwieldy.

One way to capture the subgroup matches is an array assignment:

instead of your example
    /.....(....)/  &  /...(...)/

you could do something like
    ($m1) = /.....(....)/ and ($m2) = /...(...)/;

followed by

    print "$m1\t$m2\n";
0
 

Author Comment

by:bt707
ID: 18041422
ok, Thanks to everyone for the explaination, now I see what the problem is and why it's doing what it is and thanks to jmcq for the good example of how to work this out.


Thanks again!!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
work on ods spreadsheet with perl in ubuntu 4 80
perl CPAN issue 3 117
Any syntax error for this clone.plscript 6 143
Ned Perl Snippet to Read Files in Directory 5 74
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

827 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question