For Programmers: Free Programming Magazines  


Home > Archive > AWK > October 2004 > using identified field









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author using identified field
Thomas Toth

2004-10-05, 3:55 pm

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
Ed Morton

2004-10-05, 3:55 pm



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.
Kenny McCormack

2004-10-05, 3:55 pm

In 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""}

William James

2004-10-05, 3:55 pm

{ sub( / \$\.MODEL=PR[^ ]*/, "" }
Thomas Toth

2004-10-07, 8:55 am

thanks 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

Thomas Toth

2004-10-13, 8:55 pm

thanks 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

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com