简体   繁体   中英

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:

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:

Time in seconds = 260.37
Total processes = 1

Output: (t1.dat)

260.37
1

Desired output:

260.37 1

But, for now, awk is appending it as a new line in 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?

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

ORS is an abbreviation for output records separator . Perhaps Ed Morton has an easier way of doing it.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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