Column to Row Data.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Column to Row Data.
# 1  
Old 11-06-2015
Column to Row Data.

HI Guys,

I have below Input :-

Code:
X	L1	5
Y	L1	10
Z	L1	15
X	L2	20
Y	L2	12
Z	L2	15
X	L3	100
Y	L3	
Z	L3	300

Output:-

Code:
ID	L1	L2	L3
X	5	10	15
Y	20	12	15
Z	100	Null	300


I have used Below awk command but it massing my header.

Code:
nawk -v w=10 '{a[$1]=($1 in a)?a[$1] OFS sprintf("%-*s",w,$3):sprintf("%-*s",w,$3);h[$2]}END{for(i in h) printf("\t%-*s", w,i);print "";for(i in a) print i,a[i]}' OFS='\t'

# 2  
Old 11-06-2015
Your output does NOT reflect the input data; e.g. Z / L1 should be 15, not 100. Try
Code:
awk     '       {LN[$1]; HD[$2]; MX[$1,$2]=$3}
         END    {               printf "%10s", "ID"; for (i in HD) printf "%10s", i; print "";
                 for (j in LN) {printf "%10s",j;     for (i in HD) printf "%10s", MX[j,i]?MX[j,i]:"NULL"; print ""}
                }
        ' file
        ID        L1        L2        L3
         X         5        20       100
         Y        10        12      NULL
         Z        15        15       300

This User Gave Thanks to RudiC For This Post:
# 3  
Old 11-06-2015
Thanks a lot for Code

Yes You right and one more this ....

It Replace 0 to Null....I want to replace on "" to Null"

Can you try with This Input :



Code:
X	L1	5
Y	L1	10
Z	L1	15
X	L2	20
Y	L2	
Z	L2	15
X	L3	0
Y	L3	0
Z	L3	300

# 4  
Old 11-06-2015
Can't you do it on your own?
This User Gave Thanks to RudiC For This Post:
# 5  
Old 11-06-2015
Thanks Buddy ....I will try from my End.
# 6  
Old 11-06-2015
Just a hint - look at the MX[j,i]?MX[j,i]:"NULL" (conditional assinment) part; it now checks the array value if 0 or empty, make it check if empty only.
# 7  
Old 11-06-2015
I tried But No Luck Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to get row data printed in column using awk?

Hi team, I have below sample file. $ cat sample dn: MSISDN=400512345677,dc=msisdn,ou=NPSD,serv=CSPS,ou=servCommonData,dc=stc structuralObjectClass: NphData objectClass: NphData objectClass: MSISDN entryDS: 0 nodeId: 35 createTimestamp: 20170216121047Z modifyTimestamp: 20170216121047Z... (3 Replies)
Discussion started by: shanul karim
3 Replies

2. Shell Programming and Scripting

Help with merge data at same column but different row inquiry

Hi, Anyone did experience to merge data at same column but different row previously by using awk, sed, perl, etc? Input File: SSO12256 SSO0001 thiD-1 rbsK-1 SSO0006 SSO0007 SSO0008 SSO0009 SSO0010 SSO0011 Desired Output File: (5 Replies)
Discussion started by: perl_beginner
5 Replies

3. Shell Programming and Scripting

Replace First Column and First Row Data

HI Guys, I just want to replace data for First Column and Row Cell(1,1) Input :- Hello A B C X 1 2 3 Y 4 5 6 Z 7 8 9 Output:- Byee A B C X 1 2 3 Y 4 5 6 Z 7 8 9 From Hello to Byee .....And The Each file have Different String. (3 Replies)
Discussion started by: pareshkp
3 Replies

4. Shell Programming and Scripting

Convert Data from Column to Row

Hi FileA.txt E_TIM 16, ETE 15, EOND 26, EEC 81, E_1 un, E_2 un, E_3 un, E_4 284, E_TIM 17, ETE 15, EOND 29, EEC 82, E_1 un, E_2 un, E_3 un, E_4 249, (6 Replies)
Discussion started by: asavaliya
6 Replies

5. UNIX for Advanced & Expert Users

Convert column data to row data using shell script

Hi, I want to convert a 3-column data to 3-row data using shell script. Any suggestion in this regard is highly appreciated. Thanks. (4 Replies)
Discussion started by: sktkpl
4 Replies

6. Shell Programming and Scripting

Sort data from column to row

Hi, I need somebody's help with sorting data with awk. I've got a file: 10 aaa 4584 12 bbb 6138 20 ccc 4417 21 ddd 7796 10 eee 7484 12 fff ... (5 Replies)
Discussion started by: killerbee
5 Replies

7. Shell Programming and Scripting

Moving data from a specified column/row to another column/row

Hello, I have an input file like the following: 11_3_4 2_1_35 3_15__ _16989 Where '_' is a space. The data is in a table. Is there a way for the program to prompt the user for x1,y1 and x2,y2, where x1,y1 is the desired number (for example x=6 y=4 is a value of 4) and move to a desired spot... (2 Replies)
Discussion started by: jl487
2 Replies

8. Shell Programming and Scripting

row to column and position data in to fixed column width

Dear friends, Below is my program and current output. I wish to have 3 or 4 column output in order to accomodate in single page. i do have subsequent command to process after user enter the number. Program COUNT=1 for MYDIR in `ls /` do VOBS=${MYDIR} echo "${COUNT}. ${MYDIR}" ... (4 Replies)
Discussion started by: baluchen
4 Replies

9. Shell Programming and Scripting

Convert row data to column data

Hi Guys, I have a file as follows: a 1 b 786 c 90709 d 99 a 9875 b 989 c 887 d 111 I want: a 1 9875 b 786 989 (3 Replies)
Discussion started by: npatwardhan
3 Replies

10. Shell Programming and Scripting

Move data from Column to Row

Hi all, I have a file with several columns of data, eg: A B C D 1 2 5 1 2 2 2 2 8 4 4 4 4 2 3 4 10 9 4 4 9 7 1 2 I need to get the values from, say, column B and place them into a string separated by a semicolon, eg: 2;2;4;2;9;7 Does... (4 Replies)
Discussion started by: danhodges99
4 Replies
Login or Register to Ask a Question