I have a file with content similar to:
google.com,9,AB+CD,nonAB+nonCD
youtube.com,9,AB+CD,AB+CD
facebook.com,20,AB+CD,nonCD
The number of columns is not fixed. But the first column is a URL, the second is a number, starting form the third is keywords separated by commas but they vary from site to another.
I want to count the number of URLs (lines) where I can control what keywords are in the line. For example,
1) AB+CD
without nonAB
and nonCD
. Note: the word AB+CD can occur many times.
2) AB+CD without occurrence of nonCD
(but it is ok if there is anything else)
How to search for a string in a line AND ensure the absence of another string. When I use:
grep 'AB+CD' test.txt > result.txt
It prints every line where 'AB+CD' is found.
What if I want to print the line where there is only 'AB+CD' to get:
youtube.com,9,AB+CD,AB+CD
Or there is 'AB+CD' with anything else except 'nonAB' to get:
youtube.com,9,AB+CD,AB+CD
facebook.com,20,AB+CD,nonCD