Transpose data as rows using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Transpose data as rows using awk
# 1  
Old 02-11-2014
Transpose data as rows using awk

Hi I have below requirement, need help

One file contains the meta data information and other file would have the data, match the column from file1 and with file2 and extract corresponding column value and display in another file

File1:
Code:
CUSTTYPECD
COSTCENTER
FNAME
LNAME
SERVICELVL

File2:
Code:
CUSTTYPECD:B|FNAME:JOHN|LNAME:PETER
CUSTTYPECD:B|COSTCENTER:11014|FNAME:RAJ|LNAME:KUMAR|SERVICELVL:1501
CUSTTYPECD:A|FNAME:ANDREW|LNAME:BOB|SERVICELVL:1501

Output:
Code:
CUSTTYPECD|COSTCENTER|FNAME|LNAME|SERVICELVL
B|null|JOHN|PETER|null
B|11014|RAJ|KUMAR|1501
A|null|ANDREW|BOB|1501

Note: Second file has missing column values and that could repeat through the file any of the column and column value corresponding to that could be missed.

Any help is greatly appreciated..

Thanks

Last edited by Don Cragun; 02-11-2014 at 12:21 PM.. Reason: Add CODE tags.
# 2  
Old 02-11-2014
You could try something like:
Code:
awk -F '[:|]' '
FNR == NR {
        # Gather and print column headings from 1st input file:
        head[$1] = ++head_count
        printf("%s%s", FNR == 1 ? "" : "|", $1)
        next
}
FNR == 1 && NR != 1 {
        # We have read the 1st line in the 2nd input file...  Add terminating
        # newline to output heading.
        printf("\n")
}
{       for(i = 1; i <= head_count; i++)
                # Clear output fields...
                out[i] = "null"
        for(i = 1; i <= NF; i += 2)
                # Odd fields are column headings, even fields are data for the
                # corresponding output column.  Gather output data...
                out[head[$i]] = $(i + 1)
        for(i = 1; i <= head_count; i++)
                # Print the accumlated output for this linput line...
                printf("%s%s", out[i], i == head_count ? "\n" : "|")
}' File1 File2

If you want to run this on a Solaris/SunOS system, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of the default /usr/bin/awk.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to transpose pieces of data in a column to multiple rows?

Hello Everyone, I am very new to the world of regular expressions. I am trying to use grep/sed for the following: Input file is something like this and there are multiple such files: abc 1 2 3 4 5 ***END*** abc 6 7 8 9 ***END*** abc 10 (2 Replies)
Discussion started by: shellnewuser
2 Replies

2. Shell Programming and Scripting

Transpose comma delimited data in rows to columns

Hello, I have a bilingual database with the following structure a,b,c=d,e,f The right half is in a Left to right script and the second is in a Right to left script as the examples below show What I need is to separate out the database such that the first word on the left hand matches the first... (4 Replies)
Discussion started by: gimley
4 Replies

3. Shell Programming and Scripting

awk to transpose every 7 rows into columns

input: a1 a2 a3 a4 a5 a6 a7 b1 b2 b3 .. b7 .. z1 .. z7 (12 Replies)
Discussion started by: ux4me
12 Replies

4. Shell Programming and Scripting

Transpose Column of Data to Rows

I can no longer find my commands, but I use to be able to transpose data with common fields from a single column to rows using a command line. My data is separated as follows: NAME=BOB ADDRESS=COLORADO PET=CAT NAME=SUSAN ADDRESS=TEXAS PET=BIRD NAME=TOM ADDRESS=UTAH PET=DOG I would... (7 Replies)
Discussion started by: docdave78
7 Replies

5. Shell Programming and Scripting

awk to transpose preceding row to 1st column of next rows

Gurus: How can I transpose the output below to a format in which I can plot a graph to show VSZ memory usage by PIDs? stdout: Tue Jan 22 07:29:19 CUT 2013 42336296 1841272 java wilyadm 21889232 438616 jlaunch sidadm 42532994 414336 jlaunch sidadm Tue Jan 22 07:49:20 CUT 2013... (1 Reply)
Discussion started by: ux4me
1 Replies

6. Shell Programming and Scripting

Transpose Data from Columns to rows

Hello. very new to shell scripting and would like to know if anyone could help me. I have data thats being pulled into a txt file and currently have to manually transpose the data which is taking a long time to do. here is what the data looks like. Server1 -- Date -- Other -- value... (7 Replies)
Discussion started by: Mikes88
7 Replies

7. Shell Programming and Scripting

awk transpose rows to column

Need to transpose in awk rows to column like this: input: A1,6,5,4 3,2,1, A2,8,7,9,10,11,12,13,14 A3,1,2,3,5,7,8,9 A4,9,4,8,1,5,3, output: A1,1 A1,2 A1,4 ... A2,7 A2,8 ... A3,1 A3,2 ... A4,1 A4,3 (5 Replies)
Discussion started by: sdf
5 Replies

8. Shell Programming and Scripting

Transpose columns to Rows : Big data

Hi, I did read a few posts on the subjects, tried out a few solutions, but did not solve my problem. https://www.unix.com/302121568-post11.html https://www.unix.com/shell-programming-scripting/137953-large-file-columns-into-rows-etc-4.html Please help. Problem very similar to the second link... (15 Replies)
Discussion started by: genehunter
15 Replies

9. Shell Programming and Scripting

How to transpose a table of data using awk

Hi. I have this data below:- v1 28 14 1.72414 1.72414 1.72414 1.72414 1.72414 v2 77 7 7.47126 6.89655 6.89655 6.89655 6.89655 v3 156 3 21.2644 21.2644 20.6897 21.2644 20.6897 v4 39 3 1.72414 1.72414 1.72414 1.72414 1.72414 v5 155 1 21.2644 23.5632 24.1379 23.5632 24.1379 v6 62 2 2.87356... (2 Replies)
Discussion started by: ahjiefreak
2 Replies

10. Shell Programming and Scripting

How to transpose data elements in awk

Hi, I have an input data file :- Test4599,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,2,2,Rain Test90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,0,Not Rain etc.... I wanted to transpose these data to:-... (2 Replies)
Discussion started by: ahjiefreak
2 Replies
Login or Register to Ask a Question