Hello I am writing a script to extract some lines from a list, the main idea is that i have a list of names and I want to create an archive only with the information of that persons, the first list looks as follows:
Name Address Age
Steve 72 Andover Dr. 11
Yuri 1133 Nicholas Av. 14
David 110 Arlington Street 11
Andy 38 Devonshire Road 11
Dan 38 Devonshire Road 14
Blaine 59 Village Road 11
Justin 59 Village Road 11
Rob 39 Jackson Drive 15
Russell 39 Jackson Dr 14
Justin 16 Crestwood Rd. 17
Tristan 75 Peak Avenue 11
Marty 101 Captains Walk 16
Jason 73 Minuteman Dr. 13
Joe 73 Minuteman Dr. 11
Philip 61 Prospect Avenue 11
Evan 281 Burnt Plains Road 11
And the list of names is the following:
Andy
Russell
Philip
Evan
I want to produce a new archive called newlist.txt that should contain only the following information:
Andy 38 Devonshire Road 11
Russell 39 Jackson Dr 14
Philip 61 Prospect Avenue 11
Evan 281 Burnt Plains Road 11
In order to achieve that I tried writing the following script:
#!/bin/bash
while read line
do
grep $line $2 >> newlist.txt
done <$1
I am getting the file newlist.txt with the information of the persons, in fact my script works well but I am not sure if this is the best approach and I am considering the enough cautions, since I am going to use it with a very long text I decided to show my code in order to receive suggestions, thanks any how.