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
Donc, vous voulez faire cela dans le script 'bash' ou une commande' Awk'? – Inian
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
la ligne _proper_ est avec 3 champs et les incorrects ont moins de 3? – Inian