compare two fields and get a third field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting compare two fields and get a third field
# 1  
Old 01-30-2012
compare two fields and get a third field

Hello,

I'm trying to get a value based on a comparison of two fields, this is:

file1
Code:
687.45
687.18
687.322
687.405
686.865

file 2
Code:
685    6.43 
686    6.43 
687    6.42 
688    6.42

I'm using this code that works well for rounded values:

Code:
awk -F " " 'BEGIN{while(getline<"file1") a [$1]=1 } ; a [$1] == 1 { print $2 }' file2

because my file1 has decimals, I dont know how to adapt the awk line above to round decimals in file1.

Any suggestion and help on this are very welcome, thanks in advance.
# 2  
Old 01-30-2012
Code:
awk -F " " 'BEGIN{while(getline<"file1"){b=int($1+0.5);a[b]=1}} ; a [$1] == 1 { print $2 }' file2

# 3  
Old 01-30-2012
Code:
awk 'NR==FNR{a[int($1+0.5)];next} $1 in a {print $2}' file1 file2

# 4  
Old 01-31-2012
Thanks for your answers, I'm a bit confused about the output, let me put an example please:

I have the two-columns file, from where I need to extract the info stored in the second column based on the other file (explained afterhere). The 2-columns has 3438 records:

fileA
Code:
1 23.69
2 23.64
3 23.63
4 23.69
5 23.64
6 23.63
7 23.61
8 23.49
9 23.32
10 23.06
...
...
3452 1.78
3453 1.78
3454 1.78
3455 1.78
3456 1.78
3457 1.78
3458 1.78
3459 1.78
3460 1.78
3461 1.78
3462 1.78
3463 1.78
3464 1.78
3465 1.78
3466 1.78
3467 1.78
3468 1.78
3469 1.78
3470 1.78

the other file, one-column file, has 15287 records:

fileB
Code:
3462.91
3456.48
3456.48
3456.48
3452.03
3452.03
3447.48
3447.48
3443.03
3443.03
3443.03
3438.63
3438.63
3434.08
3434.08
3434.08
3431.35
...
...
687.705
687.607
687.263
687.518
687.45
687.18
687.322
687.405
686.865

basically, the fileB should match the first column of fileA and then output the second column of fileA. But here is the part I don't know how to do it, it should be row-by-row, or in order based on fileB, so for instance, at the end I would get something like:

Code:
1.78
1.78
1.78
1.78
1.78
...

thinking on the first part of fileB for instance, from 3462.91 to 3452.03 (in red above). I think on this like a row-by-row scanning. So at the end, I should have an output with 15287 records like fileB.

Again, thanks for your nice support.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Is there a UNIX command that can compare fields of files with differing number of fields?

Hi, Below are the sample files. x.txt is from an Excel file that is a list of users from Windows and y.txt is a list of database account. $ head -500 x.txt y.txt ==> x.txt <== TEST01 APP_USER_PROFILE USER03 APP_USER_PROFILE TEST02 APP_USER_EXP_PROFILE TEST04 APP_USER_PROFILE USER01 ... (3 Replies)
Discussion started by: newbie_01
3 Replies

2. Shell Programming and Scripting

Inserting a field without disturbing field separator on other fields

Hi All, I have the input as below: cat input 032016002 2.891 97.109 16.605 27.172 24.017 32.207 0.233 0.021 39.810 0.077 0.026 19.644 13.882 0.131 11.646 0.102 11.449 76.265 23.735 16.991 83.009 8.840 91.160 0.020 99.980 52.102 47.898 44.004 55.996 39.963 18.625 0.121 1.126 40.189... (15 Replies)
Discussion started by: am24
15 Replies

3. Shell Programming and Scripting

Help in splitting Sub Fields and compare with other field

Hi All, We are trying to pull out data from below table, the table contains four fields and out of which last two fields are having sub-fields with delimiter $, we want to identify number "1" position in the 3rd field and from 4th field need to extract the information from the same position. ... (4 Replies)
Discussion started by: rramkrishnas
4 Replies

4. Shell Programming and Scripting

awk - compare 1st 15 fields of record with 20 fields

I'm trying to compare 2 files for differences in a selct number of fields. When differnces are found it will write the whole record of the second file including appending '|C' out to a delta file. Each record will have 20 fields, but only want to do comparison of 1st 15 fields. The 1st field of... (7 Replies)
Discussion started by: sljnk
7 Replies

5. Shell Programming and Scripting

How to print 1st field and last 2 fields together and the rest of the fields after it using awk?

Hi experts, I need to print the first field first then last two fields should come next and then i need to print rest of the fields. Input : a1,abc,jsd,fhf,fkk,b1,b2 a2,acb,dfg,ghj,b3,c4 a3,djf,wdjg,fkg,dff,ggk,d4,d5 Expected output: a1,b1,b2,abc,jsd,fhf,fkk... (6 Replies)
Discussion started by: 100bees
6 Replies

6. Shell Programming and Scripting

Plz Help. Compare 2 files field by field and get the output in another file.

Hi Freinds, I have 2 files . one is source.txt and second one is target.txt. I want to keep source.txt as baseline and compare target.txt. please find the data in 2 files and Expected output. Source.txt 1|HYD|NAG|TRA|34.5|1234 2|CHE|ESW|DES|36.5|134 3|BAN|MEH|TRA|33.5|234... (5 Replies)
Discussion started by: i150371485
5 Replies

7. Shell Programming and Scripting

Compare two files Field by field and output the result in another file

Hi Friends, Need Help. I have file1.txt as File1.txt |123|A|7267|Hyder|Cross|Sell|7801 |995|A|7051|2008|Lunar|New|Year|Promotion|7801 |996|A|7022|Q108|Targ|Prospect|&|SSCC|Savings|Promo|7801 |997|A|7182|Q1|Feb-Apr|08|Credit|ITA|PA|SBA|Campaign|7801 File2.txt... (7 Replies)
Discussion started by: i150371485
7 Replies

8. Shell Programming and Scripting

AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2

First, thanks for the help in previous posts... couldn't have gotten where I am now without it! So here is what I have, I use AWK to match $1 and $2 as 1 string in file1 to $1 and $2 as 1 string in file2. Now I'm wondering if I can extend this AWK command to incorporate the following: If $1... (4 Replies)
Discussion started by: right_coaster
4 Replies

9. Shell Programming and Scripting

Compare Tab Separated Field with AWK to all and print lines of unique fields.

Hi. I have a tab separated file that has a couple nearly identical lines. When doing: sort file | uniq > file.new It passes through the nearly identical lines because, well, they still are unique. a) I want to look only at field x for uniqueness and if the content in field x is the... (1 Reply)
Discussion started by: rocket_dog
1 Replies

10. Shell Programming and Scripting

Sorting on two fields time field and number field

Hi, I have a file that has data in it that says 00:01:48.233 1212 00:01:56.233 345 00:09:01.221 5678 00:12:23.321 93444 The file has more line than this but i just wanted to put in a snippet to ask how I would get the highest number with time stamp into another file. So from the above... (2 Replies)
Discussion started by: pat4519
2 Replies
Login or Register to Ask a Question