简体   繁体   English

如何使用awk中print命令的输出附加新列?

[英]How to append new column using output from print command in awk?

I'm trying to add the result of a print statement as a new column in the file t1.dat: 我正在尝试将打印语句的结果添加为文件t1.dat中的新列:

awk '/Time in seconds/ {print $5}' bt.B.1.log > t1.dat
awk '/Total processes/ {print $4; exit}' bt.B.1.log >> t1.dat

Contents of bt.B.1.log: bt.B.1.log的内容:

Time in seconds = 260.37 时间以秒为单位= 260.37
Total processes = 1 总过程= 1

Output: (t1.dat) 输出:(t1.dat)

260.37
1

Desired output: 所需的输出:

260.37 1

But, for now, awk is appending it as a new line in t1.dat. 但是,目前,awk将其作为新行添加到t1.dat中。 How do I get it to append it as new column in an existing file (in this case, t1.dat), without using intermediate files? 我如何在不使用中间文件的情况下将其作为新列追加到现有文件中(在本例中为t1.dat)?

awk 'BEGIN{ORS=" "} {if(NR==1) {print $5} else {print $4}}' bt.B.1.log

ORS is an abbreviation for output records separator . ORS输出记录分隔符的缩写。 Perhaps Ed Morton has an easier way of doing it. 也许埃德·莫顿(Ed Morton)有一个更简单的方法。

Alternatively: 或者:

awk '{if(NR==1) {printf $5} else {printf " "$4"\n"}}' bt.B.1.log

Do you want a line break at the end or not? 您是否要在结尾处换行?

Another way: 其他方式:

$ awk -"F=" '{ result = $2; getline; result = (result $2); print result; }' bt.B.1.log
 260.37 1

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

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