Print merged rows from two files by applying if condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print merged rows from two files by applying if condition
# 1  
Old 09-03-2011
Print merged rows from two files by applying if condition

Hi all,

I have list of two kind of files and I want to compare the rows and print the merged data by applying if condition.

First kind of file looks like:

Quote:
6 TRP 5 LEU 3.800394
6 TRP 7 GLN 3.837793
12 THR 11 VAL 3.794017
12 THR 13 ILE 3.787175
12 THR 14 LYS 7.014680
12 THR 15 ILE 10.132971
98 ASN 97 LEU 3.807972
98 ASN 99 PHE 3.811863
99 PHE 1 PRO 14.907668
and second kind of file looks like :

Quote:
6 TRP 5 LEU 3.800394
12 THR 13 ILE 3.787175
22 ALA 21 GLU 3.800398
41 ARG 40 GLY 3.790884
98 ASN 97 LEU 3.807972
60 ASP 59 TYR 3.807268
I want to print the rows present in second file followed by 3 more columns from first file by applying if condition

if (3rd column = 1st column - 1) #from second file
compare it in first file and
print 3rd, 4th and 5th column from the row having (column1 in frist file=
column1 in second file && 3rd column = 1st column +1 ) # in first file

and vive versa....
i.e, if (3rd column = 1st column + 1)
then print 3rd, 4th and 5th column from the row having (column1 in first file= column1 in second file && 3rd column = 1st column - 1 ) # in first file

therefore, the expected output must be like:
Quote:
6 TRP 5 LEU 3.800394 7 GLN 3.837793
12 THR 13 ILE 3.787175 14 LYS 7.014680
98 ASN 97 LEU 3.807972 99 PHE 3.811863
Thanks in advance !!!!!
# 2  
Old 09-03-2011
I think your output is incorrect... Based on your conditions this is what is produced:
Code:
6 TRP 5 LEU 3.800394  7 GLN 3.837793
12 THR 13 ILE 3.787175  11 VAL 3.794017
98 ASN 97 LEU 3.807972  99 PHE 3.811863

And here is the code:
Code:
awk 'NR==FNR&&$3==($1-1){a[$1]=$0;next};NR==FNR&&$3==($1+1){b[$1]=$0;next};$3==($1+1)&&a[$1]{$0=a[$1]" "$3" "$4" "$5;print};$3==($1-1)&&b[$1]{$0=b[$1]" "$3" "$4" "$5;print};' file2 file1

# 3  
Old 09-03-2011
Do I understand right:
1. The keys for files are combinations of the second and forth fields (they are unique for each file).
2. You need compare these fields.
3. And these fields are always 3-letter words.

===

Well, I think after the bartus11's solution these questions are no longer needed.

Last edited by yazu; 09-03-2011 at 09:40 AM..
# 4  
Old 09-03-2011
Yes Yazu....u r correct

@ Bartus: yes, the answer indicated by u is correct....but, m sorry to say that I am not getting any output from ur help code and also I want to undersatnd ur code...can u please explain it to me and assist me to get the correct output
# 5  
Old 09-03-2011
Can you post the sample data for which my code is not working?
# 6  
Old 09-03-2011
Hey Bartus......

M really very sorry for the above said....as ur code is working on other similar files....and the output is exactly similar to what I needed....Thanks a lot Smilie
my previous sample data file has some faults in column number....
# 7  
Old 09-06-2011
Hi,

One more thing...... if it doesn't follow the condition $3=($1 +1) or $3=($1-1) in file2, then it must print the row as such from file2.

Im stuck in this....plz help me Smilie

Last edited by CAch; 09-08-2011 at 12:45 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Applying the same awk over a directory of files with individual file output

I am trying to apply an awk action over multiple files in a directory. It is a simple action, I want to print out the 1st 2 columns (i.e. $1 and $2) in each tab-separated document and output the result in a new file *.pp This is the awk that I have come up with so far, which is not giving me a... (6 Replies)
Discussion started by: owwow14
6 Replies

2. Shell Programming and Scripting

Average across rows with a condition

Hi Friends, My input file Gene1 10 20 0 Gene2 5 0 15 Gene3 10 10 10 Gene4 5 0 0 If there is a zero for any gene in any column, I don't want that column to be considered which reduces the denominator value during average. Here is my output Gene1 10 20 0 10 Gene2 5 0 15 10 Gene3... (5 Replies)
Discussion started by: jacobs.smith
5 Replies

3. UNIX for Dummies Questions & Answers

Applying python on all the files in one folder --UNIX

Dear all, I know this might be a simple and silly question but I am struggling with it I have a long unix script and in a section of if I want to use a python script which should select all the files and run the script on it (as a whole) the loop I have is selecting file by file and I dont know... (2 Replies)
Discussion started by: A-V
2 Replies

4. Shell Programming and Scripting

Columns to Rows - Transpose - Special Condition

Hi Friends, Hope all is well. I have an input file like this a gene1 10 b gene1 2 c gene2 20 c gene3 10 d gene4 5 e gene5 6 Steps to reach output. 1. Print unique values of column1 as column of the matrix, which will be a b c (5 Replies)
Discussion started by: jacobs.smith
5 Replies

5. Shell Programming and Scripting

Convert rows to columns based on condition

I have a file some thing like this: GN Name=YWHAB; RC TISSUE=Keratinocyte; RC TISSUE=Thymus; CC -!- FUNCTION: Adapter protein implicated in the regulation of a large CC spectrum of both general and specialized signaling pathways GN Name=YWHAE; RC TISSUE=Liver; RC ... (13 Replies)
Discussion started by: raj_k
13 Replies

6. Shell Programming and Scripting

deleting rows under a certain condition

there are 20 variables and I would like to delete the rows if 13th-20th columns are all NA. Thank you! FID IID aspirpre statihos fibrahos ocholhos arbhos betabhos alphbhos cacbhos diurehos numbcig.x toast1 toast2 toast3 toast4 ischoth1 ischoth2 ischoth3 ischoth4 101 101 1 1 1 1 1 2 1 2... (2 Replies)
Discussion started by: johnkim0806
2 Replies

7. Shell Programming and Scripting

Compare values in two files. For matching rows print corresponding values from File 1 in File2.

- I have two files (File 1 and File 2) and the contents of the files are mentioned below. - I am trying to compare the values of Column1 of File1 with Column1 of File2. If a match is found, print the corresponding value from Column2 of File1 in Column5 of File2. - I tried to modify and use... (10 Replies)
Discussion started by: Santoshbn
10 Replies

8. UNIX for Dummies Questions & Answers

merged 10 files with column extraction into one

Hi, I have 600 text files. In each txt file, I have 3 columns, e.g: File 1 a 0.21 0.003 b 0.34 0.004 c 0.72 0.002 File 2 a 0.25 0.0083 b 0.38 0.0047 c 0.79 0.00234 File 3 a 0.45 0.0063 b 0.88 0.0027 c 0.29 0.00204 ... my filename as "sc2408_0_5278.txt sc2408_0_5279.txt... (2 Replies)
Discussion started by: libenhelen
2 Replies

9. Shell Programming and Scripting

How to capture 2 consecutive rows when a condition is true ?

Hi All, i have an input below. As long as "x= 1" , i would want to capture 2 lines using sed or awk for eg : 0001 x= 1 $---------------------------------..-.--.. 0001 tt= 137 171 423 1682 2826 0 Pls help. Thanks in advance. Note that the number of lines in each block do... (37 Replies)
Discussion started by: Raynon
37 Replies
Login or Register to Ask a Question