user13153738
user13153738

Reputation:

How to print output side by side?

i have the following output from a script:

192.168.0.10
192.168.0.10
undefined
192.168.0.130
192.168.0.130
ansible
192.168.0.141
192.168.0.141
undefined
192.168.0.252
192.168.0.252
undefined

but i want it to be like:

192.168.0.10 192.168.0.10 undefined
192.168.0.130 192.168.0.130 ansible
192.168.0.141 192.168.0.141 undefined
192.168.0.252 192.168.0.252 undefined

i have seen how to get 2 lines next to each other but i fail at 3 ^^

awk '{getline b;printf("%s %s\n",$0,b)}'

Upvotes: 2

Views: 392

Answers (2)

RavinderSingh13
RavinderSingh13

Reputation: 133640

With awk could you please try following.

awk '{count++;printf("%s%s",$0,count%3==0?ORS:OFS)} END{if(count%3!=0){print ""}}' Input_file

Explanation: Adding detailed explanation for above code.

awk '                                       ##Starting awk program from here.
{
  count++                                   ##Increasing variable count value with 1 here.
  printf("%s%s",$0,count%3==0?ORS:OFS)      ##Printing current line and either new line(ORS) or space(OFS) based opon condition if count is divided by 3 or NOT.
}
END{                                        ##Starting END block for this awk code here.
  if(count%3!=0){                           ##Checking condition if count is NOT fully divided by 3 then do following.
    print ""                                ##Printing null variable here in case previous line was not having new line so to add it in last.
  }
}
'  Input_file                               ##Mentioning Input_file name here.

Upvotes: 3

Jetchisel
Jetchisel

Reputation: 7821

Try using paste.

paste - - - < file.txt

To use just a space delimiter.

paste -d ' ' - - - < file.txt

Upvotes: 2

Related Questions