I am trying to execute the below code snippet to determine the presence of an entry in an extract. The problem is that the awk
is not working properly in the script whereas just as a command is it working perfectly.
Let me know if anything else is required!
-bash-4.2$ cat checkSub.sh
#!/bin/bash -x
path=`pwd`
while IFS= read -r line
do
SUB="`echo $line | cut -d"," -f1`"
VAL="`awk -F',' '$1 == "$SUB" {print 1}' ${path}/count.csv`"
#VAL=`cat ${path}/count.csv | grep $SUB | wc -l`
echo $VAL
if [ $VAL == 1 ];then
echo "$SUB has entry in the output sheet!"
else
echo "$SUB doesn't have an entry!"
fi
done < ${path}/GTPL_SLA.dat
PFB output. I am thinking if it has anything to do with using print
to assign value or something is wrong with the syntax itself
-bash-4.2$ ./checkSub.sh
++ pwd
+ path=<designated path>
+ IFS=
+ read -r line
++ echo STD_GTPL_UNSETL_1_NY_DL,01:30:00
++ cut -d, -f1
+ SUB=STD_GTPL_UNSETL_1_NY_DL
++ awk -F, '$1 == "$SUB" {print 1}' <designated path>/count.csv
+ VAL=
+ echo
+ '[' -eq 1 ']'
./checkSub.sh: line 12: [: -eq: unary operator expected
+ echo 'STD_GTPL_UNSETL_1_NY_DL doesn'\''t have entry!'
STD_GTPL_UNSETL_1_NY_DL doesn't have an entry!
+ IFS=
+ read -r line
++ echo STD_GTPL_UNSETL_2_CA_DL,00:00:00
++ cut -d, -f1
+ SUB=STD_GTPL_UNSETL_2_CA_DL
++ awk -F, '$1 == "$SUB" {print 1}' <designated path>/count.csv
+ VAL=
+ echo
+ '[' -eq 1 ']'
./checkSub.sh: line 12: [: -eq: unary operator expected
+ echo 'STD_GTPL_UNSETL_2_CA_DL doesn'\''t have entry!'
STD_GTPL_UNSETL_2_CA_DL doesn't have entry!
+ IFS=
+ read -r line
As a standalone command, it works like a charm :
-bash-4.2$ awk -F',' '$1 == "STD_GTPL_UNSETL_1_NY_DL" {print 1}' count.csv
1
-bash-4.2$
In the end, I have opted for grep
, but not a big fan of it, as it gives the desired result.
VAL=`cat ${path}/count.csv | grep $SUB | wc -l`
-bash-4.2$ ./checkSub.sh
1
STD_GTPL_UNSETL_1_NY_DL has entry in the output sheet!
0
STD_GTPL_UNSETL_2_CA_DL doesn't have entry!
-bash-4.2$
GTPL_SLA.dat
-bash-4.2$ cat GTPL_SLA.dat
STD_GTPL_UNSETL_1_NY_DL,01:30:00
STD_GTPL_UNSETL_2_CA_DL,00:00:00
-bash-4.2$
count.csv
-bash-4.2$ cat count.csv
STD_GTPL_UNSETL_1_NY_DL,9/8/2021,2021-09-08~22:25:20,2021-09-08~22:28:21,Completed,898724
-bash-4.2$
Ed, adding the code after following suggestions from shellcheck.net
-bash-4.2$ cat checkSub.sh
#!/bin/bash -x
#path=$(pwd)
while IFS= read -r line
do
VAL=0
SUB=$(echo "$line" | cut -d"," -f1)
VAL=$(awk -F"," -v a="$SUB" '"$1" == a {print 1}' count.csv)
#VAL=`cat ${path}/count.csv | grep $SUB | wc -l`
echo "$VAL"
if [ "$VAL" -eq 1 ];then
echo "$SUB has entry in the output sheet!"
else
echo "$SUB doesn't have entry!"
fi
done < GTPL_SLA.dat