I am trying to make a while loop that goes through a text file line by line, tests whether a field is blank using Awk, then does an action based on whether that condition is true or false.
The input file is this:
$ cat testarr.csv
cilantro,lamb,oranges
basil,,pears
sage,chicken,apples
oregano,,bananas
tumeric,turkey,plums
pepper,,guavas
allspice,goose,mangos
My expected output is:
this_is_one_iteration
ItIsNotBlank
this_is_one_iteration
ItIsBlank
this_is_one_iteration
ItIsNotBlank
this_is_one_iteration
ItIsBlank
this_is_one_iteration
ItIsNotBlank
this_is_one_iteration
ItIsBlank
this_is_one_iteration
ItIsNotBlank
based on Using 'if' within a 'while' loop in Bash and similar threads, I did this:
#!/bin/bash
error=ItIsBlank
success=ItIsNotBlank
while read LINE; do
echo this_is_one_iteration
QZ1=$(awk -F "," '{print (!$2)}')
if [[ $QZ1==0 ]] ; then
echo $error
else
echo $success
fi
done < testarr.csv
which gave me:
$ bash testloop.sh
this_is_one_iteration
ItIsBlank
So it doesn't even seem to be iterating through the file. However, if I take out the conditional, it iterates just fine.
#!/bin/bash
error=ItIsBlank
success=ItIsNotBlank
while read LINE; do
echo this_is_one_iteration
done < testarr.csv
gives:
$ bash testloop.sh
this_is_one_iteration
this_is_one_iteration
this_is_one_iteration
this_is_one_iteration
this_is_one_iteration
this_is_one_iteration
this_is_one_iteration
also, the conditional seems to work OK when not using awk:
QZ1=test
while read LINE; do
echo this_is_one_iteration
if [[ $QZ1=="test" ]] ; then
echo It_worked
fi
done < testarr.csv
Gives me:
$ bash testloop.sh
this_is_one_iteration
It_worked
this_is_one_iteration
It_worked
this_is_one_iteration
It_worked
this_is_one_iteration
It_worked
this_is_one_iteration
It_worked
this_is_one_iteration
It_worked
this_is_one_iteration
It_worked
bashscript or anAwkcommand?