次のawkスクリプトの意味を説明できる人はいますか?

次のawkスクリプトの意味を説明できる人はいますか?

入力ファイルから空のフィールドを含むレコードを削除するために、次のawkスクリプトが作成されました。しかし、理解するのが難しいです。この記事を書いた理由を詳しく説明してください。

awk -F, '{for(i=1;i<=NF;i++)if($i==""){next}}1' inputfile

ベストアンサー1

これをコンポーネントに分割しましょう。

awk                    # The actual program
-F,                    # Set the field delimiter to a comma
'{                     # Beginning of the main series of action statements
for(i=1;i<=NF;i++)     # Your typical for loop.   NF is the number of fields 
                       # in the input
  if($i==""){          # $i will expand to e. g. $1, then $2, etc.,  which 
                       # is each field in the input data
                       # If it is "" (or an empty string):
     next}             # Skip to the next entry.  End of conditional
                       # and of the foor loop (no braces here)
}                      # End of the main series of action statements
1'                     # awkish shorthand basically for `print`, and end of
                       # the script
inputfile              # The file to be processed

基本操作はデータを含むことであるため、awkスクリプトは空のデータフィールドを持つレコードをスキップし、スキップしていないすべての項目を印刷します。

おすすめ記事