4

How can I set a row as column in Unix. Example:

name : Keshav
sub  : C++
branch: cse
DOB  :22/09/1990
company:NONE

name : Vijay
sub  : Java
branch: cse
DOB  :22/09/1985
company:NONE

I want output like this:

name     sub   branch  DOB           company
Keshav   C++   cse     22/09/1990    NONE
Vijay    Java  cse     22/09/1990    NONE
jimmij
  • 47,140

2 Answers2

3

Another approach:

awk 'BEGIN{FS=":|\n";RS=""}NR==1{print $1,$3,$5,$7,$9}{print $2,$4,$6,$8,$10}'
cuonglm
  • 153,898
jimmij
  • 47,140
1

You may want to put the header manually and run the following awk on the file:

awk -F: '{count++; printf("%s ",$2); if (count>5){count=0; printf("\n");}}' x.txt >> res.txt

Assuming x.txt contains the inputs as you have and res.txt is a prepared file with headers as follows:

name sub branch DOB company

The resulting output will be:

name  sub  branch  DOB  company
 Keshav  C++  cse 22/09/1990 NONE
 Vijay  Java  cse 22/09/1985 NONE
Ketan
  • 9,226