grep Command
grep command :- grep command is used to search files and print lines that have the matched string .
1) Print all lines that have the "error" word in file
grep error file.txt
2) Print all lines of files that have the "error" word
grep error file1.txt file2.txt file3.txt
3) Print all lines that have unix word in any capital and small letter
grep -i unix file.txt
Note:- "i" option for case insensitive search.
4) Print all lines those starts with digit
grep "^[0-9]*" file.txt
Note:-"^" denote start of line
5) Print all lines those does not start with digit
grep "^[^0-9]*" file.txt
Note:-"^" sign inside [ ] denote negation of given range
6) Print all lines those end with digit
grep "*[0-9]$" file.txt
7) Print all lines those does not end with digit
grep "*[^0-9]$" file.txt
8) Print all matched lines with 2 lines before matched line that have the "error" word in file
grep -B 2 error file.txt
9) Print all matched lines with 3 lines after matched line that have the "error" word in file
grep -A 3 error file.txt
10) Print all lines that does not contain "error" word in file
grep -v error file.txt
Note:- "v" option invert the selection
11) Print all non-empty lines
grep -v "^$" file.txt
12) Print line count that have matched pattern in file
grep -c error file.txt
13) Print all matched line with line number
grep -n error file.txt
14) Print the file name that contain matched pattern
grep -l error *.txt
No comments:
Post a Comment