awk-filter record by another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk-filter record by another file
# 1  
Old 06-20-2012
awk-filter record by another file

I have file1
Code:
3049
3138
4672
22631
45324
112382
121240
125470
130289
186128
193996
194002
202776
228002
253221
273523
284601
284605
641858
701851
716844

and file2

Code:
3983    4981
5843    7501
9169    11160
12222   12776
14276   15016
17390   19207
20065   20781
21922   22746
23512   24480
25457   26044
27418   30078
30656   32185
33362   33610
34289   34639
36834   37322
38330   39691
40664   42940
45072   45596
48065   48874
49576   50022
53338   55938
58650   59420
60581   62711
63709   64716
65602   65925
67187   68425
73410   74783
75569   76438
78806   79312
79687   80358
80927   82090
82426   82869
85172   86095
87726   88358

The output file should be
Code:
4672
22631
45324


I need to filter file1 if the value is in between $1 and $2 of file2
Could anyone give a help? awk maybe a good way to filter them...Smilie

Last edited by biomed; 06-20-2012 at 06:34 AM.. Reason: code tags
# 2  
Old 06-20-2012
Please post the output required.

Does this meet your requirement?

Code:
awk 'NR==FNR{a[$1]=$2;next} {
 for(i in a)
 {
  if($0 >= i && $0 <= a[i])
  {
   print
   break
  }
 }
}' file2 file1


Last edited by elixir_sinari; 06-20-2012 at 06:34 AM..
This User Gave Thanks to elixir_sinari For This Post:
# 3  
Old 06-20-2012
Yes, your code meet my requirements, thanks a lot...
# 4  
Old 06-20-2012
Quote:
Originally Posted by biomed
The output file should be
Code:
4672
22631
45324

Code:
awk 'NR==FNR{a[i++]=$1;next}{for(x=1;x<=i;x++) if( a[x] >= $1 && a[x] <= $2) print a[x]}' file1 file2 >outfile

---------- Post updated at 12:06 PM ---------- Previous update was at 11:50 AM ----------

Quote:
Originally Posted by elixir_sinari
Please post the output required.

Does this meet your requirement?

Code:
awk 'NR==FNR{a[$1]=$2;next} {
 for(i in a)
 {
  if($0 >= i && $0 <= a[i])
  {
   print
   break
  }
 }
}' file2 file1

Are you sure with your code? These two 3049 and 3138 digits shall not be in outfile.

outfile

Code:
3049
3138
4672
22631
45324

# 5  
Old 06-20-2012
Quote:
Originally Posted by sdf
Are you sure with your code? These two 3049 and 3138 digits shall not be in outfile.

outfile

Code:
3049
3138
4672
22631
45324

Pretty sure that those 2 numbers don't turn up in the output.
# 6  
Old 06-20-2012
Quote:
Originally Posted by elixir_sinari
Pretty sure that those 2 numbers don't turn up in the output.
Odd, checked your code on gawk 3 and 4 and they appear.
# 7  
Old 06-20-2012
Try then with

Code:
gawk 'NR==FNR{a[$1]=$2;next} {
 for(i in a)
 {
  if(int($0) >= int(i) && int($0) <= int(a[i]))
  {
   print
   break
  }
 }
}' file2 file1

Or this to force a numeric comparison...

Code:
gawk 'NR==FNR{a[$1]=$2;next} {
 for(i in a)
 {
  if(0+$0 >= 0+i && 0+$0 <= 0+a[i])
  {
   print
   break
  }
 }
}' file2 file1

The latter is safer as you prevent truncating floats..

Last edited by elixir_sinari; 06-20-2012 at 07:38 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

CSV File:Filter duplicate records from column1 & another column having unique record

Hi Experts, I have csv file with 30, 40 columns Pasting just 2 column for problem description. Need to print error if below combination is not present in file check for column-1 (DocumentNumber) and filter columns where value in DocumentNumber field is same. For all such rows, the field... (7 Replies)
Discussion started by: as7951
7 Replies

2. Shell Programming and Scripting

Filter and sort the file using awk

I have file and process it and provide clean output. input file Device Symmetrix Name : 000A4 Device Symmetrix Name : 000A5 Device Symmetrix Name : 000A6 Device Symmetrix Name : 000A7 Device Symmetrix Name : 000A8 Device Symmetrix Name : 000A9 Device Symmetrix Name ... (10 Replies)
Discussion started by: ranjancom2000
10 Replies

3. Shell Programming and Scripting

Delete record filter by column

Dear friend, I have a file 2 files with column wise FILE_A ------------------------------ x,1,@ y,3,$ x,5,% FILE_B -------------------- x,1,@ i like to delete the all lines in FILE_A ,if first column available in FILE_B. output (in FILE_A) y,3,$ x,5,% (10 Replies)
Discussion started by: Jewel
10 Replies

4. Shell Programming and Scripting

Help with awk, using a file to filter another one

I have a main file: ... 17,466971 0,095185 17,562156 id 676 17,466971 0,096694 17,563665 id 677 17,466971 0,09816 17,565131 id 678 17,466971 0,099625 17,566596 id 679 17,466971 0,101091 17,568062 id 680 17,466971 0,016175 17,483146 id... (4 Replies)
Discussion started by: boblix
4 Replies

5. Shell Programming and Scripting

AWK filter from file and print

Dear all, I am using awk to filter some data like this:- awk 'NR==FNR{a;next}($1 in a)' FS=":" filter.dat data.dat >! out.dat where the filter and input data look like this:- filter.dat... n_o00j_1900_40_007195350_0:n_o00j_1940_40_007308526... (3 Replies)
Discussion started by: atb299
3 Replies

6. Shell Programming and Scripting

Filter record from a file

Reposting since I didnt not get any reply. I have a problem while filtering records from a file. Can somebody help please? For eg: Consider the below files Record file: 0003@00000000000190@20100401@201004012010040120100401@003@... (1 Reply)
Discussion started by: gpaulose
1 Replies

7. Shell Programming and Scripting

filter record from a file reading another file

Hi, I want to filter record from a file if the records in the second column matches the data in another file. I tried the below awk command but it filters the records in the filter file. I want the opposite, to include only the records in the filter file. I tried this: awk -F'|'... (8 Replies)
Discussion started by: gpaulose
8 Replies

8. Shell Programming and Scripting

Filter records in a file using AWK

I want to filter records in one of my file using AWK command (or anyother command). I am using the below code awk -F@ '$1=="0003"&&"$2==20100402" print {$0}' $INPUT > $OUTPUT I want to pass the 0003 and 20100402 values through a variable. How can I do this? Any help is much... (1 Reply)
Discussion started by: gpaulose
1 Replies

9. Shell Programming and Scripting

Help in writing a KSH script to filter the latest record?

Hi All, I have a text file with the folowing content. BANGALORE|1417|2010-02-04 08:41:04.174|dob|xxx BANGALORE|1416|2010-02-04 08:23:19.566|dob|yyy BANGALORE|1415|2010-02-04 08:20:14.497|dob|aaa BANGALORE|1414|2010-02-04 08:19:40.065|dob|vvv BANGALORE|1413|2010-02-04... (4 Replies)
Discussion started by: Karpak
4 Replies

10. Shell Programming and Scripting

filter the uniq record problem

Anyone can help for filter the uniq record for below example? Thank you very much Input file 20090503011111|test|abc 20090503011112|tet1|abc|def 20090503011112|test1|bcd|def 20090503011131|abc|abc 20090503011131|bbc|bcd 20090503011152|bcd|abc 20090503011151|abc|abc... (8 Replies)
Discussion started by: bleach8578
8 Replies
Login or Register to Ask a Question