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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to use awk with a $variable 1

Status
Not open for further replies.

rhoover

Programmer
Jun 13, 2003
6
US
I am attempting to compare wc -l in multiple files to validate todays wc -l is greater than yesterdays. Here is what I was experimenting with:
#!/bin/ksh
---------------
# Compare the record count file by file to make sure we did not receive less
---------------#
if [[ -a /u07/scripts/record_counts ]]
then
cat /dev/null > /u07/scripts/record_counts
else
touch /u07/scripts/record_counts
fi

for currentFile in /u14/tempdata/staging/*.in
do
wc -l $currentFile >> /u07/scripts/record_counts
done

sed 's/\/u14\/tempdata\/staging\///' < /u07/scripts/record_counts > /u07/scripts/pre_record_counts

cat /dev/null > /u07/scripts/record_counts

for currentFile in /u14/tempdata/staging_bkp/*.in
do
wc -l $currentFile >> /u07/scripts/record_counts
done

sed 's/\/u14\/tempdata\/staging\///' < /u07/scripts/record_counts > /u07/scripts/post_record_counts

for SAPfile in `cut -f 2 -d ' ' post_record_counts`
do
preCNT=`awk '/$SAPfile/' pre_record_counts|cut -f 1 -d ' '`;export preFile
postCNT=`awk '/$SAPfile/' post_record_counts|cut -f 1 -d ' '`;export postFile
if (($preCNT < $postCNT))
then
echo Danger Will Robinson, $SAPfile pre $preFile vs post $postFile
else
echo GOOD $SAPfile
fi
done

------
I am getting the following error:
awk: There is a regular expression error.
Invalid pattern.
The input line number is 1. The file is pre_record_counts.
The source line number is 1.
awk: There is a regular expression error.
Invalid pattern.
The input line number is 1. The file is post_record_counts.
The source line number is 1.
ksh[5]: < : syntax error
GOOD zmm01.in


 
Your script is rather hard to follow, but it looks like you're having trouble embedding shell variables in an awk script. Take a look at the FAQ in this forum:

faq271-1281

Regards,


Ed
 
Thanks Ed,

You can probably tell I'm new at this. The FAQ definately got me much closer. This is what I used for the variable now:
for SAPfile in `cut -f 2 -d ' ' post_record_counts`
do
preCNT=`awk '/{print"'"$SAPfile"'"}/' pre_record_counts|cut -f 1 -d ' '`;export preFile
done

Here is an example of post_record_counts:
462374 zmm01.in
45779 bkpf.in
202700 bseg.in
61862 eban.in
61706 ebkn.in
183445 ekbe.in
1121484 eket.in
205775 ekkn.in
29970 ekko.in

This is an example of what I get now: (veda.in & zmm01.in are the shell variables)
awk: There is a regular expression error.
?, *, or + not preceded by valid regular expression
The source line number is 1.
The error context is
>>> /{print"veda.in"}/ <<<
awk: There is a regular expression error.
?, *, or + not preceded by valid regular expression
The source line number is 1.
The error context is
>>> /{print"zmm01.in"}/ <<<
 

Sorry, but I can not see what you are doing within the awk script?


 
You've got the code in the wrong place in your awk script. The syntax is awk '/regexp/ {code}', not awk '/{code}/'. You can omit the /regexp/ part completely if you want to process all lines of the input file.

Annihilannic.
 
Here is an example of post_record_counts:
462374 zmm01.in
45779 bkpf.in
202700 bseg.in
61862 eban.in
61706 ebkn.in
183445 ekbe.in
1121484 eket.in
205775 ekkn.in
29970 ekko.in

The first field is Word Count and the Second Field is File name. I am trying to loop through that file using the second field as a variable. Then within the loop setting 2 other variables both being field 1 of 2 different files so I can compare the 2 counts.

To test Annihilannic suggestion, I performed the following just from the command line:

< ronh@locutus: /home/ronh > [vi (ronh) pts/tb (ansi)]
$ preCNT=`awk '/ekbe.in/' pre_record_counts|cut -f 1 -d ' '`;export preCNT

< ronh@locutus: /home/ronh > [vi (ronh) pts/tb (ansi)]
$ echo $preCNT
183445


$ preCNT=`awk '/regexp/ ekbe.in)' pre_record_counts|cut -f 1 -d ' '`;export preCNT
echo $preCNT syntax error The source line is 1.
The error context is
/regexp/ >>> ekbe. <<< in
awk: Quitting
The source line is 1.
 
Does this help:

Code:
preCNT=`awk ' $2 ~ /ekbe.in/ { print $1 } ' pre_record_counts`;export preCNT
echo $preCNT

SAPfile=ekbe.in
preCNT=`awk ' $2 ~ /'"$SAPfile"'/ { print $1 } ' pre_record_counts`;export preCNT
echo $preCNT
 
OldEd,

You are the man. I tried to find a BOW Smiley but there are none. Thanks - Ron
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top