开发者

Compare file with variable list AWK

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.开发者_StackOverflow社区 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?


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜