0

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?

Lucas
  • 134
  • 14

2 Answers2

1
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?

tommy.carstensen
  • 8,962
  • 15
  • 65
  • 108
  • Both solutions work fine with the terminal. But now I'm stuck in another problem. Those awk commands don't work from within "system" calls in gnuplot (which is the bigger problem). Example (from within gnuplot prompt): system "awk '{if(NR==1) {printf $5} else {printf " "$4"\n"}}' bt.B.1.log". First solution gives the error message ';' expected at the second double quote and the second solution gives the error message invalid character \ at the backslash that precedes n. My colleague and I have been searching thoroughly for a solution but nothing yet. Any ideas that might help? – Lucas Apr 23 '15 at 19:04
  • Just escape the characters with a backslash `\`. Perhaps ask a new question tagged gnuplot. – tommy.carstensen Apr 23 '15 at 19:13
0

Another way:

$ awk -"F=" '{ result = $2; getline; result = (result $2); print result; }' bt.B.1.log
 260.37 1
dekkard
  • 6,121
  • 1
  • 16
  • 26