1. 目标
本文主要介绍 Linux “grep” 命令:用于查找文件里符合条件的字符串或正则表达式。
2. grep 命令
grep [options] pattern [files]
2.1 示例
root@dev:~/linux# cat > text.txt
-------------------------------------------------------------------
unix is great os. unix was developed in Bell labs.
learn operating system.
Unix linux which one you choose.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
通过 -i
选项,可以在给定文件中搜索不区分大小写的字符串。例如:匹配 “UNIX”、”Unix“、”unix “等词。
root@dev:~/linux# grep -i "UNix" text.txt
-------------------------------------------------------------------
unix is great os. unix was developed in Bell labs.
Unix linux which one you choose.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
root@dev:~/linux# grep -c "unix" text.txt
-------------------------------------------------------------------
2
root@dev:~/linux# grep -l "unix" *
或者
root@dev:~/linux# grep -l "unix" text.txt text1.txt text2.txt
输出:
text.txt
默认情况下,grep 会匹配给定的字符串/模式,即使该字符串/模式在文件中是子串。grep 的 -w
选项使其只匹配整个单词。
root@dev:~/linux# grep -w "unix" text.txt
-------------------------------------------------------------------
unix is great os. unix was developed in Bell labs.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
(5)只显示匹配的模式默认情况下,grep 会显示匹配字符串的整行。我们可以使用 -o
选项让 grep 只显示匹配的字符串。
root@dev:~/linux# grep -o "unix" text.txt
-------------------------------------------------------------------
unix
unix
unix
unix
unix
显示匹配行的文件行号。
root@dev:~/linux# grep -n "unix" text.txt
-------------------------------------------------------------------
1:unix is great os. unix was developed in Bell labs.
4:uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
root@dev:~/linux# grep -v -n "unix" text.txt
-------------------------------------------------------------------
2:learn operating system.
3:Unix linux which one you choose.
(8)匹配以字符串开头的行
^
正则表达式指定一行的开头。在 grep 中使用它可以匹配以给定字符串或正则匹配开头的行。
root@dev:~/linux# grep "^unix" text.txt
-------------------------------------------------------------------
unix is great os. unix was developed in Bell labs.
(9)匹配以字符串结尾的行
$
正则表达式指定一行的结尾。在 grep 中可以使用它来匹配以给定字符串或正则表达式结尾的行。
root@dev:~/linux# grep "labs.$" text.txt
-------------------------------------------------------------------
unix is great os. unix was developed in Bell labs.
root@dev:~/linux# grep -e "unix" -e "great" -e "os" text.txt
-------------------------------------------------------------------
unix is great os. unix was developed in Bell labs.
Unix linux which one you choose.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
(11)-f
文件选项 查找符合规则条件的文件内容,格式为每行一个规则样式
root@dev:~/linux# cat > pattern.txt
-------------------------------------------------------------------
unix
learn
root@dev:~/linux# grep -f pattern.txt text.txt
-------------------------------------------------------------------
unix is great os. unix was developed in Bell labs.
learn operating system.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
(12)打印文件中的 n 个特定行
-A
打印搜索到的行和结果之后的 n 行,-B
打印搜索到的行和结果之前的 n 行,-C
打印搜索到的行和结果之后及之前的 n 行。
语法:
$grep -A[NumberOfLines(n)] [search] [file]
$grep -B[NumberOfLines(n)] [search] [file]
$grep -C[NumberOfLines(n)] [search] [file]
-A
示例:
root@dev:~/linux# grep -A1 great text.txt
-------------------------------------------------------------------
unix is great os. unix was developed in Bell labs.
learn operating system.
root@dev:~/linux# grep -A2 great text.txt
-------------------------------------------------------------------
unix is great os. unix was developed in Bell labs.
learn operating system.
Unix linux which one you choose.
-B
示例:
root@dev:~/linux# grep -B1 choose text.txt
-------------------------------------------------------------------
learn operating system.
Unix linux which one you choose.
root@dev:~/linux# grep -B2 choose text.txt
-------------------------------------------------------------------
unix is great os. unix was developed in Bell labs.
learn operating system.
Unix linux which one you choose.
-C
示例:
root@dev:~/linux# grep -C1 choose text.txt
-------------------------------------------------------------------
learn operating system.
Unix linux which one you choose.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
语法:
grep -R [Search] [directory]
示例:
root@dev:~/linux# grep -iR operating /root
-------------------------------------------------------------------
Binary file /root/proxy-agent.v0.6.tar matches
/root/get-docker.sh: echo "ERROR: Unsupported operating system 'macOS'"
Binary file /root/frontend.tar matches
/root/linux/text.txt:learn operating system.
(14)-E
正则表达式
root@dev:~/linux# grep -E "^[A-Z][a-z]" text.txt
-------------------------------------------------------------------
Unix linux which one you choose.
(15)-o
输出文件中匹配到的部分
root@dev:~/linux# grep -o -E "[A-Z]" text.txt
-------------------------------------------------------------------
B
U
N
L
原文地址:https://blog.csdn.net/qq_59831338/article/details/134723501
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_42116.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!