简体   繁体   English

使用awk以相同的格式为所有列打印$ 0

[英]Use awk to print $0 using the same format for all columns

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

I know we can do this in awk using the printf statement, but that would be tedious if the number of columns is big. 我知道我们可以使用printf语句在awk执行此操作,但是如果列数很大,那将很乏味。 I was wondering if there is a way to set the format for all columns, and just use print $0 ? 我想知道是否有一种方法可以设置所有列的格式,并且只使用print $0

Considering this input: 考虑以下输入:

$ a=$'0.1 35 23e3\n0.2 36 24e3';echo "$a"
0.1 35 23e3
0.2 36 24e3

This gnu awk will achieve what you expect without looping over the fields: 这个gnu awk将实现您期望的结果,而无需遍历各个字段:

$ 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

I have intentionally exclude 4.0D+03 since does not seem valid. 我故意排除了4.0D+03因为它似乎无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM