Help converting row data to columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help converting row data to columns
# 1  
Old 09-08-2010
Help converting row data to columns

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.
# 2  
Old 09-08-2010
Perl:
Code:
 perl -F':' -alne 'BEGIN{print "First Name Last Name DOB POB"};if ($. % 4){$s.=$F[1]}else{$s=~s/^ //;print $s.$F[1];$s=""}' file

AWK:
Code:
awk -F": " -vORS="" 'BEGIN{print "First Name Last Name DOB POB\n"}NR%4{print $2" ";next}{print "\n"}' file

# 3  
Old 09-08-2010
Code:
awk -F": " 'BEGIN{print "First Name Last Name DOB POB"}{printf $2" "}!(NR%4){print ""}' infile

# 4  
Old 09-08-2010
bash way

Code:
echo -e "First Name\tLast Name\tDOB\tPOB"
cut -d':' -f2 file | paste - - - -

# 5  
Old 09-08-2010
Code:
awk -F":" '{print $2}' infile |xargs -n4

# 6  
Old 09-09-2010
Another Perl:

Code:
$ 
$ 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:

Code:
$ 
$ 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           
$ 
$ 

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Converting data from specific columns

i have a file (csv or txt or anything which has 4 columns (id,name,number,location) and it contains data. i want to convert the data of specific columns like name to ooooo and number to 88888 matching the field length of that columns. for example if name column has anthony which is 7, it should... (2 Replies)
Discussion started by: prajaktaraut
2 Replies

2. Emergency UNIX and Linux Support

[Solved] Mysql - Take data from row and copy it to another row

Sorry if I repost my question in this section, but I'm really in a hurry since I have to finish my work... :( Dear community, I have a table with two rows like: Row1 Row2 ======= ======= 7,3 text 1 1,3 text 2 1,2,3 blabla What i need to do is add/copy... (2 Replies)
Discussion started by: Lord Spectre
2 Replies

3. UNIX for Dummies Questions & Answers

Select 2 columns and transpose row by row

Hi, I have a tab-delimited file as follows: 1 1 2 2 3 3 4 4 a a b b c c d d 5 5 6 6 7 7 8 8 e e f f g g h h 9 9 10 10 11 11 12 12 i i j j k k l l 13 13 14 14 15 15 16 16 m m n n o o p p The output I need is: 1 1 a a 5 5 e e 9 9 i i 13... (5 Replies)
Discussion started by: mvaishnav
5 Replies

4. Shell Programming and Scripting

converting column to row

Hi everyone.. I have a list of values in a file... 1.2345e-1 2.282828e+ 3.2341e-1 1.1223445e-1 I am interested in converting this column to a row.. 1.2345e-1 2.282828e+ 3.2341e-1 1.1223445e-1 can anyone pls help?? I am a liunx newbie.. Thanks.. (7 Replies)
Discussion started by: kjha
7 Replies

5. Shell Programming and Scripting

Format row data into columns

I have a file which looks like this: /* ----------------- EDW$MOC139_R_NNA_BR_SUM_FACT2 ----------------- */ insert_job: EDW$MOC139_R_NNA_BR_SUM_FACT2 job_type: c command: /home/btchproc/load_process/batch_files/batch_nna_brn_split_sum_fact2.sh m machine: edwprod02.dsm.pwj.com #owner:... (29 Replies)
Discussion started by: Gangadhar Reddy
29 Replies

6. Shell Programming and Scripting

Splitting data from one row as multiple columns

Hi I have a file containing some data as follows: 11-17-2010:13:26 64 4 516414 1392258 11-17-2010:13:26 128 4 586868 695603 11-17-2010:13:26 256 4 474937 1642294 11-17-2010:13:32 64 4 378715 1357066 11-17-2010:13:32 128 4 597981 1684006 ... (17 Replies)
Discussion started by: annazpereira
17 Replies

7. Shell Programming and Scripting

How to convert 2 column data into multiple columns based on a keyword in a row??

Hi Friends I have the following input data in 2 columns. SNo 1 I1 Value I2 Value I3 Value SNo 2 I4 Value I5 Value I6 Value I7 Value SNo 3 I8 Value I9 Value ............... ................ SNo N (1 Reply)
Discussion started by: ks_reddy
1 Replies

8. Shell Programming and Scripting

How to insert data befor some field in a row of data depending up on values in row

Hi I need to do some thing like "find and insert before that " in a file which contains many records. This will be clear with the following example. The original data record should be some thing like this 60119827 RTMS_LOCATION_CDR INSTANT_POSITION_QUERY 1236574686123083rtmssrv7 ... (8 Replies)
Discussion started by: aemunathan
8 Replies

9. Shell Programming and Scripting

Converting tables of row data into columns of tables

I am trying to transpose tables listed in the format into format. Any help would be greatly appreciated. Input: test_data_1 1 2 90% 4 3 91% 5 4 90% 6 5 90% 9 6 90% test_data_2 3 5 92% 5 4 92% 7 3 93% 9 2 92% 1 1 92% ... Output:... (7 Replies)
Discussion started by: justthisguy
7 Replies
Login or Register to Ask a Question