How To Use Regular Expression In Grep Command In Linux?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
1. To search for a pattern in a file or files, use the grep command with the -E (extended regex) option:
$ grep -E “pattern” files
2. To search for a pattern and display only the matched lines, use the -o (only) option:
$ grep -E -o “pattern” files
3. To search multiple patterns in a file or files, use the -E option and the pipe (|) operator:
$ grep -E “pattern1|pattern2|pattern3” files
4. To search for a pattern and display the matched lines along with line numbers, use the -n (number) option:
$ grep -E -n “pattern” files
5. To search for a pattern and ignore case, use the -i (ignore case) option:
$ grep -E -i “pattern” files
6. To search for a pattern and display only the total count of matching lines, use the -c (count) option:
$ grep -E -c “pattern” files