2017-10-09 3 views
0

J'essaye de faire une boucle while qui traverse un fichier texte ligne par ligne, teste si un champ est vide en utilisant Awk, puis fait une action selon que cette condition est vraie ou fausse.Awk conditionnel intérieur bash while loop

Le fichier d'entrée est la suivante:

$ cat testarr.csv 
cilantro,lamb,oranges 
basil,,pears 
sage,chicken,apples 
oregano,,bananas 
tumeric,turkey,plums 
pepper,,guavas 
allspice,goose,mangos 

Ma sortie attendue est:

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 

basé sur Using 'if' within a 'while' loop in Bash et discussions similaires, je l'ai fait:

#!/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 

qui m'a donné :

$ bash testloop.sh 
this_is_one_iteration 
ItIsBlank 

Ainsi, il ne semble même pas que vous parcouriez le fichier. Cependant, si je sors le conditionnel, ça se passe bien.

#!/bin/bash 

error=ItIsBlank 
success=ItIsNotBlank 
while read LINE; do 
echo this_is_one_iteration 
done < testarr.csv 

donne:

$ 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 

aussi, le conditionnel semble fonctionner correctement lorsque vous n'utilisez awk:

QZ1=test 
while read LINE; do 
echo this_is_one_iteration 
if [[ $QZ1=="test" ]] ; then 
    echo It_worked 
fi 
done < testarr.csv 

Donne-moi:

$ 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 
+0

Donc, vous voulez faire cela dans le script 'bash' ou une commande' Awk'? – Inian

+0

Je m'en fous. J'ai juste besoin de tester si un champ est vide, puis faire des bash sur la base de cela. Pourquoi ça ne marche pas pour passer la sortie de awk à bash? – Thoughtcraft

+0

la ligne _proper_ est avec 3 champs et les incorrects ont moins de 3? – Inian

Répondre

1

Votre script est correct, sauf pour une erreur mineure. Ajoutez echo $ LINE et placez-le dans l'instruction awk. Awk dans votre script n'a aucune entrée à travailler.

#!/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 

Quand je lance le script maintenant:

[[email protected] 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 résoudre votre problème.

+0

Merci, cela a fonctionné presque, mais l'opérateur == n'a pas fonctionné dans ce contexte, à la place j'ai utilisé -eq et qui a donné la sortie attendue – Thoughtcraft

+0

Heureux que cela a fonctionné.Veuillez fermer le thread si le problème est résolu. J'ai édité la réponse au cas où quelqu'un d'autre rencontrerait le même problème –

+1

Le problème n'était pas '==' vs '-eq', c'était que vous avez besoin d'espaces autour de l'opérateur. '==' (ou '=') test pour l'égalité des chaînes, '-eq' teste l'égalité numérique. Dans ce cas, l'un ou l'autre fonctionnerait. –

0

teste si un champ est vide en utilisant awk

je suppose, il peut être réalisé avec un seul awk processus:

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 

La sortie:

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 
+0

Mais cela permettra-t-il au résultat du test de vérité d'exécuter le code bash? Mon point n'était pas d'imprimer "ItIsBlank", je faisais juste écho à cela comme un test de capacité à l'envoyer à bash. – Thoughtcraft