ampersander
ampersander

Reputation: 288

AWK Finding a way to print lines containing a word from a comma separated string

I want to write a bash script that only prints lines that, on their second column, contain a word from a comma separated string. Example:

words="abc;def;ghi;jkl"

>cat log1.txt
hello;abc;1234
house;ab;987
mouse;abcdef;654

What I want is to print only lines that contain a whole word from the "words" variable. That means that "ab" won't match, neither will "abcdef". It seems so simple yet after trying for manymany hours, I was unable to find a solution.

For example, I tried this as my awk command, but it matched any substring.

-F \; -v b="TSLA;NVDA" 'b ~ $2 { print $0 }'

I will appreciate any help. Thank you.

EDIT:

A sample input would look like this

1;UNH;buy;344.74
2;PG;sell;138.60
3;MSFT;sell;237.64
4;TSLA;sell;707.03

A variable like this would be set

filter="PG;TSLA"

And according to this filter, I want to echo these lines

2;PG;sell;138.60
4;TSLA;sell;707.03

Upvotes: 2

Views: 1712

Answers (2)

anubhava
anubhava

Reputation: 784968

You may use this awk:

words="abc;def;ghi;jkl"
awk -F';' -v s=";$words;" 'index(s, FS $2 FS)' log1.txt

hello;abc;1234

Upvotes: 2

glenn jackman
glenn jackman

Reputation: 246754

Grep is a good choice here:

grep -Fw -f <(tr ';' '\n' <<<"$words") log1.txt

With awk I'd do

awk -F ';' -v w="$words" '
    BEGIN {
        n = split(w, a, /;/)
        # next line moves the words into the _index_ of an array, 
        # to make the file processing much easier and more efficient
        for (i=1; i<=n; i++) words[a[i]]=1
    }
    $2 in words
' log1.txt

Upvotes: 4

Related Questions