It is currently Thu, 20 Jan 2022 05:16:32 GMT



 
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
In article <979667743.651...@ernani.logica.co.uk>,

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

var=`awk ...`

--
Barry Margolin, bar...@genuity.net
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
In article <979667743.651...@ernani.logica.co.uk>,

% 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
p...@interlog.com



 Mon, 07 Jul 2003 02:36:27 GMT   
 
   [ 3 post ] 

Similar Threads

1. Getting awk computed value into shell variable

2. Variables getting the return value in Korn shell?

3. getting shell variables into awk

4. awk and sh variables in a shell script

5. awk and sh variables in a shell script

6. Help!How to assign a value to a variable in awk

7. Getting A Day Using AWK or A Shell Script

8. detecting difference between unset variable and variable whose value is null in Korn Shell


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software