merging files using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting merging files using awk
# 8  
Old 02-03-2012
Try:
Code:
awk 'NR==FNR{a[$0]=$2;b[$0]=$3}NR!=FNR{for (i in a){if (($2>=a[i]&&$2<=b[i])||($3>=a[i]&&$3<=b[i])){print i" "$4}}}' file2 file1

# 9  
Old 02-07-2012
Hi,

Firstly thanks for the solution. However it does not seem to be 100% proper for my requirement. In the above code I dont understand where are you checking for the same chr( chromosome) in 2 files?

for example

Code:
chr1    670944  671094  altPromoter     - LOC100133331
chr18   670323  674160  bleedingExon    - LOC100133331
chr18   670323  674160  bleedingExon    - DQ575786
chr18   670323  674160  bleedingExon    - DQ599872

In the above o/p LOC100133331 belongs to chr1 and chr18 based on the coordinates. but in real scenario if you take file 1 its only on chr1.

It should go by chr basis in both files. If the row on file 2 is on chr1 then only chr1 rows should be checked in file1 for assigning the column 4 value.

Is this been taken care of in the above code?

Thanks,

---------- Post updated 02-07-12 at 12:22 PM ---------- Previous update was 02-06-12 at 03:09 PM ----------

Hi,

Please let me know if I was not clear.. I can explain more in detail once again

Thanks,
# 10  
Old 02-07-2012
Try this:
Code:
awk 'NR==FNR{a[$0]=$2;b[$0]=$3;}NR!=FNR{for (i in a){split(i,x," +");if (($1==x[1]&&$2>=a[i]&&$2<=b[i])||($3>=a[i]&&$3<=b[i])){print i" "$4}}}' file2 file1

# 11  
Old 02-08-2012
Hi,

I tried the new code.. but still i dont find the difference between both outputs. What I feel is its not considering the chr name in column 1 of 2 files.. It needs to pick column 4 value only from the same chr in both files..

Thanks,
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk Merging multiple files with symbol representing new file

I just tried following ls *.dat|sort -t"_" -k2n,2|while read f1 && read f2; do awk '{print}' $f1 awk FNR==1'{print $1,$2,$3,$4,$5,"*","*","*" }' OFS="\t" $f2 awk '{print}' $f2 donegot following result 18-Dec-1983 11:45:00 AM 18.692 84.672 0 25.4 24 18-Dec-1983 ... (3 Replies)
Discussion started by: Akshay Hegde
3 Replies

2. Shell Programming and Scripting

Merging rows in awk

Hello, I have a data format as follows: Ind1 0 1 2 Ind1 0 2 1 Ind2 1 1 0 Ind2 2 2 0 I want to use AWK to have this output: Ind1 00 12 21 Ind2 12 12 00 That is to merge each two rows with the same row names. Thank you very much in advance for your help. (8 Replies)
Discussion started by: Homa
8 Replies

3. Shell Programming and Scripting

Merging two files in awk

Hi, How I can merge two file columns such as the followings using awk: file 1 2 3 2 2 1 1 file 2 4 3 4 5 7 6 Result: 2 3 4 3 2 2 4 5 1 1 7 6 This is an example, at the end, I will have about 25 files that I want to merge them, it is important for me that the order in the... (7 Replies)
Discussion started by: Homa
7 Replies

4. Shell Programming and Scripting

Merging together two awk scripts

I have two awk scripts shown below. checkTrvt.awk works on file format .xt, whereas checkData.awk workds on file format .dat I want to merge the two scripts together, if I find that the user passed .xt file I do the code for .xt file, whereas if user passes .dat file, I go through the code for... (9 Replies)
Discussion started by: kristinu
9 Replies

5. Shell Programming and Scripting

AWK Script For Merging Text Files

Hello, I am trying to merge data from two text files. One file (File1) contains a listing of data which includes the trial number in Column 5, while the other text file (File2) contains what category the trial belongs to. Here is a snippet of what File1 looks like. 1 Arrow_ST 9.738 0.905... (2 Replies)
Discussion started by: Jahn
2 Replies

6. Shell Programming and Scripting

awk command : row by row merging of two files

I want to write a scrpit to merge files row wise (actually concatinating) main.txt X Y Z file 1 A B C file 2 1 2 3 now i want the script to check if the file1 is empty or not, if empty then make it like A B C 1 2 3 again to check if second file is empty if not do as done... (0 Replies)
Discussion started by: shashi792
0 Replies

7. Shell Programming and Scripting

awk merging files based on 2 complex conditions

1. if the 1st row IDs of input1 (ID1/ID2.....) is equal to any IDNames of input2 print all relevant values together as defined in the output. 2. A bit tricky part is IDno in the output. All we need to do is numbering same kind of letters as 1 (aa of ID1) and different letters as 2 (ab... (4 Replies)
Discussion started by: ruby_sgp
4 Replies

8. Shell Programming and Scripting

merging of 2 files AWK, SHELL or something else

I have 2 files pipe delimted and want to merge them based on a key e.g file 1 123|xxx|yyy|zzz 345|xab|yzy|zyz 456|sss|ttt|foo file 2 123|hhh|ggg|xxx 345|ddd|www|ddd|fff 456|ddd|sss|sss|eee so if the key is the first field, and the result should be file 1 with field 2 from file 2... (24 Replies)
Discussion started by: klut
24 Replies

9. Shell Programming and Scripting

merging of 2 files AWK - part 2

i have try , but i think i will never learn awk :( now i have 2 files : a 1:aaa:2:aaa1 2:bbb:2:bbb1 3:ccc:3:ccc1 b aaa:2 bbb:0 ccc:3 output: for all lines where a.$2 == b.$1 i want to compare a.$3 != b.$2 if true then set err=1 if false set err=0 and print all lines from file a +... (2 Replies)
Discussion started by: pp56825
2 Replies

10. Shell Programming and Scripting

Merging files with AWK filtering and counting lines

Hi there, I have a couple of files I need to merge. I can do a simple merge by concatenating them into one larger file. But then I need to filter the file to get a desired result. The output looks like this: TRNH 0000000010941 ORDH OADR OADR ORDL ENDT 1116399 000000003... (2 Replies)
Discussion started by: Meert
2 Replies
Login or Register to Ask a Question