テキストファイルを1行ずつ処理するwhileループを作成しようとしています.Awkを使用してフィールドが空白かどうかをテストし、その条件が真か偽かに基づいてアクションを実行します。Awk条件付きの内側のbash whileループ
入力ファイルはこれです:
$ cat testarr.csv
cilantro,lamb,oranges
basil,,pears
sage,chicken,apples
oregano,,bananas
tumeric,turkey,plums
pepper,,guavas
allspice,goose,mangos
私の予想される出力は次のとおりです。Using 'if' within a 'while' loop in Bashと同様のスレッドに基づいて
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
、私はこれをしなかった:
くれた#!/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
:
$ bash testloop.sh
this_is_one_iteration
ItIsBlank
ファイルを反復しているようにも見えません。しかし、私が条件付きを取り除くと、それはうまく反復処理されます。
#!/bin/bash
error=ItIsBlank
success=ItIsNotBlank
while read LINE; do
echo this_is_one_iteration
done < testarr.csv
ができます:awkを使用していないとき
$ 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
また、条件がOKに動作するようです:
QZ1=test
while read LINE; do
echo this_is_one_iteration
if [[ $QZ1=="test" ]] ; then
echo It_worked
fi
done < testarr.csv
は私に与える:
$ 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
これを 'bash'スクリプトや' Awk'コマンドでやりたいのですか? – Inian
私は気にしません。私は、フィールドが空白であるかどうかをテストし、それに基づいてバッシュを実行するだけです。 awkの出力をbashに渡すのはなぜですか? – Thoughtcraft
_proper_行は3つのフィールドで、不適切な行は3未満ですか? – Inian