打印逗号除了在awk中的最后一行 [英] Print a comma except on the last line in Awk

查看:298
本文介绍了打印逗号除了在awk中的最后一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下脚本

awk '{printf "%s", $1"-"$2", "}' $a >> positions;

其中, $ A 存储文件的名称。其实我写多个列的值成一排。不过,我想打印一个逗号只如果我没有在最后一行。

where $a stores the name of the file. I am actually writing multiple column values into one row. However, I would like to print a comma only if I am not on the last line.

推荐答案

我会通过查找的行数之前运行脚本,例如做用的coreutils和bash:

I would do it by finding the number of lines before running the script, e.g. with coreutils and bash:

awk -v nlines=$(wc -l < $a) '{printf "%s", $1"-"$2} NR != nlines { printf ", " }' $a >>positions

这篇关于打印逗号除了在awk中的最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆