Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

perl print question

Posted on 2006-11-29
6
Medium Priority
?
557 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 17

Assisted Solution

by:mjcoyne
mjcoyne earned 800 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 1200 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
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…
Six Sigma Control Plans

670 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