使用awk以相同的格式为所有列打印$ 0 [英] Use awk to print $0 using the same format for all columns
本文介绍了使用awk以相同的格式为所有列打印$ 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
举个例子,我有一个只有一行的文件
Taking an example, I have a file with one line
0.1 35 23.0e3 4.0D+03
,我想将其转换为格式良好的文件,例如
and I'd like to convert it to a nicely formatted file like
1.00e-01 3.50e+01 2.30e+04 4.00e+03
我知道我们可以使用printf
语句在awk
中执行此操作,但是如果列数很大,那将是乏味的.我想知道是否有一种方法可以设置所有列的格式,而仅使用print $0
?
I know we can do this in awk
using the printf
statement, but that would be tedious if the number of columns is big. I was wondering if there is a way to set the format for all columns, and just use print $0
?
推荐答案
考虑此输入:
$ a=$'0.1 35 23e3\n0.2 36 24e3';echo "$a"
0.1 35 23e3
0.2 36 24e3
此gnu awk将实现您期望的结果,而无需遍历字段:
This gnu awk will achieve what you expect without looping over the fields:
$ echo "$a" |awk '{printf "%.2e%s",$0,RT}' RS="[ ]|\n"
1.00e-01 3.50e+01 2.30e+04
2.00e-01 3.60e+01 2.40e+04
我故意排除了4.0D+03
,因为它似乎无效.
I have intentionally exclude 4.0D+03
since does not seem valid.
这篇关于使用awk以相同的格式为所有列打印$ 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文