Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

accessing awk variabls in shell

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
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,&quot;<&quot;)}; { split(a[2],b,&quot;>&quot;) }}
END {print b[1]}' count=&quot;$counter&quot; 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.

 
This is done by assigning the output of
awk to a shell variable by using back ticks:

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

You may have to reposition the second back tick
as I didn't have time to test it before posting.

Hope this helps


flogrr
flogr@yahoo.com

 
I tried your suggestion , but it doesnt work!! the backquotes..the variable does not get updated with the value
 

Hi borisyakoff-

I got it to work with minor edits to your script.

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

echo
echo The number to find is $myvar

You need not print the line ( $0 ) that matched your
pattern. Doing so puts that line in your output along
with the number 37 which was what you were after.

Also, you don't need the Korn shell in the shebang line
and the typeset is not necessary.

I proved this on a Sun Ultra2 box running Solaris 7
using the sample input you provided- it works.

Your mileage may vary, however!



flogrr
flogr@yahoo.com

 
I am trying to find the last occurence of a word in a file including all that follows after that word.I am trying to use sed but it dosen't work.

1,/ERROR/{
N
d
}

Input file
----------
Hello
Line1
ERROR
Hello
Line2
ERROR
Hello
Line3
ERROR
Hello
This line and the above Hello should stay

Output
------
Line2
ERROR

Actually, iwoudl want only the last two lines :
Hello
This line and the above Hello should stay

Any ideas?
 
# Hi Unised!

# Here is one way to do it. I'm sure there are others.


#
# File: findlast
#
# Purpose: To collect all lines in file after
# the last &quot;pattern&quot; has been found.
#
# Usage: findlast inputfile outputfile <CR>
#
# Note-
# The inputfile size may be too large to
# process if you have a relatively small
# amount of memory in the computer.
#
#

awk ' {

if (($0 ~ /ERROR/) && (!flag)) {
flag = 1
getline
}

if (flag) {
collect[++count] = $0
}

if (($0 ~ /ERROR/) && (flag)) {
for ( i = 1; i <= count; i++ )
delete collect
flag = 0
count = 0
}

} END {
for ( i = 1; i <= count; i++ )
print collect
printf $0 # printf so no newline

}' $1 > $2

flogrr
flogr@yahoo.com

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top