What is Grep ?
grep is a command line utility for searching plain text data sets for lines that match a regular expression. grep was developed originally for Unix operating system but later was available for all Unix like operating systems.
grep stands for global regular expression print.
Which file types grep is compatible with ?
grep is compatible with a wide variety of file types, as long as the files contain text that can be searched for patterns using regular expressions.
Some common file types that can be searched with grep include:
Plain text files: These are simple text files with no special formatting or encoding.
Configuration files: These files are used to configure applications and services and often have specific syntax and formatting rules.
Code files: These files contain programming code written in various languages such as C, Java, Python, etc.
Log files: These files are used to store system logs and application logs and can be searched to find specific events or error messages.
HTML, XML, and other markup files: These files contain structured data and can be searched for specific tags, attributes, or values.
CSV and other delimited data files: These files contain tabular data separated by commas or other delimiters, and can be searched for specific values or patterns.
In general, any file that contains text can be searched with grep. However, grep may not work well with binary files or files that are encoded in a non-text format, such as images or audio files. In such cases, you may need to use specialized tools or libraries that are designed to work with those file types.
What is the syntax for grep command ?
grep [OPTIONS] PATTERN [FILE...]
OPIONS are various command-line options that can be used to modify the behavior of grep. Some common options include:
-i: Ignore case distinctions during the search.
-v: Invert the match; display all lines that do not match the pattern.
-n: Display the line numbers of the matched lines.
-c: Display only the count of matched lines.
-r: Search recursively in directories and their subdirectories.
PATTERN is the regular expression that you want to search for in the specified files.
FILE... is the name of the file(s) to search. If no file is specified, grep searches for the pattern in standard input.
What are the examples of Grep command ?
1. Searching for a string in a file
grep "search text" file.txt
It will search file with name file.txt for any matching string "search text" in whole file and return lines which contains string "search text".
2. Searching for a string in multiple files.
grep "search text" file1,txt file2.txt file3.txt
It will search for files with names file1.txt, file2.txt, file3.txt for text "search text" and return lines with matching text.
3. Searching for a string in all files in a directory
grep "search text" directory/*
It will search all files under directory with name 'directory' and returns lines with matching text.
4. Searching for à string in all the files in a directory and its subdirectories.
grep -r "search text" directory/
It will search in files in a directory with name 'directory' and recursively under its subdirectories for text "search text" and return all matching lines.
5. Searching for a string and ignoring case.
grep -i "search text" file1.txt
It will search in file file1.txt for text "search text" ignoring case and return all matching lines. So, for example, if there is line which has "SEARCH TEXT" or "Search Text", those lines will also be returned.
6. Searching for a string and returning line numbers.
grep -n "search text" file1.txt
It will search file file1.txt for text "search text" and return all lines having "search text" along with line numbers of the lines.
7. Searching for a string and only returning matching string.
grep -o "search text" file1.txt
It will search in file file1.txt for "search text" and return for each matching line with "search text".
8. Searching for a string and returning lines which does not contain the string
grep -v "search text" file.txt
9. Searching for a string and returning only count of matching string
grep -c "search text" file.txt
10. Searching for a string and returning count of the number of occurrences of string.
grep -o "search text" file.txt | wc -l
How to use output of another command as input to search for grep command ?
Example1:
Suppose you have a command that generates some output, like ls -l, and you want to search that output for files that end with ".txt". You can pipe the output of ls -l into 'grep' like this:
ls -l | grep "\.txt$"
In this case, ls -l generates output and pipes it into grep. The pattern \.*txt$ will then be applied to the input that grep receives from ls -l. The $ at the end of the pattern means "end of line", so this pattern will match any line that ends with ".txt".
So, if no file is specified, grep searches for the pattern in standard input, which means you can pipe the output of another command into grep to search for a pattern within that output.
Example2:
Suppose under the current directory and its subdirectories, you want to search for all the files which has .log extension and then for each file that has .log extension, you want to execute grep to search for a text 'search text', then you need to execute following command:
find . -name "*.log" -exec grep -H 'search text' \{\} \
It will print the name of the file along with each matching line that contains the 'search text'.
Here's how the command works:
find . -name "*.log" searches for all files under the current directory and its subdirectories with a .log extension.
-exec is a flag that tells find to execute a command on each file that matches the search criteria.
grep -H 'search text' {} is the command that gets executed for each file that matches the search criteria. grep is a command that searches for patterns in text files. The -H flag tells grep to print the filename along with each matching line.
The \{\} is a placeholder that find replaces with the name of each file that matches the search criteria.
So when you run this command, find searches for all files with a .log extension under the current directory and its subdirectories, and for each file that matches the search criteria, it executes the grep command to search for the pattern "search text". The output shows the filename along with each matching line that contains the pattern.
Example 3:
cat file.txt | grep "pattern"
cat can be used to display the contents of a file, and grep can be used to filter the output based on a pattern.
Example 4:
cat file.txt | grep "pattern" | awk '{print $1}'
awk is a powerful text processing tool that can be used to filter and manipulate text data. grep can be used to filter the input, and awk can be used to perform additional processing.
Example 5:
cat file.txt | grep "pattern" | sed 's/foo/bar/g'
sed is another text processing tool that can be used to manipulate text data. grep can be used to filter the input, and sed can be used to perform additional processing.
This command uses grep to filter the input based on a pattern, and then uses sed to replace all occurrences of "foo" with "bar".
Example 6:
ps -ef | grep "process name"
ps can be used to list all running processes, and grep can be used to filter the output based on process name.
Can we use grep to search text in compressed file, like file compressed using gzip ?
grep does not work with compressed files compressed with gzip. To search text in files compressed with gzip, you need to use zgrep command line utility.
Example:
zgrep "search text" file.gz
Thanks for reading. If you enjoyed reading the article then please Subscribe to the blog.