Help with merge row if share same column info


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with merge row if share same column info
# 1  
Old 01-21-2016
Help with merge row if share same column info

Input file:
Code:
32568        SSO7483
32568        SSO7486
117231       SSO1293
117231       SSO1772
178081       SSO3076
178081       SSO3077
222417       porA-2
222417       porB-2
263778       SSO1245
263778       SSO0509
.
.

Desired output:
Code:
32568        SSO7483,SSO7486
117231       SSO1293,SSO1772
178081       SSO3076,SSO3077
222417       porA-2,porB-2
263778       SSO1245,SSO0509
.
.

I wanna merge the column 2 info if both row share same column 1 info.
I did try this code,
Code:
awk '{if (a[$1])a[$1]=a[$1]","$2;else a[$1]=$2;}END{for (i in a)print i, a[i];}' Input_File
263778 SSO1245,SSO0509
117231 SSO1293,SSO1772
32568 SSO7483,SSO7486
222417 porA-2,porB-2
178081 SSO3076,SSO3077
.
.

It worked as what I want but it will change the orientation of column 1 which make me confuse Smilie

Thanks for any advice.
# 2  
Old 01-21-2016
Hello perl_beginner,

If you are not bothered about the output's sequence(Means it may differ from the Input_file input sequence), then following may help you.
Code:
awk '{A[$1]=A[$1]?A[$1] OFS $NF:$NF} END{for(i in A){print i "\t" A[i]}}' OFS=,   Input_file

Output will be as follows.
Code:
263778  SSO1245,SSO0509
117231  SSO1293,SSO1772
32568   SSO7483,SSO7486
222417  porA-2,porB-2
178081  SSO3076,SSO3077

If you need output to be in same order as in Input_file, then following may help you in same.
Code:
awk 'FNR==NR{A[$1]=A[$1]?A[$1] OFS $NF:$NF;next} ($1 in A){print $1 "\t" A[$1];delete A[$1]}' OFS=,  Input_file  Input_file

Output will be as follows.
Code:
32568   SSO7483,SSO7486
117231  SSO1293,SSO1772
178081  SSO3076,SSO3077
222417  porA-2,porB-2
263778  SSO1245,SSO0509

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 01-21-2016
Try also
Code:
awk '   
$1 != LAST      {if (NR != 1) printf "%s", RS
                 printf "%s", $0
                 LAST = $1
                 next
                }
                {printf ",%s", $2
                }
END             {printf "%s", RS
                }
' file
32568        SSO7483,SSO7486
117231       SSO1293,SSO1772
178081       SSO3076,SSO3077
222417       porA-2,porB-2
263778       SSO1245,SSO0509

This User Gave Thanks to RudiC For This Post:
# 4  
Old 01-21-2016
Thanks again, Singh Smilie
It worked perfect ^.^

---------- Post updated at 10:18 AM ---------- Previous update was at 10:18 AM ----------

Thanks, RudiC.
Again you help me a lot Smilie

Really many thanks to you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print first row of column a, last row of column b if column a has the same value

I have a table with this structure: cola colb colc 1 19 lemon 20 31 lemon 32 100 lemon 159 205 cherries 210 500 cherries and need to parse it into this format: cola colb colc 1 100 lemon 159 500 cherries So I need the first row of cola and the last row of colb if colc has the... (3 Replies)
Discussion started by: coppuca
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

Print row on 4th column to all row

Dear All, I have input : SEG901 5173 9005 5740 SEG902 5227 5284 SEG903 5284 5346 SEG904 5346 9010 SEG905 5400 5456 SEG906 5456 5511 SEG907 5511 9011 SEG908 5572 9015 SEG909 5622 9020 SEG910 5678 5739 SEG911 5739 5796 SEG912 5796 9025 ... (3 Replies)
Discussion started by: attila
3 Replies

4. UNIX for Dummies Questions & Answers

awk to print first row with forth column and last row with fifth column in each file

file with this content awk 'NR==1 {print $4} && NR==2 {print $5}' file The error is shown with syntax error; what can be done (4 Replies)
Discussion started by: cdfd123
4 Replies

5. Shell Programming and Scripting

How to merge multiple rows into single row if first column matches ?

Hi, Can anyone suggest quick way to get desired output? Sample input file content: A 12 9 A -0.3 2.3 B 1.0 -4 C 34 1000 C -111 900 C 99 0.09 Output required: A 12 9 -0.3 2.3 B 1.0 -4 C 34 1000 -111 900 99 0.09 Thanks (3 Replies)
Discussion started by: cbm_000
3 Replies

6. Shell Programming and Scripting

Subtracting each row from the first row in a single column file using awk

Hi Friends, I have a single column data like below. 1 2 3 4 5 I need the output like below. 0 1 2 3 4 where each row (including first row) subtracting from first row and the result should print below like the way shown in output file. Thanks Sid (11 Replies)
Discussion started by: ks_reddy
11 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 files with same row values

Hi everyone, I'm just wondering how could I using awk language merge two files by comparison of one their row. I mean, I have one file like this: file#1: 21/07/2009 11:45:00 100.0000000 27.2727280 21/07/2009 11:50:00 75.9856644 25.2492676 21/07/2009 11:55:00 51.9713287 23.2258072... (4 Replies)
Discussion started by: tonet
4 Replies

9. 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

10. Shell Programming and Scripting

Changing the column for a row in a text file and adding another row

Hi, I want to write a shell script which increments a particular column in a row from a text file and then adds another row below the current row with the incremented value . For Eg . if the input file has a row : abc xyz lmn 89 lm nk o p I would like the script to create something like... (9 Replies)
Discussion started by: aYankeeFan
9 Replies
Login or Register to Ask a Question