Solved

perl print question

Posted on 2006-11-29
6
547 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl Script to append xml file 32 191
MySQL Web Database 5 111
Perl Write to Specific line in a file 15 75
Control Number of Log Files -Perl 7 54
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…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now