others-how to grep in linux or macos?
1. Purpose
In this post, I will demonstrate how to grep something in linux server or MacOS .
2. Solution
What is grep?
Grep is a powerful text search tool that uses regular expressions to search text and print the matching lines.
Grep command family
The grep family includes grep
, egrep
, and fgrep
: egrep is an extension of grep that supports more regex(regular expressions) metacharacters; fgrep is fixed grep or fast grep.
How does grep work?
Grep searches one or more files for string templates. If the template includes spaces, it must be quoted, and all strings following the template are treated as filenames. The results of the search are sent to standard output and do not affect the contents of the original file.
The grep command format
grep [options] pattern [files]
Simple grep command examples
1) grep something in a file
grep "keyword" my_file.txt
2) grep in many files
grep "keyword" my_file.*
or
grep "keyword" file1 file2
3) grep case insensitive
grep -i "Keyword" my_file.txt
4) count the result of grep
grep -c "keyword" my_file
5) show grep result around the matching lines
If you want to show more context around the matching lines, you can use the -C
option to grep:
grep -C 5 "keyword" my_file
The above command will show 5 lines before the matching line and 5 lines after the matching line.
6) grep the result that does NOT match, e.g. grep inversely
grep -v "keyword" my_file
7) grep with some directories excluded
grep --exclude-dir="mydir" .
Grep recursively
For example to search content name
in current directory recursively:
grep -r 'name' .
It means to recursively search the current directory for lines with the string “name”.
Grep with line number
If you want to show the line number in the result, you can just add "-n"
to display line numbers in the result:
grep -rln 'name' .
It means to recursively search for "name"
in the current directory and display the line number in the result.
Grep with regex
Interestingly, regular expressions can be enclosed in double quotes or single quotes. And if your expression doesn’t contain spaces, you can even omit the quotes.
For example , if you want to grep the files that contains lines that starts with How to
, you can grep as follows:
➜ bswen-github grep -r '^How to' .
./rust-tutorials/README.md:How to learn rust languages and introduce some rust tutorials.
it’s highlighted!
A more complete regular expression that supports in grep is as follows:
* Match the preceding item zero or more times.
? Match the preceding item zero or one time.
+ Match the preceding item one or more times.
{n} Match the preceding item exactly n times.
{n,} Match the preceding item at least n times.
{,m} Match the preceding item at most m times.
{n,m} Match the preceding item from n to m times.
Some useful grep commands
1) grep by process name and then kill it
ps aux|grep my_process_keyword|grep -v grep|awk '{print $2}'|xargs kill
The above command will search in current processes to find the process whose name include my_process_keyword
, and then kill it.
2) grep filenames recusively
grep -ir --include "*.cpp" "xyz" .
The command above says to search recursively starting in current directory ignoring case on the pattern and to only search in files that match the glob pattern “*.cpp”.
3) grep lines that starts with some keyword
grep '^How' my.txt
4) grep lines that ends with some keyword
grep `times$` my.txt
3. Summary
In this post, I demonstrated how to use grep something in files. That’s it, thanks for your reading.