tags:

views:

313

answers:

1

I'm stumbling over myself trying to get a seemingly simple thing accomplished. I have one file, and one newline delimited string list.

File:

Dat1 Loc1

Dat2 Loc1

Dat3 Loc1

Dat4 Loc2

Dat5 Loc2

My list is something like this:

Dat1

Dat2

Dat3

Dat4

What I am trying to do is compare the list with the data file and count the number of unique Locs that appear. I am interested only in the largest count. In the example above, when comparing the list to the file, I want essentially:

Dat1 MATCHED Loc1Count = 1

Dat2 MATCHED Loc1Count = 2

Dat3 MATCHED Loc1Count = 3

Dat4 MATCHED Loc2Count = 1

Return: Loc1 if Loc1Count/Length of List > 50%

Now,

I know that awk 1 file will read a file line by line. Furthermore I know that "echo "$LIST" | awk '/search for a line that contains this/" will return the line that matches that internal string. I haven't been able to combine these ideas successfully though as nested awks, much less how to count "loc1" vs "loc2" (which, by the way, are going to be random strings, and not form-standard)

I feel like this is simple, but I'm banging my head against a wall. Any ideas? Is this sufficiently clear?

+2  A: 
list="Dat1 Dat2 Dat3 Dat4"
awk -vli="$list" 'BEGIN{
   # here list from shell is converted to awk array "list". 
   m=split(li,list," ") 
}
{
    # go through the list 
    for(i=1;i<=m;i++){
        if($1 == list[i]){
            # if Dat? is found in list, print , at the same time
            print $1" matched Locount="$2" "++data[$2]   # increment the count for $2 and store in loc array
            loc[$2]++ 
        }
    }
} 
END{
    # here returns loc1 count
    loc1count=loc["Loc1"]
    if(( loc1count / m *100 ) > 50) {
        print "Loc1 count: "loc1count
    }
} ' file

output

$ ./shell.sh
Dat1 matched Locount=Loc1 1
Dat2 matched Locount=Loc1 2
Dat3 matched Locount=Loc1 3
Dat4 matched Locount=Loc2 1
Loc1 count: 3
ghostdog74
Oops - I'm having difficulty finding how to include my variable "$LIST"awk: illegal field $(), name "LIST" source line number 1returns when: msplit($LIST,list,"\n")
Andrew J. Freyer
awk variables and shell variables are different. to pass shell variables to awk, use -v option.
ghostdog74
I'm having no success with awk -v LIST=${LIST} 'BEGIN ...
Andrew J. Freyer
-v LIST="${LIST}" Works well, seemingly, to pass the variable within awk. However, now I'm having difficulty with the newline delimiter. ERROR - newline in string Dat1 ... at source line 1
Andrew J. Freyer
show how you get your $LIST. or even better, show the code that you have.
ghostdog74
vote up, great use of awk, ghostdog, would you mind doing some commenting on the code?
Anders
LIST=`airport -s | egrep -o '([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}'` this retrieves a list of the BSSIDs of neighboring APs on my Mac.
Andrew J. Freyer
ok, then use tr to convert the newlines to space. >>> LIST=$(airport -s | egrep -o '([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}' | tr "\n" " ") . This way, your LIST will become like what i used. one string
ghostdog74
This is a great start for me. Thanks very much.
Andrew J. Freyer