count values based on contents of another file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers count values based on contents of another file
# 1  
Old 05-24-2011
Question count values based on contents of another file

Hello,

I have two files as shown below:

test1
678
679
689
690
710

test2
1 678
654 800
676 791
689 900

I want to get a count of lines from test2 whose columns bound the values in test1

I tried running the code below; however am getting wrong results.

Thanks for your input,

~Guss

Code:
while read A
do
awk '$1<=$A && $2>=$A' test2 | wc -l  >> Count
done < test1


Last edited by vbe; 05-24-2011 at 10:14 AM.. Reason: repl color with code tags...
# 2  
Old 05-24-2011
I'd do it all in a single awk. I'm sure there are other ways that require less keystrokes, but this is readable. Assuming your input files are named t1 and t2:

Code:
awk '
    BEGIN {
        n = 0;
        while( (getline< "t1") > 0 )       # save values from file 1
            val[n++] = $1;

        close( "t1" );
    }

    {                                   # test each pair against each file 1 value
        for( i = 0; i < n; i++ )
            if( $1 <= val[i]  && $2 >= val[i] )
                count++;                # count if file 1 value lies between
    }

    END {
        printf( " %d\n", count );       # print final count to stdout
    }
'<t2


Last edited by agama; 05-24-2011 at 12:34 AM.. Reason: clarification
# 3  
Old 05-24-2011
Thanks agama, however the code is giving a single value of 14. For t1 (same contents as test1) there are 3 occurrences of each of the values in t2 (same contents as test2).

Thanks,
~Guss

Last edited by Gussifinknottle; 05-24-2011 at 07:08 AM.. Reason: Wrong File Name given
# 4  
Old 05-24-2011
Ok, I misunderstood and was counting the total number of times that a line in test2 bound a value in test1.

This exits the loop on the first match, and thus counts the number of lines from test1 which are bound by any line in test2.

Code:
awk -v t1=t1 '
    BEGIN {
        n = 0;
        while( (getline<t1) > 0 )       # save values from file 1
            val[n++] = $1;

        close( t1 );
    }

    {                                   # test each pair against each file 1 value
        for( i = 0; i < n; i++ )
            if( $1 < val[i]  && $2 > val[i] )
            {
                count++;                # count if file 1 value lies between
                break;           #<<<< break on first hit
            }
    }

    END {
        printf( " %d\n", count );
    }
'<t2

From your initial example, I assumed that bounding was <= or >=, however if only three lines are bound then the equal must bed dropped else the 1,678 pair will test positive for 678.

This is assuming I've understood correctly this time. If not, please give an example of which lines would match.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove or rename based on contents of file

I am trying to use the two files shown below to either remove or rename contents in one of those files. If in file1.txt $5 matches $5 of file2.txt and the value in $1 of file1.txt is not "No Match" then that value is substituted for all values in $5 and $1 of file2.txt. If however in $1 ... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Shell Programming and Scripting

File comparison based on contents

Hi I have 2 files 1.del ---- 1,2,3,4,5 1,2,3,4,4 1,1,1,1,2 2.del ---- 1,2,3,4,5 1, 1,2,3,4,4 1,1,1,1,2 I need to compare the above two files in unix, as in the output should only tell the difference in contents as I should get only the line 1 ( from 2.del) , rest all lines are... (4 Replies)
Discussion started by: Ethen561
4 Replies

3. Shell Programming and Scripting

Find and count unique date values in a file based on position

Hello, I need some sort of way to extract every date contained in a file, and count how many of those dates there are. Here are the specifics: The date format I'm looking for is mm/dd/yyyy I only need to look after line 45 in the file (that's where the data begins) The columns of... (2 Replies)
Discussion started by: ronan1219
2 Replies

4. Shell Programming and Scripting

Picking contents value of file at run time based on variable values

HI, I have to write a unix script and need your help. in my application where I have to invoke this script a varialble is there where the value comes in a variable . for example variable can be var=ABC like this there will be any type of value in the vcariable. there is a unix directory... (2 Replies)
Discussion started by: manish8484
2 Replies

5. UNIX for Dummies Questions & Answers

find lines in another file based on contents in a second file

Hello, I have a file with tab delimited columns like: File1 A 2 C R F 4 D Q C 9 A B ...... I want to grep out the lines in a second file, File2, corresponding to each line in File1 Can I do this: while read a b c d do grep '$a\t$b\t$c\t$d' File2 >>... (2 Replies)
Discussion started by: Gussifinknottle
2 Replies

6. UNIX for Dummies Questions & Answers

count number of rows based on other column values

Could anybody help with this? I have input below ..... david,39 david,39 emelie,40 clarissa,22 bob,42 bob,42 tim,32 bob,39 david,38 emelie,47 what i want to do is count how many names there are with different ages, so output would be like this .... david,2 emelie,2 clarissa,1... (3 Replies)
Discussion started by: itsme999
3 Replies

7. Shell Programming and Scripting

move contents from one file to another based on line number or content

I want a script that will move everything beyond a certain line number or beyond a certain content word into another file. For example, if file A has this: first line second line third line forth line fifth line sixth line I want to run a script that will move everything beyond the third... (4 Replies)
Discussion started by: robp2175
4 Replies

8. Shell Programming and Scripting

Execution problem with sort the file based on contents

My input file: >ali ASSDDGHFHFHJFJHJDSDGSDGSDGSDGSDGSDGSDGDSGDSGSDGDSGSDGSDGDSGSDGGDSG >zzz ASdASDASDSADSADDSADJKHJDSADKLJADKLSAJDLJLKJLDASDDSADd >abu ASDASDFSAFASFSADFASDASDSADSADSADSADSADSADASDASdSADSADSADA >aaa... (2 Replies)
Discussion started by: patrick87
2 Replies

9. Shell Programming and Scripting

Remove lines based on contents of another file

So, this issue is driving me nuts! I was hoping to get a lending hand here... I have 2 files: file1.txt contains: this is example1 this is example2 this is example3 this is example4 this is example5 file2.txt contains: example3 example5 Basically, I need a script or command to... (4 Replies)
Discussion started by: bashshadow1979
4 Replies

10. Shell Programming and Scripting

sh script that reads/writes based upon contents of a file

Hi everyone, Ive got a quick question about the feasibility and any suggestions for a shell script. I can use sh or ksh, doesnt matter. Basically, Ive got an output file from a db2 command that looks like so: SCHEMA NAME CARD LEAF ELEAF LVLS ISIZE NDEL KEYS F4 F5 ... (3 Replies)
Discussion started by: rdudejr
3 Replies
Login or Register to Ask a Question