以下 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
的默认操作是包含数据,因此该脚本只是跳过具有任何空数据字段的记录,并打印未跳过的所有内容。