2

I am trying to use an awk script that compiles several lines into one; how can I do that?

I'm using the command:

awk < $pot/towhee.prod 'NR==411{print$5,$6}''NR==412{print$5,$6}''NR==430{print$7,$8}' 

and it produces this:

0.24975E-01 0.00000E+00
0.36574E+04 0.59343E+02
-3340.691 0.399

but I want this:

0.24975E-01 0.00000E+00 0.36574E+04 0.59343E+02 -3340.691 0.399

How can I do it?

1
  • You can pipe the output to paste -sd ' ' - (note that you can do NR == 411 || NR == 412 {print $5, $6}) May 23, 2014 at 15:31

3 Answers 3

4

You could set the output record separator to a space (or tab). If you want the output to be printed with a terminating newline, you can change it back before the final print

awk 'BEGIN{ORS=" "} NR==411{print$5,$6}''NR==412{print$5,$6}''NR==430{ORS="\n"; print$7,$8}'
3

printf allows you more control over the print format, including no new line unless you specify one with a \n.

awk < $pot/towhee.prod 'NR==411{printf("%s %s ",$5,$6)}''NR==412{printf("%s %s ",$5,$6)}''NR==430{printf("%s %s ",$7,$8)}' 

%s denotes the string format of the variable. Other control letters are available that allow you more control over the formatting of each variable.

1

Try this,

awk < $pot/towhee.prod 'NR==411{print$5,$6}''NR==412{print$5,$6}''NR==430{print$7,$8}' | tr '\n' ' '
1
  • Thanks! i try this and it work to: awk < L040/towhee.prod 'NR==411{printf "%s"" ",$5 " " $6}''NR==412{printf "%s" " ", $5 " "$6}''NR==430{print $7 " " $8}'
    – Joao Macau
    May 23, 2014 at 14:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .