user3216879
user3216879

Reputation: 13

awk-convert first 2 lines to first row and next 2 lines to second row and so on

I would like to convert first 2 lines to first row and next 2 lines to second row and so on. Could someone help me? .would it possible to do by awk command.

File.txt has the below entries

Bin1
Bin2
Hex3
Hex4 
oct5
oct6

I would like to get output as below

Bin1  Bin2
Hex3 Hex4
oct5 oct6

Upvotes: 1

Views: 74

Answers (4)

Ed Morton
Ed Morton

Reputation: 203219

$ awk '{ORS=(NR%2?FS:RS)}1' file
Bin1 Bin2
Hex3 Hex4
oct5 oct6

Upvotes: 1

BMW
BMW

Reputation: 45223

Here is solution by sed

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

Upvotes: 1

Chris Seymour
Chris Seymour

Reputation: 85775

You could just use xargs:

$ xargs -n2 < file                                                          
Bin1 Bin2
Hex3 Hex4
oct5 oct6

Or paste:

$ paste -d' ' - - < file
Bin1 Bin2
Hex3 Hex4
oct5 oct6

Upvotes: 1

H&#229;kon H&#230;gland
H&#229;kon H&#230;gland

Reputation: 40718

You can try:

awk 'NR%2{printf "%s%s",$0,OFS}NR%2==0' file

Output:

Bin1 Bin2
Hex3 Hex4 
oct5 oct6

Upvotes: 1

Related Questions