Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

using identified field
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

Report this thread to moderator Post Follow-up to this message
Old Post
Thomas Toth
10-05-04 08:55 PM


Re: using identified field

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.

Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
10-05-04 08:55 PM


Re: using identified field
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""}


Report this thread to moderator Post Follow-up to this message
Old Post
Kenny McCormack
10-05-04 08:55 PM


Re: using identified field
{ sub( / \$\.MODEL=PR[^ ]*/, "" }

Report this thread to moderator Post Follow-up to this message
Old Post
William James
10-05-04 08:55 PM


Re: using identified field
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

Report this thread to moderator Post Follow-up to this message
Old Post
Thomas Toth
10-07-04 01:55 PM


Re: using identified field
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

Report this thread to moderator Post Follow-up to this message
Old Post
Thomas Toth
10-14-04 01:55 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:49 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.