help for newbie: way to get the value of an awk variable in a shell script 
Author Message
 help for newbie: way to get the value of an awk variable in a shell script

Hi ,
I am writing a shell script in which I have to search for a pattern, get the
last occurence of the pattern in a file, read in the next line after the
last such occurence of the pattern, parse in this line so as to get a number
which is between the '<' and '>' marks. Now the script I have written does
this stuff and gets the number in awk . Now how am I to access this same
value outside the awk part. Any help would be appreciated. Here is my wee
script.

#! /usr/bin/ksh
typeset -i count
counter=`grep -c charVerifyFile input.err`
awk ' BEGIN {newcount=0}
         /charVerifyFile/ { ++newcount
                      if (newcount == count )
                         { print $0 ; k=NR } }
{if ( NR==k+1 ) { split($0,a,"<")}; { split(a[2],b,">") }}
END {print b[1]}' count="$counter" input.err

Here is the sample input file input.err
Exception report - Tue Jan 16 16:07:27 2001
1  charInit. 1000       376   charVerifyFile
   [Failed to scan valid # of elements from map file. Line # <32>]
2  charInit. 1000       111   charFile
3  unitTest.c   1000       151   Main
4  charMapInit. 1000       376   charVerifyFile    Second occ
   [Failed to scan valid # of elements from map file. Line # <37>]
5  charInit. 1000       111   charFile
6  unitTest.c   1000       151   Main

At the end of this stage the variable b[1] contains the desired value which
is 37 from the line
   [Failed to scan valid # of elements from map file. Line # <37>]. How do I
access this variable once outside awk.



Sun, 06 Jul 2003 01:55:41 GMT  
 help for newbie: way to get the value of an awk variable in a shell script

Quote:

>Hi ,
>I am writing a shell script in which I have to search for a pattern, get the
>last occurence of the pattern in a file, read in the next line after the
>last such occurence of the pattern, parse in this line so as to get a number
>which is between the '<' and '>' marks. Now the script I have written does
>this stuff and gets the number in awk . Now how am I to access this same
>value outside the awk part. Any help would be appreciated. Here is my wee
>script.

The output of any command can be substituted into another command using
backticks:

var=`awk ...`

--

Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.



Sun, 06 Jul 2003 02:12:40 GMT  
 help for newbie: way to get the value of an awk variable in a shell script

% which is between the '<' and '>' marks. Now the script I have written does
% this stuff and gets the number in awk . Now how am I to access this same
% value outside the awk part. Any help would be appreciated. Here is my wee

Do it precisely the same way you do with grep:

% counter=`grep -c charVerifyFile input.err`

you could have
 nbr=`awk -F'[<>]' '/charVerifyFile/ { nbr = $2 } END { print nbr }'`

The important thing is to only print the value you want to assign. Since
you use ksh, you could write
 $(awk ...)
instead of using back-quotes.

--

Patrick TJ McPhee
Knightsbridge SW7



Mon, 07 Jul 2003 02:36:27 GMT  
 help for newbie: way to get the value of an awk variable in a shell script

[ needs one-line output from awk in a shell variable ]
Quote:
> Do it precisely the same way you do with grep:

> % counter=`grep -c charVerifyFile input.err`

> you could have
>  nbr=`awk -F'[<>]' '/charVerifyFile/ { nbr = $2 } END { print nbr }'`

[ or ksh $( ... ) ]

Also, instead of first finding the number of occurrences
with grep and then using awk to search for the last one,
you could just have awk match on each occurrence,
remember it, and at END output the last one:
  nbr=`awk -F'[<>]'
  '/charVerifyFile/{x=NR+1} NR==x{y=$2} END{print y}' `

If you still need the number of occurrences (and number
of the last occurrence) for something else:
  eval `awk ...{x=NR+1;++cnt}...{print "cnt="cnt";nbr="nbr}'`

On some Unices, you could also use 'tac' to read
the file in reverse and select the now-first occurrence.

--
- David.Thompson 1 now at worldnet.att.net



Thu, 17 Jul 2003 06:59:42 GMT  
 help for newbie: way to get the value of an awk variable in a shell script

Quote:




> [ needs one-line output from awk in a shell variable ]
> > Do it precisely the same way you do with grep:

> > % counter=`grep -c charVerifyFile input.err`

> > you could have
> >  nbr=`awk -F'[<>]' '/charVerifyFile/ { nbr = $2 } END { print nbr }'`

> [ or ksh $( ... ) ]

> Also, instead of first finding the number of occurrences
> with grep and then using awk to search for the last one,
> you could just have awk match on each occurrence,
> remember it, and at END output the last one:
>   nbr=`awk -F'[<>]'
>   '/charVerifyFile/{x=NR+1} NR==x{y=$2} END{print y}' `

> If you still need the number of occurrences (and number
> of the last occurrence) for something else:
>   eval `awk ...{x=NR+1;++cnt}...{print "cnt="cnt";nbr="nbr}'`

> On some Unices, you could also use 'tac' to read
> the file in reverse and select the now-first occurrence.

Is that the standard way to reverse a file? I did think it
was tail -r until I discovered that GNU tail doesn't support
the option :(

Anyhow, you could do it with sed & tac;
nbr=`tac input.err|sed -n "/charVerifyFile/{g;s/.*<\([0-9]*\)>.*/\1/p;q;};h"`

Bye,

L

- Show quoted text -

Quote:
> --
> - David.Thompson 1 now at worldnet.att.net



Fri, 01 Aug 2003 04:50:41 GMT  
 help for newbie: way to get the value of an awk variable in a shell script

Quote:





> > [ needs one-line output from awk in a shell variable ]
> > > Do it precisely the same way you do with grep:

> > > % counter=`grep -c charVerifyFile input.err`

> > > you could have
> > >  nbr=`awk -F'[<>]' '/charVerifyFile/ { nbr = $2 } END { print nbr }'`

> > [ or ksh $( ... ) ]

> > Also, instead of first finding the number of occurrences
> > with grep and then using awk to search for the last one,
> > you could just have awk match on each occurrence,
> > remember it, and at END output the last one:
> >   nbr=`awk -F'[<>]'
> >   '/charVerifyFile/{x=NR+1} NR==x{y=$2} END{print y}' `

> > If you still need the number of occurrences (and number
> > of the last occurrence) for something else:
> >   eval `awk ...{x=NR+1;++cnt}...{print "cnt="cnt";nbr="nbr}'`

> > On some Unices, you could also use 'tac' to read
> > the file in reverse and select the now-first occurrence.

> Is that the standard way to reverse a file? I did think it
> was tail -r until I discovered that GNU tail doesn't support
> the option :(

> Anyhow, you could do it with sed & tac;
> nbr=`tac input.err|sed -n "/charVerifyFile/{g;s/.*<\([0-9]*\)>.*/\1/p;q;};h"`

> Bye,

Or buffer the result and do without tac altogether;
nbr=`sed -n '/charVerifyFile/{n;s/.*<\([0-9]*\)>.*/\1/;h;};${g;p;}' input.err`

- Show quoted text -

Quote:
> L

> > --
> > - David.Thompson 1 now at worldnet.att.net



Fri, 01 Aug 2003 05:06:22 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Getting awk computed value into shell variable

2. Getting A Day Using AWK or A Shell Script

3. Getting shell variables into awk

4. AWK-Script to set a shell variable (Win32) ?

5. Passing command line variables to AWK in shell script

6. Shell variable within an awk script

7. shell variable in awk script

8. need help substituting shell variable into search string with awk

9. awk variables --> shell variables

10. established ways to run shell scripts from proc

11. HELP: sourcing shell scripts or functions to change env variables

12. Set expect script variables via a korn shell script

 

 
Powered by phpBB® Forum Software