Operating System - HP-UX
1819792 Members
3087 Online
109607 Solutions
New Discussion юеВ

awk script using system COMMAND

 
SOLVED
Go to solution
Glenn Morton_1
Occasional Advisor

awk script using system COMMAND

I have an awk script that was reccommened on 10/7/02. See the my previous entry, for details.
Here is the line that I'm having a problem with.

COMMAND=("grep -q " substr($0,53,4) " /tmp/reffile")

The above line works, but it needs to have a variable for the file name, based on SITECODE. I tried $REFFILE, but it didn't work. This script will use different DATAFILE files, and REFFILE files, based on SITECODE.
See the attachment for the exact coding.
The attachment has additional comments.
4 REPLIES 4
Sridhar Bhaskarla
Honored Contributor

Re: awk script using system COMMAND

Glenn,

If you checked that thread, I posted one script purely in Shell that can be easily modified to fit your purposes. It will take care of things like this.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
James R. Ferguson
Acclaimed Contributor

Re: awk script using system COMMAND

Hi Glenn:

You can pass a variable into a 'awk' script thusly:

#!/usr/bin/sh
typeset THEFILE=$1
awk -v THEFILE="$THEFILE"'
{ if ($2~/455/) {
if (substr($4,17,6)=="abcdef") {
COMMAND=("grep -q " substr($4,1,4) " "THEFILE)
if (system(COMMAND))
{print $0 "XXbb"
}
else
{print $0 "XXYY"
}
close(COMMAND)
}
else
{print $0"bbbb"
}
}
else
{print $0
}
} ' /tmp/myinput

Regards!

...JRF...
wyan lowe
Frequent Advisor

Re: awk script using system COMMAND

You might try this:

#!/bin/sh
#

FILE=$1
VAR=$2

echo "debug: file=$FILE var=$VAR"

echo "" | awk '{
printf "this is the '"${FILE}"' file.\n"
printf "and this the '"${VAR}"' variable\n"
}'

=======================
> ./pass_vars BLAH BLEH
debug: file=BLAH var=BLEH
this is the BLAH file.
and this the BLEH variable

> ./pass_vars "BLAH BLAH" "BLEH BLEH"
debug: file=BLAH BLAH var=BLEH BLEH
this is the BLAH BLAH file.
and this the BLEH BLEH variable



James R. Ferguson
Acclaimed Contributor
Solution

Re: awk script using system COMMAND

Hi (again) Glenn:

...sorry, priority interrupt. In the case you have shown, I'd simply create a procedure (function) to which you pass the value of the input file (INPFILE) and the reference file (REFFILE). This will allow you to call the 'awk' code with variable arguments. Mind you, this is but one way you can implement this.

#!/usr/bin/sh
typeset INPFILE=$1
typeset REFFILE=$2
#
function filter
{
awk -v REFFILE=$REFFILE '
{ if ($2~/455/) {
if (substr($4,17,6)=="abcdef") {
COMMAND=("grep -q " substr($4,1,4) " "REFFILE)
if (system(COMMAND))
{print $0 "XXbb"
}
else
{print $0 "XXYY"
}
close(COMMAND)
}
else
{print $0"bbbb"
}
}
else
{print $0
}
} ' $INPFILE
} #_endof_filter
#
filter $INPFILE $REFFILE
exit 0

...since the indentation will be mangled here, I've also attached the above script.

Regards!

...JRF...