All Articles

The ABC's of grep - F

F

Use the -F flag if you want your match pattern to be “fixed strings”. What does that mean? This is the option you want if you are looking for lines with special meta characters used in regular expressions.

Let’s take a look at our file test.txt and add a line with a caret at the end:

sad
happy
awake
coffee
work
school
This is a caret ^

In order to search for the ^​ character, run the following:

$ grep -F "^" test.txt
This is a caret ^

If you have a bunch of patterns in a text file and want to pass it along to the grep command, -f is your friend.

Create a file patterns.txt with the following content:

^s
e$
work

The patterns in this file are saying, look for all the lines that start with s​, ends with e​ and contains the word work​.

Here is an example of how to run it:

$ grep -f patterns.txt test.txt
sad
coffee
work
school

Published Oct 30, 2023

I love coffee, coding and writing.