简体   繁体   中英

Awk conditional inside bash while loop

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

Your script is correct except for a minor error. Add echo $LINE and pipe it to the awk statement. Awk in your script has no input to work on.

#!/bin/bash 

error=ItIsBlank
success=ItIsNotBlank
while read LINE; do
echo this_is_one_iteration
QZ1=$(echo $LINE|awk -F "," '{print (!$2)}')
if [[ $QZ1 -eq 0 ]] ; then
 echo $error
else
 echo $success 
fi
done < testarr.csv

When i run the script now:

[ec2-user@ip check]$ ./script.sh
this_is_one_iteration
ItIsBlank
this_is_one_iteration
ItIsBlank
this_is_one_iteration
ItIsBlank
this_is_one_iteration 
ItIsBlank
this_is_one_iteration
ItIsBlank
this_is_one_iteration
ItIsBlank
this_is_one_iteration
ItIsBlank

Hope this resolves your issue.

tests whether a field is blank using Awk

I suppose, it can be achieved with single awk process:

awk -F, '{ print "this_is_one_iteration"; f="Not"; 
           for(i=1;i<=NF;i++) if($i=="") { f="";break }; printf "ItIs%sBlank\n",f }' testarr.csv

The output:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM