2017-10-09 24 views
0

テキストファイルを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 
+0

これを 'bash'スクリプトや' Awk'コマンドでやりたいのですか? – Inian

+0

私は気にしません。私は、フィールドが空白であるかどうかをテストし、それに基づいてバッシュを実行するだけです。 awkの出力をbashに渡すのはなぜですか? – Thoughtcraft

+0

_proper_行は3つのフィールドで、不適切な行は3未満ですか? – Inian

答えて

1

あなたのスクリプトはを除いて正しいです軽微な誤り。 echo $ LINEを追加し、それをawkステートメントにパイプします。あなたのスクリプト内のawkは作業する入力がありません。

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

私は今、スクリプトを実行すると:

[[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 

が、これはあなたの問題を解決します願っています。

+0

ありがとう、それはほとんど機能しましたが、==演算子はこのコンテキストでは機能しませんでしたが、代わりに-eqを使用して、期待される出力を得ました – Thoughtcraft

+0

うまくいきました。問題が解決したらスレッドを閉じてください。他の誰かが同じ問題を抱えている場合に私は答えを編集しました –

+1

問題は '=='と '-eq'とではなく、演算子の周囲にスペースが必要でした。 '=='(または '=')文字列の等価性をテストし、 '-eq'は数値の等価性をテストします。この場合、どちらかが動作します。 –

0

かどうかをテストしますフィールドには、私が思う

Awkの

を使用して空白になって、それが単一 awkのプロセスで達成することができます:

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 

出力:

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

これで、真理テストの結果がbashコードを実行できるようになりますか?私の要点は「ItIsBlank」を印刷することではなく、私はそれをbashに送る能力のテストとしてエコーしていました。 – Thoughtcraft