Merge matching rows


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merge matching rows
# 1  
Old 12-09-2013
Merge matching rows

Hello,

I need this output. thank you very much.

input:

Code:

Code:
***table***wood
***snack***top
***table***garfield
***big***zen
***table***cars

output:

Code:

Code:
***table***wood2345garfield2345cars
***snack***top
***big***zen

# 2  
Old 12-09-2013
Quote:
Originally Posted by tara123
I need this output. thank you very much.
Ok, so you need this output.

But how you derived this output from input? Explain the logic used here and also show us what you tried so far?
# 3  
Old 12-09-2013
2345 is a fixed number. can be any number.

input:

Code:
***table***wood
***table***garfield
***table**cars
***snack***top
***big***zen

output:

Code:
***table***wood2345garfield2345cars
***snack***top
***big***zen

# 4  
Old 12-09-2013
Code:
awk -F'*' -v n="2345" '
        {
                A[$4] = A[$4] ? A[$4] n $NF : "***" $4 "***" $NF
        }
        END {
                for ( k in A )
                        print A[k]
        }
' file

# 5  
Old 12-09-2013
Thank you but output:

Code:
table***wood
2345garfield
2345cars
snack***top
big***zen

# 6  
Old 12-09-2013
Code:
$ cat file
***table***wood
***snack***top
***table***garfield
***big***zen
***table***cars

Code:
$ awk  '{gsub(/\*/," ");A[$1] = A[$1] ? A[$1] "2345" $2 : $2}END{for(i in A)print "***"i"***"A[i]}' file
***table***wood2345garfield2345cars
***snack***top
***big***zen

# 7  
Old 12-09-2013
Thanks it is works Smilie



Quote:
Originally Posted by Akshay Hegde
Code:
$ cat file
***table***wood
***snack***top
***table***garfield
***big***zen
***table***cars

Code:
$ awk  '{gsub(/\*/," ");A[$1] = A[$1] ? A[$1] "2345" $2 : $2}END{for(i in A)print "***"i"***"A[i]}' file
***table***wood2345garfield2345cars
***snack***top
***big***zen

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk merge matching columns

I know I'm not the first one asking this but my code still does not work: File 1: gi|1283| tRNAscan exon 87020 88058 . - . transcript_id "Parent=tRNA-Tyr5.r01"; gi|3283| tRNAscan exon 97020 97058 . + . transcript_id "Parent=tRNA-Tyr6.r01"; gi|4283| rRNAscan exon 197020 197058 . - . transcript_id... (5 Replies)
Discussion started by: 0sMoses
5 Replies

2. UNIX for Dummies Questions & Answers

Merge rows into one if first 2 columns match

Hi, I wanted to merge the content and below is input and required output info. Input: /hello,a,r /hello,a,L /hello,a,X /hi,b,v /hi,b,c O/p: /hello,a,r:L:X /hi,v,:v:c Use code tags, thanks. (6 Replies)
Discussion started by: ankitas
6 Replies

3. Shell Programming and Scripting

Merge files and remove duplicated rows

In a folder I'll several times daily receive new files that I want to combine into one big file, without any duplicate rows. The file name in the folder will look like e.q: MissingData_2014-08-25_09-30-18.txt MissingData_2014-08-25_09-30-14.txt MissingData_2014-08-26_09-30-12.txt The content... (9 Replies)
Discussion started by: Bergans
9 Replies

4. Shell Programming and Scripting

Merge two files matching columns

Hi! I need to merge two files when col1 (x:x:x) matching and adds second column from file1.txt. # cat 1.txt aaa;a12 bbb;b13 ccc;c33 ddd;d55 eee;e11 # cat 2.txt bbb;b55;34444;d55 aaa;a15;35666;a44 I try with this awk and I get succesfully first column from 1.txt: # awk -F";"... (2 Replies)
Discussion started by: fhluque
2 Replies

5. UNIX for Dummies Questions & Answers

Merge rows in bid data file

Dear all, Please help me ,,,, if I have input file like this A_AA960715 leucine-rich repeat-containing protein GO:0006952 defense response P A_AA960715 leucine-rich repeat-containing protein GO:0008152 metabolic process P A_AA960715 leucine-rich... (5 Replies)
Discussion started by: AAWT
5 Replies

6. UNIX for Dummies Questions & Answers

Merge rows with common column

Dear all I have big file with two columns A_AA960715 GO:0006952 A_AA960715 GO:0008152 A_AA960715 GO:0016491 A_AA960715 GO:0007165 A_AA960715 GO:0005618 A_AA960716 GO:0006952 A_AA960716 GO:0005618 A_AA960716... (15 Replies)
Discussion started by: AAWT
15 Replies

7. Shell Programming and Scripting

merge similar rows

I have a large file (10M lines) that contains two columns: a frequency and a string, ex: 3 aaaaa 4 bbbbb 2 ccccc 5 aaaaa 1 ddddd 4 ccccc I need to merge the lines whose string part is the same, while updating the frequency. The output should look like this: 8 aaaaa 4 bbbbb 5 ccccc... (2 Replies)
Discussion started by: tootles564
2 Replies

8. Shell Programming and Scripting

Merge two rows using awk or python

Hi, Suppose I have a space delimited file like this: Serial# 1970 1971 1972 1973 1974 193532 21 2 X X X 200201 20 30 X X 40 200201 X X 13 15 X 393666 66 3 X X 5 ... (2 Replies)
Discussion started by: grossgermany
2 Replies

9. Shell Programming and Scripting

How to merge rows into columns ????

Hi guz I want to merge multiple rows into a multiple columns based on the first column. The file has symbol // I want to break the symbool // and I nedd exactlynew column at that point the output will be like this please guyz help in this isssue!!!!! merging rows into columns ... (4 Replies)
Discussion started by: bogu0001
4 Replies

10. Shell Programming and Scripting

merge rows based on a common column

Hi guys, Please guide me if you have a solution to this problem. I have tried paste -s but it's not giving the desired output. I have a file with the following content- A123 box1 B345 bat2 C431 my_id A123 service C431 box1 A123 my_id I need two different outputs- OUTPUT1 A123... (6 Replies)
Discussion started by: smriti_shridhar
6 Replies
Login or Register to Ask a Question