Question: I'm reading a string from file, appliances\_list.txt. 

appliances\_list.txt contains

```
fridge
dryer
ironbox
microwave 

```

The file I'm reading is myappliances.txt. Content is

```
I have a fridge
I have another fridge
I have a refridgerator
I have a microwave
I have ironbox at home
I have another microwave
I have a hairdryer

```

I'm using 

```
grep -o -m1 -f appliances_list.txt myappliances.txt

```

Output is 

`fridge`

My desired output is, first occurrence of each string (exact match)

```
fridge
microwave
ironbox

```

Can someone point me in the right direction?

Answer: 
***awk*** solution:

```
awk 'NR==FNR{ a[$0]; next }{ 
              gsub(/<\/?[^<>]+>/,"",$0); for(i=1;i<=NF;i++) 
              if ($i in a && !a[$i]){ a[$i]++; print $i; break } 
    }' appliances_list.txt myappliances.txt

```

* `a[$0];` - capturing pattern words from `appliances_list.txt`
* `for(i=1;i<=NF;i++)` - iterating through fields/words of the line from `myappliances.txt`

The output:

```
fridge
microwave
ironbox

```
Remove `-m1` and pipe to `sort -u`:

`grep -owf appliances_list.txt myappliances.txt | sort -u`

`sort -u` will sort and then unique the lines. If that sorting is undesired, you might have to use something like awk. perl, or python.

Note that to get only `dryer` and not `hairdryer` you need `grep -w`, so the suggestion above has -o**w**.