> positions; 其中$a存储文件的名称。我实际上是将多个列值写入一行。但是,只有当我不在最后一行时,我-6ren">
gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:34:00 25 4
gpt4 key购买 nike

我有以下脚本

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com