Solved

perl print question

Posted on 2006-11-29
6
548 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

823 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