Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AWK, multiple patterns per line 2

Status
Not open for further replies.

Plavixo

Programmer
May 5, 2008
2
GB
Hi there,

Forgive my inexperience, I haven't used AWK before. I'm a java writer normally! But I need to write an AWK script to print out certain html elements from webpages.

There may be several of these elements on one line. My question is, if there are more that one of these <tags> on a line, will awk pattern match the first one, perform the actions and then ignore the rest of the line? Or will it match the pattern, perform the action and then start where it left off, part way through the line?

If it does skip the rest of the line, how can I impliment a solution that will look at all the elements on a line?

Thank you for your help,
Plavixo
 
Unless you use the next instruction all patterns are checked.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
To identify multiple patterns on a line you need to do a for loop using the match() function, e.g.

Code:
echo 'blahyak
yakblahyakblahblah
yakyakyak
blah

'| awk '
        /blah/ {
                line=$0
                newline=""
                while(match(line,"blah")) {
                        print NR,substr(line,RSTART,RLENGTH)
                        line=substr(line,RSTART+RLENGTH)
                }
        }
'

Output is:

[tt]1 blah
2 blah
2 blah
2 blah
4 blah[/tt]

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top