Append last two field with the same id


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append last two field with the same id
# 1  
Old 11-26-2013
Append last two field with the same id

Hi everybody!! I am new on this forum and I really hope that you can help me!
My problem concern the editing of a file like this:
Code:
chr1    23254208        23274520        1,9 0,5        no      chr1    23252345        2722460        h3k27me3        -4,3
chr1    23254208        23274520        1,9 0,5        no      chr1    243656645 43692360        h3k9ac  0,66
chr1    23254208        23274520        1,9 0,5        no      chr1    23256645        23292360        h3k9me3 -5,54
chr1    23254578        23274768        16,1 1,2 yes chr1    23243645        23292360        h3k27me3        -4,33
chr1    23254578        23274768        16,1 1,2 yes      chr1    23256645        2334360        h3k4me3 -1,11

What I would like to obtain is a file like this (followed by explanation):
Code:
chr1    23254208        23274520        1,9 0,5        no      chr1    23252345        2722460        h3k27me3        -4,3        h3k9ac  0,66        h3k9me3 -5,54
chr1    23254578        23274768        16,1 1,2 yes chr1    23243645        23292360        h3k27me3        -4,33 h3k4me3 -1,11

I want to compare the first 3 fields of each row and, if they are identical, I want to print all the first row followed by the last two field of the same/followed row. I really hope my explanation is satisfying!!
Thank a lot

Giuliano

Last edited by Franklin52; 11-26-2013 at 11:06 AM.. Reason: Please use code tags
# 2  
Old 11-26-2013
Try this:
Code:
awk '{x=$1$2$3; a[x]=a[x]?a[x] FS $10 FS $11:$0} END {for(i in a) print a[i]}' file

This User Gave Thanks to Subbeh For This Post:
# 3  
Old 11-26-2013
Thanks Subbeh!
It works perfectly!
SmilieSmilieSmilie
# 4  
Old 11-26-2013
How about this ? with assumption file is sorted

Code:
$ awk '{printf p == $1$2$3 ? NR == 1 ? $0 : OFS $10 OFS $11 : RS $0 }{p = $1$2$3}END{printf RS}'  file

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Linux

Bash to append domain in search field of resolv.conf after vpnc connection in Linux

After vpn connection,I am not able to resolve any machines in remote gateway.It looks like remote domain is added to domain field instead of adding it to the Search field in /etc/resolv.conf I want the remote domain to add to search field along with local domain.Can anyone tell a bash or script... (2 Replies)
Discussion started by: jeremy_brett
2 Replies

2. Shell Programming and Scripting

UNIX append field with comparing fields from multiple column

I have a csv dump from sql server that needs to be converted so it can be feed to another program. I already sorted on field 1 but there are multiple columns with same field 1 where it needs to be compared against and if it is same then append field 5. i.e from ANG SJ,0,B,LC22,LC22(0) BAT... (2 Replies)
Discussion started by: nike27
2 Replies

3. Shell Programming and Scripting

Search for a particular field length and append '0' if less less than 10

Hi, I am new to Unix. Please help me in finding solution for the below scenario. I have a flat file that contains data like 378633410|3013505414|new_378633410|ALBERT|WALLS|378633410|Rew||||||| 351049045|239|new_351049045|JIM|COOK|351049045|Rew|||||||... (6 Replies)
Discussion started by: anandek
6 Replies

4. Shell Programming and Scripting

Compare a common field in two files and append a column from File 1 in File2

Hi Friends, I am new to Shell Scripting and need your help in the below situation. - I have two files (File 1 and File 2) and the contents of the files are mentioned below. - "Application handle" is the common field in both the files. (NOTE :- PLEASE REFER TO THE ATTACHMENT "Compare files... (2 Replies)
Discussion started by: Santoshbn
2 Replies

5. Shell Programming and Scripting

Append 1st field from a file into 2nd field of another file

Hi, I've internally searched through forums for about 2+ hours. Unfortunately, with no luck. Although I've found some cases close to mine below, but didn't help so much. Actually, I'm in short with time. So I had to post my case. Hoping that you can help. I have 2 files, FILE1 ... (1 Reply)
Discussion started by: amurib
1 Replies

6. Shell Programming and Scripting

How to append a character to the last but one field on a specific line?

Hi Guys, I have a file like this: aaa b c d e f fsss g h i k l qqq r t h n I want: aaa b c d e f fsss g h i k l qqq r t h , n ggg p t e d u qqq i o s , k (2 Replies)
Discussion started by: npatwardhan
2 Replies

7. Shell Programming and Scripting

awk append to one line if first field is the same

Hi all, How would I append the second field each time to one line if the first field is the same for example I have this data: 10430,187976 10430,251588 10430,262904 10430,275008 10430,279892 10430,275008 10430,303740 10430,318136 10430,336988 10430,350324 10430,373648 And I... (4 Replies)
Discussion started by: borderblaster
4 Replies

8. Shell Programming and Scripting

How to append two files with common field.

I have two files like File1 : will get this file from "who" command. It is a unix file. user val1 Jul 29 13:15 (IP Address1) user val3 Jul 30 03:21 (IP Address2) user val2 Jul 29 13:16 (IP Address3) user val4 Jul 29 13:17 (IP Address4) ... (4 Replies)
Discussion started by: manneni prakash
4 Replies

9. Shell Programming and Scripting

append field to file(nawk)

I have two files. File A and File B. File A has two fields separated by comma 352020252365988, 652020100572356 546876543215667, 652065465654686 ... File B has many Fields separate by spaces Date Name 352020252365988 Reference Date2 Name2 546876543215667 Reference I want to... (4 Replies)
Discussion started by: axl
4 Replies
Login or Register to Ask a Question