0

I have a long list (~100 lines) containing lines of names of animals, where they were spotted and when named spottings. The first 3 lines could be this:

cat 1999 madagascar 
dog 1827 peru 
kangaroo 1995 new zealand
cat 1827 england

Now I want to match that list with another list want that goes like this:

cat
dog
kangaroo

However, I only want to get the lines that also match a given year but not a given location. For examples, with 1827 and england I've done it like this:

cat spottings | grep -i -f want | sed '/1827/I!d;/england/Id'

Now, if my yearand location are located in a string like this:

want="1827,1999,2013" # year
nonotwant="england,madagascar,peru" # location

which is made into an array like this:

want=($(echo ${want} | tr ',' '\n'))

If I would check for all elements of wantand donotwant it would be like this:

sed spottings | grep -i -f want | sed '/1827/I!d;/1999/I!d;/2013/I!d;/england/Id;/madagascar/Id;/peru/Id'

How would I check for all elements in both arrays?

1
  • To remove ambiguity, would you show your desired output for the given want and nonotwant? Also, the command sed '/1827/I!d;/1999/I!d would eliminate all lines unless they have both years on the same line. Do you, instead, want it eliminate all lines unless they have one or the other? Please clarify. Commented Oct 19, 2014 at 20:08

1 Answer 1

1

why not a

fgrep -i -f want spottings | fgrep -i -f WantYear | fgrep -v -f NotWantLocation

where want, WantYear and NotWantLocation are file with 1 criteria per line.

Sign up to request clarification or add additional context in comments.

2 Comments

I'd really prefer to have WantYear and NotWantLocation as arrays, if that's possible.
generate a temporary file from your array.(f)grep is very fast from reference file source

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.