[Bash] grep command
grep
is a powerful command-line tool used for searching text using patterns. It's widely used for finding specific text within files or output streams.
grep [options] pattern [file...]
Example file
# example.txt
Hello World
This is a sample file
Bash scripting is powerful
Learning grep, awk, and sed
Another line with the word World
Simple Search:
$ grep "awk" example.txt
Learning grep, awk, and sed
Case-Insensitive Search:
$ grep -i "AWK" example.txt
Learning grep, awk, and sed
The -i
option makes the search case-insensitive.
Search in Multiple Files:
$ grep "awk" example.txt hello.txt
example.txt:Learning grep, awk, and sed
Recursive Search in Directories:
grep -r "search_term" /path/to/directory
The -r option searches recursively through all files in the specified directory.
Display Line Numbers:
$ grep -n "awk" example.txt hello.txt
example.txt:4:Learning grep, awk, and sed
The -n
option shows the line numbers of matching lines.
Show Only Matching Part of Line:
$ grep -o awk example.txt
awk
Count Matches:
$ grep -c awk example.txt
1
The -c option counts the number of matching lines.
Invert Match:
$ grep -v awk example.txt
Hello World
This is a sample file
Bash scripting is powerful
Another line with the word World
The -v
option selects lines that do not match the pattern.
Use Regular Expressions:
grep -E "^Learning" example.txt
Learning grep, awk, and sed
The -E
option allows the use of extended regular expressions.
Try
mkdir -p test_dir
echo "Test file" > test_dir/file1.txt
echo "Another test file" > test_dir/file2.txt
grep -r "test" test_dir
test_dir/file2.txt:Another test file
echo -e "abc123\nabc456\n789" > regex_example.txt
grep -E "[0-9]{3}" regex_example.txt
abc123
abc456
789
echo -e "apple\nbanana\ncherry\napple pie" > regex_example.txt
# Search for lines containing "apple" followed by any character:
grep -E "apple." regex_example.txt
apple pie
Script example
#!/bin/zsh
echo "Enter the search term: "
read search_term
echo "Enter the file name: "
read file_name
if [ -f "$file_name" ]; then
count=$(grep -c "$search_term" "$file_name")
echo "The term '$search_term' appears $count times in $file_name."
else
echo "File $file_name does not exist."
fi
Advance usage
Search for a word and show 2 lines of context before and after each match
grep -C 2 "search_term" filename
Search for Whole Words Only
Search for whole words only (not as part of another word)
grep -w "search_term" filename
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2020-05-30 [Python] Simple Decorators
2020-05-30 [Python] Template literals f""
2020-05-30 [Python] Inheritance
2017-05-30 [RxJS] Implement pause and resume feature correctly through RxJS
2016-05-30 [RxJS] Filtering operators: distinct and distinctUntilChanged
2016-05-30 [RxJS] Filtering operators: throttle and throttleTime
2016-05-30 [RxJS] Transformation operators: debounce and debounceTime