使AWK记录分隔符的最后一个记录后不适? [英] Make the record seperator in AWK not apply after the last record?

查看:179
本文介绍了使AWK记录分隔符的最后一个记录后不适?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更改 ORS 。我发现,记录分隔最后的输出记录后适用。有没有什么办法让它不适用于最后的输出记录?

I change ORS to be ,. I find that the record separator applies after the last output record. Is there any way to make it not apply to the last output record?

类似。确实 OFS 也适用于最后一个字段?如果是的话,我可以把它呢?

Similarly. does OFS also apply to the last field? If yes, can I make it not?

推荐答案

有几个技巧。

一个是,不使用内置的ORS。

One is, not using build-in ORS.

例如,而不是

$ seq 5 | awk -v ORS=",\n" '{print $0}'
1,
2,
3,
4,
5,

做到这一点。

$ seq 5 | awk '{printf "%s%s",sep,$0; sep=",\n"} END{print ""}'
1,
2,
3,
4,
5

另一个技巧是从输入文件中删除最后一个新行并使用RT作为代理(可能只呆子)

Another trick is removing the last new line from the input file and use RT as a proxy (might be gawk only)

$ seq 5 | head -c -1 | awk -v ORS=",\n" '{printf "%s%s",$0,RT?ORS:""}'
1,
2,
3,
4,
5   # <-- no newline here but you can add in the END block.

有关OFS你可以做同样的事情。

For OFS you can do something similar.

$ paste <(seq 5) <(seq 11 15) 
  | awk -v OFS=, '{sep=""; for(i=1;i<=NF;i++) {printf "%s",sep $i*$i; sep=OFS} printf "\n"}'

1,121
4,144
9,169
16,196
25,225

请注意,如果你不建行的循环,只是添加用逗号分隔就能解决问题的领域。例如打印$ 1 * $ 1,$ 2 * $ 2

Note that if you're not constructing the line in a loop, just adding the fields separated by comma will solve the problem. e.g. print $1*$1, $2*$2

这篇关于使AWK记录分隔符的最后一个记录后不适?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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