0

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
4
  • So you want to do this in bash script or an Awk command? Commented Oct 9, 2017 at 17:25
  • I don't care. I just need to test whether a field is blank and then do bash stuff based on that. Why doesn't it work to pass the output of awk to bash? Commented Oct 9, 2017 at 17:28
  • the proper line is with 3 fields and the improper ones have less than 3? Commented Oct 9, 2017 at 17:30
  • Not necessarily, although in this specific example that happens to be true. So no, simply testing the number of fields in the line will not help me. Commented Oct 9, 2017 at 17:38

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, that almost worked, but the == operator didn't work in this context, instead I used -eq and that gave the expected output
Glad it worked . Please Close the thread if the issue is resolved . I have edited the answer in case someone else comes across the same problem
The problem wasn't == vs. -eq, it was that you need spaces around the operator. == (or =) test for string equality, -eq tests for numeric equality. In this case, either would work.
0

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

1 Comment

But will this allow the result of the truth test to execute bash code? My point wasn't to print "ItIsBlank", I was just echoing that as a test of ability to send it to bash.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.