Awk next line as column

Hi,

This forum rocks.

I think this might be an easy thing, but since I am new to awk, please help me.

input:

x y z 
1
a b c
2
d e f
3
g h i 
7

output:

x y z 1
a b c 2
d e f 3
g h i 7

Any awk single liners? Thanks in advance

here is one way to do it. basically save all odd lines to variable data and print data + the entire even line. assumes even number of lines in file

one line

awk '{if(NR%2==0){print data,$0}else{data=$0}}' input

more readable

awk '{
   if(NR%2==0)
   {
     print data,$0
   }
   else
   {
       data=$0
   }
}' input

The code by mirni is much cleaner so I suggest using that.

How about sed?

sed 'N; s/\n/ /' file

If you prefer awk:

awk '{a=$0; getline; print a,$0}' file

Please use code tags when posting sample input/output or code.

try this

xargs -L 2 < filename

Something similar

awk 'NR%2{printf $0OFS;next}1' infile

--ahamed

hi ahamed101
can you please explain you logic

For every even line number, it will avoid printing "\n"...

--ahamed

Thank you guys.

Every code has been checked and I didn't expect so many ways to solve my problem.

Thanks again.

Admittedly, my awk code has a slight bug -- if there is odd number of lines, it will double up the last line. To be entirely correct, one should check whether getline grabs a line or not, and append only if it succeeds:

 awk '{a=$0; a=(getline) ? a" "$0 : a; print a}'