Roger
Roger

Reputation: 8576

grep from two variables

I am trying to eliminate the duplicate lines of a list like this.

LINES='opa
opa
eita
eita
argh'

DUPLICATE='opa
eita'

The output I am looking for is argh. Till now, this is what I tried:

echo -e "$DUPLICATE" | grep --invert-match -Ff- <(echo -e "$LINES")

And:

grep --invert-match -Ff- <(echo -e "$DUPLICATE") <(echo -e "$LINES")

But unsuccessfuly.

I know that I can achieve this if I put the content of $LINES into a file:

echo -e "$DUPLICATE" | grep --invert-match -Ff- FILE

But I'd like to know if this is possible only with variables.

Upvotes: 2

Views: 130

Answers (2)

karakfa
karakfa

Reputation: 67497

another approach which doesn't require to create a duplicate list separately,

$ awk '{a[$0]++} END{for(k in a) if(a[k]==1) print k}' <<< "$LINES"

count occurrence of each line, print only if it's not duplicated (count==1).

Upvotes: 1

John Kugelman
John Kugelman

Reputation: 361605

Passing a dash as the file name to -f means "read from stdin". Get rid of it so the file name given to -f is the process substitution.

There's no need for echo -e, and -v is shorter and more common than --invert-match.

echo "$LINES" | grep -vFf <(echo "$DUPLICATE")

Equivalently, using a herestring:

grep -vFf <(echo "$DUPLICATE") <<< "$LINES"

Upvotes: 1

Related Questions