Grep Linux Command
Posted at 12:38 AM
grep Linux Command
grep is a utility to look for specific strings in files, making a search line by line, you can also use regular expression instead of a string.
Usage
grep [options] pattern [file-list]
grep output with no options are all the lines that contain the pattern in the given file.
Some of its more useful options are:
- -v
- Reverse the behavior of grep and make it list the lines that do not contain the pattern
- -n
- Shows the line where the pattern is (or is not using the -v option) together with the line number
- -c
- Displays the number of lines that contains the pattern in the give file(s)
- -i
- Makes grep to show as a match no matter the case, (lower or upper) so it is no more case sensitive
Some examples of these options are:
Having the file grep.txt (Which the man page of grep listed below)
grep -c very grep.txt 1
grep -c in grep.txt 181
grep -i email grep.txt
Example for grep -E
[prompt]$ cat abc.txt
1
2
3
4
[ prompt ]$ grep -E "1|3" abc.txt
1
3