eja
April 3, 2007, 12:51pm
1
I have an awk statement where I Need to pass an environment variable but I cannot get it to work:
My evironment varible examples below:
$FILE1=/dev/fs/file.new
$FILE2=/dev/fs/file.old
Code below:
awk -F"|" '
BEGIN {
while( getline < "$FILE1" )
{ arr[$1]=1 }
}
arr[($1)] != 1 { print }
' $FILE2
I cannot hard code the file names into the awk statement. I need to apss the file name as a varibale. but my above awk is not reading $FILE1 as the value it is defined as in the environment.
$FILE1=/dev/fs/file.new
$FILE2=/dev/fs/file.old
nawk -F'|' -v OFS='|' '
NR==FNR { arr[$1]; next }
arr[$1]
' $FILE1 $FILE2
eja
April 3, 2007, 1:35pm
3
THank you for the reply.
Unfortunately I am running ina UNIX emulator WIndows Services for Unix. It does not ahve nawk. can this be done in awk?
eja
April 3, 2007, 2:02pm
5
I did after I sent the reply. Sorry should have thought before I typed.
and....? the verdict is....???
eja
April 3, 2007, 2:11pm
7
Yes that would help. you can tell I am a newbie. Works great. I changed it slightly but it works.
awk -F'|' '
NR==FNR { arr[$1]=1; next }
arr[$1] !=1' $HOME/$WORK/$NewFile $HOME/$COMPARE/$OldFile > $HOME/$WORK/$NewFile.del
The above compares yesterdays file to todays and find deleted records.
Thanks very much for the help.
eja:
Yes that would help. you can tell I am a newbie. Works great. I changed it slightly but it works.
awk -F'|' '
NR==FNR { arr[$1]=1; next }
arr[$1] !=1' $HOME/$WORK/$NewFile $HOME/$COMPARE/$OldFile > $HOME/$WORK/$NewFile.del
The above compares yesterdays file to todays and find deleted records.
Thanks very much for the help.
First, get used to using vB Codes when quoting either code or other's posts - it really makes it easier to read and would improve your chances of people pay attention to you posts.
Secondly, you really don't need to
arr[$1]=1
OR
arr[$1] != 1
both of the above is redundant: you don't need to assign '1' reading the first file as you're just building the hash the values of the hash don't matter as long as there's a key/index in a hash.
and you don't need to 'arr[$1] != 1' as the sheer presence of a key in a hash is enough. you re-code the second piece for ease of reading/understanding as:
$1 in arr
eja
April 3, 2007, 2:30pm
9
I did try it w/o =1 or !=1. When I did I got no results. As soon as I put the =1 and !=1. It displayed results.
one awkward awk you've got there - must be M$ or something.....
eja
April 3, 2007, 2:39pm
11
COuld be the version of awk. More likely me and something I am doing. But it does what i need for now. And it is all thanks to your help.
Again, thank you for taking the time to help.
eja
April 3, 2007, 5:08pm
12
Dear vgersh99,
One more followup question if you do not mind? I may need to compare other files where the static data is not just in the first field arr[$1].
It could be the 1st and 3rd arr[$1,$3] or 2nd and 5th etc..
Is there a way to write this so I have one script and I can pass the value to it? So somehting like: arr[$NUMS]. Where $NUMS may equal $1 or $1,$3?
NOTE: fields are defined on the command line by the value of a variable 'fields' - list of fields separated by a ','
awk -v flds='5,6,7' -f eja.awk FILE1 FILE2
eja.awk:
BEGIN {
SEP_list=","
n=split(flds, fldsA, SEP_list)
}
{
idx=""
for(i=1; i<=n; i++) idx=(i==1) ? $(fldsA) : idx SUBSEP $(fldsA)
#printf("[%f]: idx->[%s]\n", FNR, idx)
}
NR==FNR { arr[idx]=1; next }
arr[idx] !=1