Concatenate column data, grouped by row info


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Concatenate column data, grouped by row info
# 1  
Old 09-21-2010
Question Concatenate column data, grouped by row info

Hello,

I have data which looks like:

1 2 3

a x 0 0
a 0 p 0
a 0 0 0
b 0 b c
b a 0 0
b 0 0 0
c q 0 s
c 0 r 0


I would like to concatenate each of the column data, grouped by the row values, i.e. my output should look like this:
1 2 3
a x00 0p0 000
b 0a0 b00 c00
c q0 0r s0

~G
# 2  
Old 09-21-2010
I made the assumption that you have 3 columns of data... but here goes...

Code:
awk '
{
        if (NR == 1) { print;next; }
        if (val1 != "" && $1 != lastkey) {
                print lastkey " " val1 " " val2 " " val3;
                val1="";
                val2="";
                val3=""
        }

        lastkey=$1;
        val1=val1 $2;
        val2=val2 $3;
        val3=val3 $4;
}
END {
        if (val1 != "") {
                print lastkey " " val1 " " val2 " " val3;
        }
}'

You can put that into a script and redirect input with your data.
# 3  
Old 09-21-2010
Code:
$ ruby -ane 'BEGIN{h={};h.default=""};$.==1and s=$_;$.>=2 and h[$F[0]]+=$F[1..-1].join;END{puts s;h.each{|x,y|print "#{x} #{y.scan(/.../).join(" ")}\n" }}' file
1 2 3
a x00 0p0 000
b 0bc a00 000
c q0s 0r0

# 4  
Old 09-21-2010
Code:
awk 'NR==1{print;next} {a[$1]=a[$1]$2;b[$1]=b[$1]$3;c[$1]=c[$1]$4}END{for (i in a) print i,a[i],b[i],c[i]}' urfile


Last edited by rdcwayx; 09-21-2010 at 11:21 PM..
# 5  
Old 09-22-2010
Quote:
Originally Posted by rdcwayx
Code:
awk 'NR==1{print;next} {a[$1]=a[$1]$2;b[$1]=b[$1]$3;c[$1]=c[$1]$4}END{for (i in a) print i,a[i],b[i],c[i]}' urfile

Wonderful!! could you pls explain us the logic behind this, especially the associative array part ??
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with merge row if share same column info

Input file: 32568 SSO7483 32568 SSO7486 117231 SSO1293 117231 SSO1772 178081 SSO3076 178081 SSO3077 222417 porA-2 222417 porB-2 263778 SSO1245 263778 SSO0509 . . Desired output: 32568 SSO7483,SSO7486 117231 ... (3 Replies)
Discussion started by: perl_beginner
3 Replies

2. Shell Programming and Scripting

Column to Row Data.

HI Guys, I have below Input :- 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:- ID L1 L2 L3 X 5 10 15 Y 20 12 15 Z 100 Null 300 (11 Replies)
Discussion started by: pareshkp
11 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

How to concatenate a string in every row data of a file??

Please look into the example. My source file is like, 00,57,3,2008-07-24 06:30:06 10,1,8025171,"1M00",17907023,2008-07-23 18:16:58 10,2,8025171,"1M00",17907023,2008-07-23 18:17:01 99,184 What should i do if i want output like... hello,00,57,3,2008-07-24 06:30:06... (7 Replies)
Discussion started by: pkumar3
7 Replies
Login or Register to Ask a Question