Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

grep file for strings in multiple lines in external file 1

Status
Not open for further replies.

arunrr

Programmer
Oct 2, 2009
103
US
Hello,

Here is file1...

Fri Feb 17 2012, 03:20 GMT 14:20 Local, Australia v Sri Lanka,Sydney, Commonwealth Bank Series
Fri Feb 17 2012, 06:00 GMT 19:00 Local, New Zealand v South Africa,Wellington, 1st T20I
Sat Feb 18 2012, 11:00 GMT 15:00 Local, England v Pakistan,Dubai (DSC), 3rd ODI
Sat Feb 18 2012, 22:30 GMT 18:30 Local, West Indies v India (Women),North Sound, 1st T20I
Sun Feb 19 2012, 01:45 GMT 14:45 Local, New Zealand v England (Women),Hamilton, 2nd T20I
Sun Feb 19 2012, 03:20 GMT 13:20 Local, Australia v India,Brisbane, Commonwealth Bank Series

Here is file2...

Feb 17 2012,New Zealand v South Africa,1st T20I
Feb 17 2012,Australia v Sri Lanka,Commonwealth Bank Series

From file1 I need to remove lines based on the contents file2. I cant simply use "grep -f" as the string match is not exact. I read in the variable values for each line with FS=, and use "egrep -v" as below...

while IFS=, read A B C
do
egrep -v "$A.*$B.*$C" file1 > temp
mv temp file1
done < file2

Looking for a simpler, one-line approach please...

Thanks in advance...
Arun
 
Two possibilities:

Code:
awk -F, 'NR==FNR{a[++i]=$1".*"$2".*"$3;next}{for (i in a)if(match($0,a[i]))next;print}' file2 file1

Code:
sed 's/,/.*/g' file2 > file2.re
egrep -vf file2.re file1

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Thanks a lot! Tried both, they work great.
Arun
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top