Group by column and concatenate


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Group by column and concatenate
# 1  
Old 07-24-2013
Group by column and concatenate

Hi,
Can you please help in resolving below issue:

I have input file as below.
Code:
COL1 COL2
ABC    111asdf
ABC    222dfgh
ABC    333fghy
ABC    4rtyu
XYZ    1yuio
XYZ    2ytre

Lookg for the Output File as below

Code:
COL1     COL2
ABC    '111asdf','222dfgh','333fghy','4rtyu'
XYZ     '1yuio','2ytre'


Last edited by Scrutinizer; 07-24-2013 at 03:11 PM.. Reason: code tags
# 2  
Old 07-24-2013
Code:
awk 'NR==1 { print ; next }
{ A[$1]=A[$1]","$2 }
END { for(X in A) print X,substr(A[X],2) }' inputfile

# 3  
Old 07-24-2013
Java

This code not working as expected. Please Suggest me another approach
# 4  
Old 07-24-2013
If you don't tell me what way it's misbehaving, I'm liable to repeat the mistake.
# 5  
Old 07-24-2013
Corona688's approach should work.

By the way I made minor modifications to produce requested output:
Code:
awk '
        NR == 1 {
                print $1 "\t" $2
        }
        NR > 1 {
                A[$1] = A[$1] ? A[$1] OFS "\x27" $2 "\x27" : "\x27" $2 "\x27"
        }
        END {
                for ( k in A )
                        print k "\t" A[k]
        }
' OFS=, file

Output:
Code:
COL1    COL2
ABC     '111asdf','222dfgh','333fghy','4rtyu'
XYZ     '1yuio','2ytre'

# 6  
Old 07-24-2013
Try:
Code:
awk '
  NR>1 {
    $2=q $2 q
  }
  $1!=p {
    if(NR>1) print s
    s=$1 "\t" $2
    p=$1
    next
  }
  {
    s=s "," $2
  }
  END{
    print s
  }
' q=\' file


Last edited by Scrutinizer; 07-24-2013 at 03:41 PM..
# 7  
Old 07-24-2013
try also:
Code:
awk '
NR==1 { printf("%s\t%s", $1, $2) ; next }
{ff=""; of=",%s"; if (!a[$1]++) { ff=$1; of="\n%s\t"; } printf ( of "\x27%s\x27", ff, $2); }
END {print ""}' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Group/concatenate certain column and basis on this do addition on other column

Hi Experts, Need your support I want to group/concatenate column 1,2,12 and 13 and if found duplicate then need to sum value of column 17,20,21 and column22. After concatenation if found unique then no action to be taken. Secondly want to make duplicate rows basis on grouping/concatenation of... (1 Reply)
Discussion started by: as7951
1 Replies

2. Shell Programming and Scripting

awk script concatenate two column and perform mutiplication

Need your help in solving this puzzle. Any kind of help will be appreciated and link for any documents to read and learn and to deal with such scenarios would be helpful Concatenate column1 and column2 of file 1. Then check for the concatenated value in Column1 of File2. If found extract the... (14 Replies)
Discussion started by: as7951
14 Replies

3. UNIX for Beginners Questions & Answers

Concatenate column values when header is Matching from multiple files

there can be n number of columns but the number of columns and header name will remain same in all 3 files. Files are tab Delimited. a.txt Name 9/1 9/2 X 1 7 y 2 8 z 3 9 a 4 10 b 5 11 c 6 12 b.xt Name 9/1 9/2 X 13 19 y 14 20 z 15 21 a 16 22 b 17 23 c 18 24 c.txt Name 9/1 9/2... (14 Replies)
Discussion started by: Nina2910
14 Replies

4. Shell Programming and Scripting

Concatenate values in the first column based on the second column.

I have a file (myfile.txt) with contents like this: 1.txt apple is 3.txt apple is 5.txt apple is 2.txt apple is a 7.txt apple is a 8.txt apple is a fruit 4.txt orange not a fruit 6.txt zero isThe above file is already sorted using this command: sort -k2 myfile.txtMy objective is to get... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

5. Shell Programming and Scripting

Group by a column and first 2 characters of another

Hello all, I want to consolidate my data group by cities into 4 rolled up code categories (first 2 characters of 2nd col), namely PR, GR, TY and others. CHICAGO PR1 CHICAGO GR3 CHICAGO GR4 CHICAGO HT5 CHICAGO HT6 CHICAGO TY5 HOUSTON PR3 HOUSTON PR9 HOUSTON TY4 HOUSTON HJ5... (1 Reply)
Discussion started by: jalaj841
1 Replies

6. Shell Programming and Scripting

Group by column from CSV file

Hello Friends, I need a shell script to extract a column in a csv file, group by the column, take the count and print the group with count more than one. Thanks (1 Reply)
Discussion started by: raka_rjit
1 Replies

7. UNIX for Dummies Questions & Answers

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... (4 Replies)
Discussion started by: Gussifinknottle
4 Replies

8. Shell Programming and Scripting

Merge group numbers and add a column containing group names

Hi All I do have a file like this with 6 columns. Groups of data merge together and the group number is indicated above each group. 1 1 12 26 289 3.2e-027 GCGTATGGCGGC 2 12 26 215 6.7e+006 TTCCACCTTTTG 3 9 26 175 ... (1 Reply)
Discussion started by: Lucky Ali
1 Replies

9. Shell Programming and Scripting

Merge group numbers and add a column containing group names

I have a file in the following format. Groups of data merge together and the group number is indicated above each group. 1 adrf dfgr dfg 2 dfgr dfgr 3 dfef dfr fd 4 fgrt fgr fgg 5 fgrt fgr (3 Replies)
Discussion started by: Lucky Ali
3 Replies

10. Shell Programming and Scripting

Concatenate two files after a specific column

Hi I have two files, one is 1.6 GB. I would like to add one extra column of information to the large file at a specific location. After its 2nd column. For example: File 1 has two columns more than 1000 rows like this MM009987 1 File 2 looks like this MM00098 MM00076 3 4 2 4 2 1... (1 Reply)
Discussion started by: sogi
1 Replies
Login or Register to Ask a Question