0

I am a beginner of bash script. I just started to write a script where it checks the contents of b.txt can all be found in a.txt. (line by line preferably). My code is as following:
grep -Ffw b.txt a.txt

As you can see, I want to do fixed string instead of REGEX, I want to check everything from the b.txt file, because there are some strings inside the b.txt and I want to check if all of them exist in a.txt. And I also want to match the whole word only of course. So these are the requirements, however when I run this command it returns me an error says:
grep: w: No such file or directory

I am thinking that maybe there are some limitations of the flags in bash? Sorry I am not really familiar with the language, didn't read much about the MAN page etc. If anyone could help me to solve the puzzle it would be appreciated :) In addition, i think if possible I would like to add a -q to surpress the output when there is a match also, right now I didn't add it in the example since it couldn't make it through with 3 flags even. So can anyone give me some hints here? Thanks in advance!

3
  • 4
    the b.txt has to follow the -f flag. So you have to write grep -Fwf b.txt a.txt or grep -wFf b.txt a.txt Commented Aug 10, 2021 at 13:13
  • Oh thanks, it works now :) didn't expect to be this simple though Commented Aug 10, 2021 at 13:18
  • do you care to give a breif explanation on why b.txt has to follow the ``-f` flag? :) Just curious Commented Aug 10, 2021 at 13:20

2 Answers 2

1

Hereby some explanation from the manpage:

OPTIONS
   Generic Program Information
   ...
      -F, --fixed-strings
              Interpret PATTERNS as fixed strings, ...
      -f FILE, --file=FILE
              Obtain patterns from FILE, ...
      -w, --word-regexp
              Select only those lines ...

As you can see, the options -F and -w are indicated ending immediately (hence the comma in -F, and -w,), but the -f switch is followed by FILE, with means they belong together.

I you want to preserve the order Ffw, that's possible, but then you need to do something like:

grep -Ff b.txt -w a.txt
Sign up to request clarification or add additional context in comments.

1 Comment

This is really great explanation. Thank you!
0

As mentioned by @kvantour, the solution is simply placing the -f before the b.txt file.
grep -Fwf b.txt a.txt
Should have thought it when it says 'no such file or directory' as it was a clear indication that flags after the -f were treated as the path already.

Comments

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.