I've been trying to figure this out for a while but I'm completely stumped. I have files with data in rows and I need to convert the data to columns. Each record contains four rows with a "field name: value" pair. I would like to convert it to four columns with the field names as column headers and the data from each row in the record below the corresponding column header.
For example:
Original data:
First Name: Gary
Last Name: Larson
DOB: 01/01/2010
POB: US
First Name: Bill
Last Name: Waterson
DOB: 01/01/2009
POB: US
First Name: Scott
Last Name: Adams
DOB: 01/01/2007
POB: US
Convert it to:
First Name Last Name DOB POB
Gary Larson 01/01/2010 US
Bill Waterson 01/01/2009 US
Scott Adams 01/01/2007 US
I would like to know how to do this in awk or sed. Thanks in advance for the help.
$
$ cat f3
First Name: Gary
Last Name: Larson
DOB: 01/01/2010
POB: US
First Name: Bill
Last Name: Waterson
DOB: 01/01/2009
POB: US
First Name: Scott
Last Name: Adams
DOB: 01/01/2007
POB: US
$
$
$ perl -ne 'BEGIN {print "First Name Last Name DOB POB\n"}
/^(First Name:|Last Name:|DOB:|POB:) (.*)$/ && do{print $2," "; $i++}; $i==4 && do{print "\n"; $i=0}' f3
First Name Last Name DOB POB
Gary Larson 01/01/2010 US
Bill Waterson 01/01/2009 US
Scott Adams 01/01/2007 US
$
$
tyler_durden
Or maybe you want formatted print:
$
$ perl -ne 'BEGIN {printf("%-12s %-12s %-12s %-12s\n","First Name","Last Name","DOB","POB")}
/^(First Name:|Last Name:|DOB:|POB:) (.*)$/ && do{printf("%-12s ",$2); $i++}; $i==4 && do{print "\n"; $i=0}' f3
First Name Last Name DOB POB
Gary Larson 01/01/2010 US
Bill Waterson 01/01/2009 US
Scott Adams 01/01/2007 US
$
$