So I have a if statement inside an awk to check if $2 of a awk equals a specific IP but the test fails. So here is what I have.
# !/bin/sh
echo "Enter client ID"
read ID
echo "Enter month (01, 02, 03)"
read month
echo "Enter day (03, 15)"
read day
echo "Enter Year (07, 08)"
read year
date=$year$month$day
echo "Enter the source IP (x.x.x.x)"
read source
if (ls /archive/$ID$date.gz | awk '{print $1}'); then
echo "Please wait while your request is being processed."
zcat /archive/$ID$date.gz | awk '{FS = "^"} {if($2==$source) {print $1}}' | awk '{print $7}' | sort | uniq -c | sort -n
else
echo "Couldn't find file"
exit
fi;;
What you see when its run
Enter client ID
1234
Enter month (01, 02, 03)
12
Enter day (03, 15)
10
Enter Year (07, 08)
08
Enter the source IP (x.x.x.x)
10.1.5.42
archive/1234081210.gz
Please wait while your request is being processed.
#
No results but there should be...
However if I run the following manually it works fine
zcat /archive/1234081210.gz | awk '{FS = "^"} {if($2=="10.1.5.42") {print $1}}' | awk '{print $7}' | sort | uniq -c | sort -n
2 122:3:0
5 122:7:0
27 3:13416:1
So the problem seems to do with getting the if($2==$source) part working. I'm not sure if this is where the awk -v would come into play but any help would be appreciated.
TIA