1

I have two files A.txt and B.txt containing two lists respectively as shown bellow.

File A.txt

hello
hi 
ko

File B.txt

fine
No 
And how
why

Now I want to check presence of any of these words (from A.txt AND B.txt) in a line in another file C.txt.

I am using the grep command

grep -iof A.txt C.txt| grep B.txt

C.txt contains sentences containing words from A.txt and B.txt

Hello I am fine
I am not fine
why ko is and how?

doesn't show any output

So, now I want if any word from A.txt and B.txt present simultaneously in one sentence it should show the output as

Hello fine
why ko and how 

To print only the matching words from both files if they occur simultaneously in C.txt, instead of printing the whole line from C.txt

Doej
  • 49
  • 7
  • 1
    You say *presence of **either** of these words*. I'm not sure whether I understand *either* in this context. You mean *presence of **any** of these words*, don't you? I.e. you want to know whether there is a line in C.txt, which contains either a word from A.txt or from B.txt. Correct? – user1934428 May 19 '16 at 09:20
  • I just edited the question. I meant presence of any of these words from A.txt and B.txt occuring simultaneously in C.txt – Doej May 19 '16 at 09:24
  • I don't quite understand what you mean by *simultaneously* in this case, but since you have accepted an answer, I guess this is what you were looking for. ;-) – user1934428 May 23 '16 at 09:56

1 Answers1

3

You probably want to say:

$ grep -if B <(grep -if A C)
Hello I am fine
why ko is and how?

This uses -f to provide the expressions. It can be a file... or a file you create on the fly with the process substitution <( ... ).

Firstly, grep -if A C matches all the words in C that are in A:

$ grep -if A C
Hello I am fine        # "Hello" highlighted
why ko is and how?     # "ko" highlighted

Then, its output is compared with the content in B.

$ grep -if B <(grep -if A C)
Hello I am fine        # "fine" highlighted
why ko is and how?     # "and how" highlighted

Depending on your needs, you may want to add -F, -w and -i.

From man grep:

   -f FILE, --file=FILE
          Obtain  patterns  from  FILE,  one  per  line.   The  empty file
          contains zero patterns, and therefore matches nothing.   (-f  is
          specified by POSIX.)

   -F, --fixed-strings
          Interpret PATTERN as a  list  of  fixed  strings,  separated  by
          newlines,  any  of  which is to be matched.  (-F is specified by
          POSIX.)

   -i, --ignore-case
          Ignore  case  distinctions  in  both  the  PATTERN and the input
          files.  (-i is specified by POSIX.)

   -w, --word-regexp
          Select  only  those  lines  containing  matches  that form whole
          words.  The test is that the matching substring must  either  be
          at  the  beginning  of  the  line,  or  preceded  by  a non-word
          constituent character.  Similarly, it must be either at the  end
          of  the  line  or  followed by a non-word constituent character.
          Word-constituent  characters  are  letters,  digits,   and   the
          underscore.
Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • where am I mentioning the two files here? – Doej May 19 '16 at 09:20
  • 1
    I think it should be - for the safe side - `fgrep`, not `grep` - just in case that one of the pattern files contains characters such as a period or $. Another problem is that the OP wants to match *words*, not strings. Grepping for, i.e., 'hi', would also match for instance 'hint', which is probably not what the OP wanted. In any case, I think the problem should be specified more precisely before we can propose a solution. – user1934428 May 19 '16 at 09:24
  • I just edited, main aim is to find presence of any of the words from A.txt and B.txt occuring simultaneously in C.txt – Doej May 19 '16 at 09:25
  • @Doej see my update with `$ grep -f B <(grep -if A C)`. – fedorqui May 19 '16 at 09:28
  • @user1934428 good point. I added (or suggested) `-F`, `-i`, `-w`. – fedorqui May 19 '16 at 09:30