shell - 在 Awk 中打印除最后一行之外的逗号

标签 shell awk separator

我有以下脚本

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/

相关文章:

bash - 在 bash 脚本中向十六进制数字添加前导零

linux - 使用 grep 提取匹配后的第一个单词

sed - 仅在引号之间替换空格

Pandas 读取以逗号分隔的千位分隔符格式的 CSV 数据

linux - 如何在 bash/zsh 中自动发出(新邮件)通知?

linux - 如何读取文件并从一个文件复制到shell脚本中的另一个文件

linux - 如何让我的 Golang Web 服务器在后台运行?

Python。 from dateutil.relativedelta import * 在 shell 中有效,但在脚本中无效

html - NSAttributedString 中的水平 <hr> 类分隔符

java - java swing 中的标题分隔符