Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...If there has ever been a justification needed for access to the net during working hours, just referring to this site should suffice. Fantastic!..."

Geography

Where in the world do Tek-Tips members come from?

grep file for strings in multiple lines in external fileHelpful Member! 

arunrr (Programmer)
16 Feb 12 14:14
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
Helpful Member!  Annihilannic (MIS)
16 Feb 12 18:32
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
tgmlify - code syntax highlighting for your tek-tips posts

arunrr (Programmer)
16 Feb 12 23:35
Thanks a lot! Tried both, they work great.
Arun

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close