how to match a pattern 
Author Message
 how to match a pattern

all,
how to match a patten in a line and then printing subsequnt lines below that
match line till the end of the file...

thanks.



Sat, 16 Aug 2003 09:21:53 GMT  
 how to match a pattern

Quote:
> all,
> how to match a patten in a line and then printing subsequnt lines below
that
> match line till the end of the file...

Try the following (note it should be a single line):

        awk '{
                   if (/pattern/) { ok = 1; }
                   if (ok) print $0;
                 }' myFile

The way this works is:

* Variable 'ok' is initially 0 (false) so nothing is printed
* Once the /pattern/ has been found, 'ok' is set to 1 (true)
* Since 'ok' is now true, each record is printed until
   end-of-file

I hope this helps.



Sat, 16 Aug 2003 12:26:13 GMT  
 how to match a pattern

Quote:

>all,
>how to match a patten in a line and then printing subsequnt lines below that
>match line till the end of the file...

awk 'c==1 {print} /pattern/ {c=1}' infile

prints all lines after the match, though this is shorter:

awk 'c /pattern/ {c=1}' infile

and this prints those lines AND the match:

awk '/pattern/ {c=1} c' infile

All this is untested, but should work.

Chuck Demas

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Sat, 16 Aug 2003 12:38:31 GMT  
 how to match a pattern

...

Quote:
> awk 'c /pattern/ {c=1}' infile

Well, you did say untested. Make it

awk 'c; /pattern/ {c=1}' infile

to avoid a parse error (at least with gawk).



Sat, 16 Aug 2003 14:08:52 GMT  
 how to match a pattern

Quote:

> how to match a patten in a line and then printing subsequnt lines below that
> match line till the end of the file...

I take it you don't want to print the matching line itself?
This is easiest with sed:

sed '1,/pattern/d'

With awk, e.g.,

awk '1,/pattern/{next} {print}'

or

awk 'f{print} /pattern/{f=1}'

or shorter

awk 'f
/pattern/{f=1}'

In recent versions of awk you can also do

awk 'f; /pattern/{f=1}'

--
Tapani Tarvainen



Sat, 16 Aug 2003 17:24:10 GMT  
 how to match a pattern


Quote:
> all, how to match a patten in a line and then printing subsequnt lines
> below that match line till the end of the file...

> thanks.

You might want sed for this:

sed -e'1,/pattern/d'

will print all lines after the pattern but not including the pattern

sed -ne'/pattern/,$p'

will print all lines after the pattern, but including the matched line.

You can also rearrange the above ones by playing about with -n, ...p
...d and the not operator. I expect one variation is marginally faster
than the others.

-Ed

--
                                                     | u98ejr

             Share, and enjoy.                       | eng.ox
                                                     | .ac.uk



Sun, 17 Aug 2003 06:06:29 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. awk question about matching a pattern

2. can't match a pattern

3. Printing certain lines after matching a pattern

4. regsub doesn't seem to match largest pattern

5. iss-matching - the free Regular Expression / Pattern Matching cluster

6. How to delete a line if next one matches a pattern?

7. Search last line match a pattern in a large file.

8. Expect: Weird bug (?) in matching a pattern in interact

9. pattern match and remove...

10. awk pattern matching?

11. text manipulation and pattern match

12. Printing matched pattern

 

 
Powered by phpBB® Forum Software