我有以下脚本
awk '{printf "%s", $1"-"$2", "}' $a >> positions;
其中$a
存储文件的名称。我实际上是将多个列值写入一行。但是,只有当我不在最后一行时,我才想打印逗号。
最佳答案
单遍方法:
cat "$a" | # look, I can use this in a pipeline!
awk 'NR > 1 { printf(", ") } { printf("%s-%s", $1, $2) }'
请注意,我还简化了字符串格式。
关于shell - 在 Awk 中打印除最后一行之外的逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14517930/