AWK question: first line that matches? 
Author Message
 AWK question: first line that matches?

Hello,

As a new AWK user, I have been reading a couple of books on the
language, but haven't come up with a way to answer my question.  I
have a file in which there will probably be multiple lines that match
what I am looking for.  How would I pick out a certain one?  For
example, in one instance I might need the first match and in another,
I might need the second match.  Is there a way to pass a parameter to
tell AWK which instance I want?

Thanks in advance.

Ron



Fri, 14 Jun 2002 03:00:00 GMT  
 AWK question: first line that matches?

...

Quote:
>As a new AWK user, I have been reading a couple of books on the
>language, but haven't come up with a way to answer my question.  I
>have a file in which there will probably be multiple lines that match
>what I am looking for.  How would I pick out a certain one?  For
>example, in one instance I might need the first match and in another,
>I might need the second match.  Is there a way to pass a parameter to
>tell AWK which instance I want?

Yes. Use a variable assignment on the command line to pass the instance
you want to match and increment a counter for how many matches you've
found. Generic pseudocode:

awk 'a_match_expression { if (++counter == inst) print }' inst=3 input

should print the 3rd instance of a record satisfying a_match_expression.
If you want maximum terseness, try

awk 'a_match_expression && ++counter == inst' inst=3 input

Sent via Deja.com http://www.deja.com/
Before you buy.



Fri, 14 Jun 2002 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. awk first word on first line

2. awk -- pattern match a line and the line that follows

3. AWK: Problem processing first line

4. awk question about matching a pattern

5. Question: How to remove END OF LINE using AWK or SED

6. Selecting first line on screen and current line after reread in browselist

7. text widget, how to get line number of first line displayed

8. How do I skip first match??

9. regex: how to limit pattern match to first occurance

10. Pattern-Matching : who's on first?

11. Return last match instead of first?

12. My first regex.error: match failure

 

 
Powered by phpBB® Forum Software