awk

awk

Post by emmbe » Wed, 25 Jul 2001 09:12:42



Why doesn't this work:

#!/bin/ksh
for I in $(cat $1); do
    if [ `echo $I | awk '{$1 ~ /Feature/}'` ]; then
        FEATURE=echo $I | awk '{ print $2 }'
        echo $FEATURE
    fi
done

What I'm trying to do is to read the second field from each line of the file
specified on the command line ($1)that contains the string "Feature".  I
have a file with multiple lines, some lines beginning with "Feature"  I want
to retrieve the field following Feature.

 
 
 

awk

Post by Steve Greatbank » Wed, 25 Jul 2001 09:27:17



Quote:> Why doesn't this work:

> #!/bin/ksh
> for I in $(cat $1); do
>     if [ `echo $I | awk '{$1 ~ /Feature/}'` ]; then
>         FEATURE=echo $I | awk '{ print $2 }'
>         echo $FEATURE
>     fi
> done

> What I'm trying to do is to read the second field from each line of the
file
> specified on the command line ($1)that contains the string "Feature".  I
> have a file with multiple lines, some lines beginning with "Feature"  I
want
> to retrieve the field following Feature.

you mean

awk '$1 ~ /Feature/ {print $2}' input > output

 
 
 

awk

Post by Chris F.A. Johnso » Thu, 26 Jul 2001 06:41:15



> Why doesn't this work:

> #!/bin/ksh
> for I in $(cat $1); do
>     if [ `echo $I | awk '{$1 ~ /Feature/}'` ]; then
>         FEATURE=echo $I | awk '{ print $2 }'
>         echo $FEATURE
>     fi
> done

> What I'm trying to do is to read the second field from each line of the file
> specified on the command line ($1)that contains the string "Feature".  I
> have a file with multiple lines, some lines beginning with "Feature"  I want
> to retrieve the field following Feature.

It's a one-liner:

awk '/^Feature/ {print $2}' $1

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2001, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

awk

Post by Joe Halpi » Thu, 26 Jul 2001 06:52:04



> Why doesn't this work:

> #!/bin/ksh
> for I in $(cat $1); do

If the file is large, this may overrun the command line size. Don't
know if this is your problem, but what remains is just commentary on
my part.

Quote:>     if [ `echo $I | awk '{$1 ~ /Feature/}'` ]; then

There's no need to do this twice. You can just call awk on the file,
and it will loop through it line by line, reacting only to the lines
you specify.

Quote:>         FEATURE=echo $I | awk '{ print $2 }'

There's no need to do it three times in a row either.

Quote:>         echo $FEATURE
>     fi
> done

> What I'm trying to do is to read the second field from each line of
> the file specified on the command line ($1)that contains the string
> "Feature".  I have a file with multiple lines, some lines beginning
> with "Feature" I want to retrieve the field following Feature.

Sounds like you need to lean what awk is and what it does, since this
is exactly what it will do for you.

Joe

 
 
 

awk

Post by Emm Be » Fri, 27 Jul 2001 00:49:32


Thank you for your input.  This is my shot at it.

cat $1 | while read I; do
    TMP=`echo $I|awk '$1 ~ /Feature/ { print $2 }'`
    LIC=`echo $I|awk 'NF==1 && $1 ~ /^[0-9]/ { print $1 }'`

    if [ "$TMP" != "" ]; then
        FTRE=$TMP
    elif [ "$LIC" != "" ]; then
        ./inst.tcl $FTRE $LIC
    fi
done

The file that I parse will have some arbitrary content on the top that I'm
not interested in.  I'm only interested in line pairs that look like this:

Feature [number]
[one long field starting with numbers]
Feature [number]
[one long field starting with numbers]
Feature [number]
[one long field starting with numbers]
Feature [number]
[one long field starting with numbers]

This line pair combination is reapeated and I want to extract the feature
number and the number on next line and pass it to an expect script as $argv0
and $argv1.  The above code works, but I'm not going to argue its elegance.

Emm




> > Why doesn't this work:

> > #!/bin/ksh
> > for I in $(cat $1); do
> >     if [ `echo $I | awk '{$1 ~ /Feature/}'` ]; then
> >         FEATURE=echo $I | awk '{ print $2 }'
> >         echo $FEATURE
> >     fi
> > done

> > What I'm trying to do is to read the second field from each line of the
file
> > specified on the command line ($1)that contains the string "Feature".  I
> > have a file with multiple lines, some lines beginning with "Feature"  I
want
> > to retrieve the field following Feature.

> It's a one-liner:

> awk '/^Feature/ {print $2}' $1

> --
>     Chris F.A. Johnson                        http://cfaj.freeshell.org
>     ===================================================================
>     My code (if any) in this post is copyright 2001, Chris F.A. Johnson
>     and may be copied under the terms of the GNU General Public License

 
 
 

awk

Post by Chris F.A. Johnso » Fri, 27 Jul 2001 01:58:12



> Thank you for your input.  This is my shot at it.

> cat $1 | while read I; do
>     TMP=`echo $I|awk '$1 ~ /Feature/ { print $2 }'`
>     LIC=`echo $I|awk 'NF==1 && $1 ~ /^[0-9]/ { print $1 }'`

>     if [ "$TMP" != "" ]; then
>         FTRE=$TMP
>     elif [ "$LIC" != "" ]; then
>         ./inst.tcl $FTRE $LIC
>     fi
> done

> The file that I parse will have some arbitrary content on the top that I'm
> not interested in.  I'm only interested in line pairs that look like this:

> Feature [number]
> [one long field starting with numbers]
> Feature [number]
> [one long field starting with numbers]
> Feature [number]
> [one long field starting with numbers]
> Feature [number]
> [one long field starting with numbers]

> This line pair combination is reapeated and I want to extract the feature
> number and the number on next line and pass it to an expect script as $argv0
> and $argv1.  The above code works, but I'm not going to argue its elegance.

Doing the whole thing in awk will result in a much faster and more
efficient script:

awk '
## Match only lines beginning with "Feature"
## and whose second field is a number
/^Feature/ && match($2,"^[0-9]+$") {
     ## get the next line
     getline num

     ## if it is a number...
     if (match(num,"^[0-9]+$")) {
        ## create a command with the two numbers
        cmd = "./inst.tcl " $2 " " num
        ## execute the command
        system(cmd)
        }

- Show quoted text -

}' $1
> Emm




> > > Why doesn't this work:

> > > #!/bin/ksh
> > > for I in $(cat $1); do
> > >     if [ `echo $I | awk '{$1 ~ /Feature/}'` ]; then
> > >         FEATURE=echo $I | awk '{ print $2 }'
> > >         echo $FEATURE
> > >     fi
> > > done

> > > What I'm trying to do is to read the second field from each line of the
> file
> > > specified on the command line ($1)that contains the string "Feature".  I
> > > have a file with multiple lines, some lines beginning with "Feature"  I
> want
> > > to retrieve the field following Feature.

> > It's a one-liner:

> > awk '/^Feature/ {print $2}' $1

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2001, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License
 
 
 

awk

Post by Emm Be » Fri, 27 Jul 2001 02:42:42


I have tried your suggestion with a slight modification:

#!/bin/ksh

awk '/^Feature/ && match($2,"^[0-9]+$") {
   getline num
   if (match(num,"^[0-9]+$")) {
      echo " $2 num "
   }

Quote:}' $1

This doesn't work.  I get the following error message:

awk: syntax error near line 1
awk: bailing out near line 1

Emm




> > Thank you for your input.  This is my shot at it.

> > cat $1 | while read I; do
> >     TMP=`echo $I|awk '$1 ~ /Feature/ { print $2 }'`
> >     LIC=`echo $I|awk 'NF==1 && $1 ~ /^[0-9]/ { print $1 }'`

> >     if [ "$TMP" != "" ]; then
> >         FTRE=$TMP
> >     elif [ "$LIC" != "" ]; then
> >         ./inst.tcl $FTRE $LIC
> >     fi
> > done

> > The file that I parse will have some arbitrary content on the top that
I'm
> > not interested in.  I'm only interested in line pairs that look like
this:

> > Feature [number]
> > [one long field starting with numbers]
> > Feature [number]
> > [one long field starting with numbers]
> > Feature [number]
> > [one long field starting with numbers]
> > Feature [number]
> > [one long field starting with numbers]

> > This line pair combination is reapeated and I want to extract the
feature
> > number and the number on next line and pass it to an expect script as
$argv0
> > and $argv1.  The above code works, but I'm not going to argue its
elegance.

> Doing the whole thing in awk will result in a much faster and more
> efficient script:

> awk '
> ## Match only lines beginning with "Feature"
> ## and whose second field is a number
> /^Feature/ && match($2,"^[0-9]+$") {
>      ## get the next line
>      getline num

>      ## if it is a number...
>      if (match(num,"^[0-9]+$")) {
>         ## create a command with the two numbers
>         cmd = "./inst.tcl " $2 " " num
> ## execute the command
>         system(cmd)
> }
> }' $1

> > Emm




> > > > Why doesn't this work:

> > > > #!/bin/ksh
> > > > for I in $(cat $1); do
> > > >     if [ `echo $I | awk '{$1 ~ /Feature/}'` ]; then
> > > >         FEATURE=echo $I | awk '{ print $2 }'
> > > >         echo $FEATURE
> > > >     fi
> > > > done

> > > > What I'm trying to do is to read the second field from each line of
the
> > file
> > > > specified on the command line ($1)that contains the string
"Feature".  I
> > > > have a file with multiple lines, some lines beginning with "Feature"
I
> > want
> > > > to retrieve the field following Feature.

> > > It's a one-liner:

> > > awk '/^Feature/ {print $2}' $1

> --
>     Chris F.A. Johnson                        http://cfaj.freeshell.org
>     ===================================================================
>     My code (if any) in this post is copyright 2001, Chris F.A. Johnson
>     and may be copied under the terms of the GNU General Public License

 
 
 

awk

Post by Chris F.A. Johnso » Fri, 27 Jul 2001 03:30:13



> I have tried your suggestion with a slight modification:

> #!/bin/ksh

> awk '/^Feature/ && match($2,"^[0-9]+$") {
>    getline num
>    if (match(num,"^[0-9]+$")) {
>       echo " $2 num "
>    }
> }' $1

> This doesn't work.  I get the following error message:

> awk: syntax error near line 1
> awk: bailing out near line 1

You must be using an old version of awk. Instead of awk, use nawk, gawk
or mawk.

With nawk, you might still get a complaint about the "&&", in which case
move the test for the number inside the braces:

nawk '/^Feature/ { if (match($2,"^[0-9]+$")) {
                   ### etc....
                   }

- Show quoted text -

}' $1

> Emm




> > > Thank you for your input.  This is my shot at it.

> > > cat $1 | while read I; do
> > >     TMP=`echo $I|awk '$1 ~ /Feature/ { print $2 }'`
> > >     LIC=`echo $I|awk 'NF==1 && $1 ~ /^[0-9]/ { print $1 }'`

> > >     if [ "$TMP" != "" ]; then
> > >         FTRE=$TMP
> > >     elif [ "$LIC" != "" ]; then
> > >         ./inst.tcl $FTRE $LIC
> > >     fi
> > > done

> > > The file that I parse will have some arbitrary content on the top that
> I'm
> > > not interested in.  I'm only interested in line pairs that look like
> this:

> > > Feature [number]
> > > [one long field starting with numbers]
> > > Feature [number]
> > > [one long field starting with numbers]
> > > Feature [number]
> > > [one long field starting with numbers]
> > > Feature [number]
> > > [one long field starting with numbers]

> > > This line pair combination is reapeated and I want to extract the
> feature
> > > number and the number on next line and pass it to an expect script as
> $argv0
> > > and $argv1.  The above code works, but I'm not going to argue its
> elegance.

> > Doing the whole thing in awk will result in a much faster and more
> > efficient script:

> > awk '
> > ## Match only lines beginning with "Feature"
> > ## and whose second field is a number
> > /^Feature/ && match($2,"^[0-9]+$") {
> >      ## get the next line
> >      getline num

> >      ## if it is a number...
> >      if (match(num,"^[0-9]+$")) {
> >         ## create a command with the two numbers
> >         cmd = "./inst.tcl " $2 " " num
> > ## execute the command
> >         system(cmd)
> > }
> > }' $1

> > > Emm




> > > > > Why doesn't this work:

> > > > > #!/bin/ksh
> > > > > for I in $(cat $1); do
> > > > >     if [ `echo $I | awk '{$1 ~ /Feature/}'` ]; then
> > > > >         FEATURE=echo $I | awk '{ print $2 }'
> > > > >         echo $FEATURE
> > > > >     fi
> > > > > done

> > > > > What I'm trying to do is to read the second field from each line of
> the
> > > file
> > > > > specified on the command line ($1)that contains the string
> "Feature".  I
> > > > > have a file with multiple lines, some lines beginning with "Feature"
> I
> > > want
> > > > > to retrieve the field following Feature.

> > > > It's a one-liner:

> > > > awk '/^Feature/ {print $2}' $1

> > --
> >     Chris F.A. Johnson                        http://cfaj.freeshell.org
> >     ===================================================================
> >     My code (if any) in this post is copyright 2001, Chris F.A. Johnson
> >     and may be copied under the terms of the GNU General Public License

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2001, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License
 
 
 

1. awk: best way to handle multiple files with awk ?

Hello *,

what's the best way to handle multiple files in awk ?
The most simple problems I have is with two files:

file1 contains search patterns (in $3) which I want to
match with $2 of file2.

How to manage this ?

I'm thinking of a solution like

{ pattern[count++] = $3 }  < file1

{ for (i = 0; i < count; i++)
        if ( $2 == pattern[i] )
                print
                }          < file2

What's the best way to combine this in a single
awk program ?

Thanks in advance, bye

Stephan

2. Error while Kernel-Compilling

3. awk: Using awk as grep

4. udp ECONNREFUSED

5. Awk Trivia - what is happening here w/ cat | awk?

6. Bravo 16P Opti 931 ??

7. sed/awk - generating Makefiles (Re: awk processing of Makefile macros)

8. CPU Upgrade: Kingston Turbochip 133: Experiences?

9. Can I use awk in another awk's action?

10. Awk within awk??

11. awk program within another awk

12. Can I use awk in another awk's action?