How to use the command fgrep (with examples)
The fgrep
command is used to search for fixed strings in files. It is equivalent to the grep -F
command. It is an efficient tool for searching for exact strings in files.
Use case 1: Search for an exact string in a file
Code:
fgrep search_string path/to/file
Motivation: This use case is helpful when you need to find an exact string in a file. It is especially useful when searching for fixed strings to avoid any interpretation as regular expressions.
Explanation:
search_string
is the exact string to be searched for in the file.path/to/file
is the path to the file where the string will be searched.
Example output:
If we run the following command:
fgrep "apple" fruits.txt
and the fruits.txt
file contains the following lines:
I like apples.
I like bananas.
Apples are delicious.
The output will be:
I like apples.
Apples are delicious.
Use case 2: Search only lines that match entirely in files
Code:
fgrep -x search_string path/to/file1 path/to/file2
Motivation: This use case is useful when you want to search for lines that match the search string entirely, without any additional characters before or after the string.
Explanation:
-x
option tellsfgrep
to search for lines that match the search string entirely.search_string
is the exact string to be searched for in the file.path/to/file1 path/to/file2
are the paths to the files where the string will be searched.
Example output:
If we have two files, file1.txt
and file2.txt
, with the following contents:
file1.txt:
apple
apple pie
banana
file2.txt:
apple
banana
Running the following command:
fgrep -x "apple" file1.txt file2.txt
will produce the output:
apple
Use case 3: Count the number of lines that match the given string in a file
Code:
fgrep -c search_string path/to/file
Motivation: This use case is helpful when you want to know the total number of lines that contain the searched string in a file.
Explanation:
-c
option tellsfgrep
to count the number of lines that match the search string.search_string
is the exact string to be searched for in the file.path/to/file
is the path to the file where the string will be searched.
Example output:
If we run the following command:
fgrep -c "apple" fruits.txt
and the fruits.txt
file contains the following lines:
I like apples.
I like bananas.
Apples are delicious.
The output will be:
2
Use case 4: Show the line number in the file along with the line matched
Code:
fgrep -n search_string path/to/file
Motivation: This use case is useful when you want to know both the line number and the line that match the searched string in a file.
Explanation:
-n
option tellsfgrep
to display the line number along with the line matched.search_string
is the exact string to be searched for in the file.path/to/file
is the path to the file where the string will be searched.
Example output:
If we run the following command:
fgrep -n "apple" fruits.txt
and the fruits.txt
file contains the following lines:
I like apples.
I like bananas.
Apples are delicious.
The output will be:
1:I like apples.
3:Apples are delicious.
Use case 5: Display all lines except those that contain the search string
Code:
fgrep -v search_string path/to/file
Motivation: This use case is helpful when you want to filter out lines that contain the search string and display all other lines in a file.
Explanation:
-v
option tellsfgrep
to display all lines that do not match the search string.search_string
is the exact string to be searched for in the file.path/to/file
is the path to the file where the string will be searched.
Example output:
If we run the following command:
fgrep -v "apple" fruits.txt
and the fruits.txt
file contains the following lines:
I like apples.
I like bananas.
Apples are delicious.
The output will be:
I like bananas.
Use case 6: Display filenames whose content matches the search string at least once
Code:
fgrep -l search_string path/to/file1 path/to/file2
Motivation: This use case is useful when you want to find the filenames of files that contain the search string at least once.
Explanation:
-l
option tellsfgrep
to display the filenames of files that contain the search string.search_string
is the exact string to be searched for in the files.path/to/file1 path/to/file2
are the paths to the files where the string will be searched.
Example output:
If we have two files, file1.txt
and file2.txt
, with the following contents:
file1.txt:
I like apples.
I like bananas.
file2.txt:
I like oranges.
Oranges are delicious.
Running the following command:
fgrep -l "apples" file1.txt file2.txt
will produce the output:
file1.txt
Conclusion:
The fgrep
command is a powerful tool for searching for exact strings in files. It provides various options to customize the search, such as searching for entire matching lines, counting occurrences, displaying line numbers, filtering out matching lines, and listing filenames. By understanding these different use cases, you can efficiently search for fixed strings in files using fgrep
.