Question: All help would be appreciated as I have tried much googling and have drawn a blank :)

I am new to sed and do not know what the command I need might be.

I have a file containing numerous lines eg

```
John Smith Aweqwewq321
Mike Smith A2345613213
Jim Smith Ad432143432
Jane Smith A432434324
John Smith Bweqwewq321
Mike Smith B2345613213
Jim Smith Bd432143432
Jane Smith B432434324
John Smith Cweqwewq321
Mike Smith C2345613213
Jim Smith Cd432143432
Jane Smith C432434324

```

The file is plain text and is not formatted (ie not csv or anything like that )

I want to search for a list of specific strings, eg. John Smith, Mike Smith, Jim Smith and return only the last line entry in the file for each string found (all other lines found would be deleted). 

(I do not necessarily need every unique entry, ie Jane Smith may or may not be needed)

It is important that the original order of the lines found is maintained in the output. 

So the result would be : 

```
John Smith Cweqwewq321
Mike Smith C2345613213
Jim Smith Cd432143432

```

I am new to sed and do not know what this command might be.

There are about 100 specific search strings.

Thank you :)

Answer: 
Assuming `sample.txt` contains the data you provided:

```
$ cat sample.txt
John Smith Aweqwewq321
Mike Smith A2345613213
Jim Smith Ad432143432
Jane Smith A432434324
John Smith Bweqwewq321
Mike Smith B2345613213
Jim Smith Bd432143432
Jane Smith B432434324
John Smith Cweqwewq321
Mike Smith C2345613213
Jim Smith Cd432143432
Jane Smith C432434324

```

For this sample data, following script works just fine:

```
$ cut -f1,2 -d' ' sample.txt  | sort | uniq | while read s; do tac sample.txt | grep -m1 -n -e "$s" ; done | sort -n -r -t':' | cut -f2 -d':'

John Smith Cweqwewq321
Mike Smith C2345613213
Jim Smith Cd432143432
Jane Smith C432434324

```

Here is the breakdown of the script:

* First generate all the unique strings ( First Name, Last Name in this case )
* Now find the last occurrence each of these strings. For this we find first occurrence by reversing the file. Also print the line number along with output.
* Now reverse the output in reverse line number order, and then remove the line numbers ( we don't need them )
* you didn't tell the format of given list, I assume it is `CSV`, same as you wrote in question: `eg. John Smith, Mike Smith, Jim Smith`
* from your description, you want line `each string found`, not only col1 and col2

from above two points, I have:

```
awk -v list="John Smith, Mike Smith, Jim Smith" 'BEGIN{split(list,p,",\\s*")}
    {for(i=1;i<=length(p);i++){
        if($0~p[i]){
            a[p[i]]=$0
            break
        }
    }
}END{for(x in a)print a[x]}' file

```

you can fill the `list` with your strings, sepearated by comma. output this with your test data:

```
John Smith Cweqwewq321
Mike Smith C2345613213
Jim Smith Cd432143432

```