Code Comments
Programming Forum and web based access to our favorite programming groups.hi there,
i'm scanning a file for a certain pattern. when it matches i would like
to remove/edit the identified field, in the identified line.
i have a solution for this using an if inside a for loop to find the
right field in the line.
(/\$\.MODEL/) {for (i=1; i<=NF; i++)
if ($i~/^\$\.MOD/) {$i=$i""}
}
this will turn RI_2 OUT IN 250 $.MODEL=PR
into RI_2 OUT IN 250
question: is there a nice way of doing this without loops? i find it
somehow a waste of resources. you've found the pattern already, why look
for it again?
thanks for the help,
tom
*************************************
don't answer by email, it's not valid
Post Follow-up to this message
Thomas Toth wrote:
> hi there,
>
> i'm scanning a file for a certain pattern. when it matches i would like
> to remove/edit the identified field, in the identified line.
>
> i have a solution for this using an if inside a for loop to find the
> right field in the line.
>
> (/\$\.MODEL/) {for (i=1; i<=NF; i++)
> if ($i~/^\$\.MOD/) {$i=$i""}
> }
>
>
> this will turn RI_2 OUT IN 250 $.MODEL=PR
>
> into RI_2 OUT IN 250
>
> question: is there a nice way of doing this without loops? i find it
> somehow a waste of resources. you've found the pattern already, why look
> for it again?
Why not just do this:
gawk 'sub(/\$\.MODEL[^[:blank:]]*/,"")'
If that doesn't work, you could maybe try using match to search for the
pattern, then use RSTART and RLENGTH to manipulate the string. Use this
to see how that works:
gawk 'match($0,/\$\.MODEL[^[:blank:]]*/){print RSTART, RLENGTH}'
The above both assume you're using the default field separator.
Regards,
Ed.
Post Follow-up to this messageIn article <4162b092$0$28012$5402220f@news.sunrise.ch>,
Thomas Toth <user@example.net> wrote:
>hi there,
>
>i'm scanning a file for a certain pattern. when it matches i would like
>to remove/edit the identified field, in the identified line.
>
>i have a solution for this using an if inside a for loop to find the
>right field in the line.
>
>(/\$\.MODEL/) {for (i=1; i<=NF; i++)
> if ($i~/^\$\.MOD/) {$i=$i""}
>}
What does {$i=$i""} do?
>this will turn RI_2 OUT IN 250 $.MODEL=PR
>
>into RI_2 OUT IN 250
As another poster noted, if all you're really trying to do is delete
a certain string, then a straightforward sub() or gsub() on the whole line
ought to do it.
>question: is there a nice way of doing this without loops? i find it
>somehow a waste of resources. you've found the pattern already, why look
>for it again?
If you really do need to do it in a loop, then one idea is to eliminate
the first check and just do:
{for (i=1; i<=NF; i++) if ($i~/^\$\.MOD/) $i=$i""}
Post Follow-up to this messagethanks for all the help, they worked well.
i guess i should look more into the functions as well, not just stop at
regexp and replace.
tom
Thomas Toth wrote:
> hi there,
>
> i'm scanning a file for a certain pattern. when it matches i would like
> to remove/edit the identified field, in the identified line.
>
> i have a solution for this using an if inside a for loop to find the
> right field in the line.
>
> (/\$\.MODEL/) {for (i=1; i<=NF; i++)
> if ($i~/^\$\.MOD/) {$i=$i""}
> }
>
>
> this will turn RI_2 OUT IN 250 $.MODEL=PR
>
> into RI_2 OUT IN 250
>
> question: is there a nice way of doing this without loops? i find it
> somehow a waste of resources. you've found the pattern already, why look
> for it again?
>
> thanks for the help,
>
> tom
>
> *************************************
> don't answer by email, it's not valid
Post Follow-up to this messagethanks for all the help, they worked well.
i guess i should look more into the functions as well, not just stop at
regexp and replace.
tom
Thomas Toth wrote:
> hi there,
>
> i'm scanning a file for a certain pattern. when it matches i would like
> to remove/edit the identified field, in the identified line.
>
> i have a solution for this using an if inside a for loop to find the
> right field in the line.
>
> (/\$\.MODEL/) {for (i=1; i<=NF; i++)
> if ($i~/^\$\.MOD/) {$i=$i""}
> }
>
>
> this will turn RI_2 OUT IN 250 $.MODEL=PR
>
> into RI_2 OUT IN 250
>
> question: is there a nice way of doing this without loops? i find it
> somehow a waste of resources. you've found the pattern already, why look
> for it again?
>
> thanks for the help,
>
> tom
>
> *************************************
> don't answer by email, it's not valid
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.