grep Command: Tutorial & Examples

Search for a pattern of text within a file

The grep command is a utility that is used to search for specific patterns of text within one or more files. It is a powerful tool that is often used in combination with other commands, such as cat, less, and tail, to filter and process the output of those commands.

To search for a specific pattern of text within a file, you can use grep followed by the search pattern and the name of the file:

grep "search_pattern" file.txt

This will search the file for lines that contain the search pattern and display any matches.

You can use the -i flag to ignore case when searching:

grep -i "search_pattern" file.txt

You can use the -r flag to search recursively through a directory and its subdirectories:

grep -r "search_pattern" directory

You can use the -v flag to invert the match and display lines that do not contain the search pattern:

grep -v "search_pattern" file.txt

grep is a very useful and versatile tool for searching and filtering text on a Linux system. It is often used in scripts and other automation tasks to extract specific information from text files or command output.

The text above is licensed under CC BY-SA 4.0 CC BY SA