Certainly! Here are some more grep commands with examples:
1.
Search for a pattern in multiple files:
grep pattern file1 file2
file3
2.
Search for a whole word:
grep -w wholeword filename
3.
Search recursively and display only filenames:
grep -r -l pattern
directory
4.
Count the number of lines that match a pattern in multiple files:
grep -c pattern file1 file2
5.
Display the line number and content for each matching line:
grep -n pattern filename
6.
Search for lines that start with a specific pattern:
grep '^pattern' filename
7.
Search for lines that end with a specific pattern:
grep 'pattern$' filename
8.
Search for lines that start with one pattern and end with another:
grep
'^start_pattern.*end_pattern$' filename
9.
Search for lines that do not contain a pattern:
grep -v pattern filename
10.
Search for lines that match one of multiple patterns:
grep -e pattern1 -e
pattern2 filename
11.
Case-insensitive search and display matching lines:
grep -i pattern filename
12.
Display only the matched part of the line (using capturing groups):
grep -oE
'pattern1(pattern2)pattern3' filename
13.
Search for lines containing any character from a specified set:
grep '[aeiou]' filename
14.
Search for lines not containing any character from a specified set:
grep '[^0-9]' filename
15.
Display lines that match multiple patterns in any order:
grep -e pattern1 -e
pattern2 filename | grep -e pattern3
These
examples cover a range of scenarios for using grep in different ways. Feel free
to adapt them to your specific needs, and always refer to the grep manual (man
grep) for more details and options.
No comments:
Post a Comment