How do I skip first match?? 
Author Message
 How do I skip first match??

All,

Newbie help required for awk in skipping the first match.

I have a file that has two occurrences of "while(1)". I need to skip the
first one and replace the second one with "while (x>MAXLOOP)" - how can I
accomplish this? Pl note that the number of lines between the two "while"
occurrences is not same for all files being processed.

Any help???

Thanks!!

essbee



Mon, 17 Nov 2003 13:09:36 GMT  
 How do I skip first match??

Quote:

> All,

> Newbie help required for awk in skipping the first match.

> I have a file that has two occurrences of "while(1)". I need to skip the
> first one and replace the second one with "while (x>MAXLOOP)" - how can I
> accomplish this? Pl note that the number of lines between the two "while"
> occurrences is not same for all files being processed.

> Any help???

> Thanks!!

> essbee

Hello,

could you provide some lines of your input file and how your desired
output should look like?

It's not clear from your post what you mean.

Michael Heiming



Mon, 17 Nov 2003 14:20:40 GMT  
 How do I skip first match??

Quote:

> Newbie help required for awk in skipping the first match.

> I have a file that has two occurrences of "while(1)". I need to skip the
> first one and replace the second one with "while (x>MAXLOOP)" - how can I
> accomplish this? Pl note that the number of lines between the two "while"
> occurrences is not same for all files being processed.

But the first and second occurrences of "while(1)" will always be
on different lines, right? (Pretty safe assumption, I think.)

Set a counter to 1 at the first occurrence of the substring, then
set it to 2 at the second occurrence and perform the substitution:

    # untested
    {
        if (matches == 0 && /while\(1\)/)
            matches = 1
        else if (matches == 1 && sub(/while\(1\)/, "while (x>MAXLOOP)"))
            matches = 2

        print
    }

This technique exploits awk's short-circuit evaluation of compound
conditional expressions to minimize the amount of regular expression
pattern matching performed. (There's no need to keep looking for
the substring "while(1)" after you've seen it the second time.)

--
Jim Monty

Tempe, Arizona USA



Mon, 17 Nov 2003 15:04:29 GMT  
 How do I skip first match??

Quote:

> Newbie help required for awk in skipping the first match.

> I have a file that has two occurrences of "while(1)". I need to skip the
> first one and replace the second one with "while (x>MAXLOOP)" - how can I
> accomplish this?

awk '/while\(1\)/ && count++ { sub("while\(1\)","while(x>MAXLOOP)"}'

That will replace all occurrences except the first.
If you want to replace only the second (and skip possible 3rd &c), do

awk '/while\(1\)/ && ++count==2 ... '

--
Tapani Tarvainen



Mon, 17 Nov 2003 17:39:36 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. re.findall() is skipping matching characters

2. Forged email from skip@calendar.com and/or skip@automatrix.com

3. tcl 8.2 regexp not doing non-greedy matching correctly

4. Done my first window...

5. opencores doing first silicon

6. opencores doing first silicon

7. AWK question: first line that matches?

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

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

10. Return last match instead of first?

11. My first regex.error: match failure

12. find first regexp match

 

 
Powered by phpBB® Forum Software