Linux三剑客之grep

2022-05-23  本文已影响0人  归源

说起Linux操作系统中的grep命令,或许没有人会不知道。在我看来,grep 命令是Linux操作系统上最强大的工具之一,正如你无时无刻不在使用它。无论是从文件中找到匹配的行,又或者是从终端输出中获取指定信息,都离不开对 grep 的使用。

了解一个工具的使用,还是先看这个工具的man手册会告诉我们什么。

# man grep
GREP(1)                               General Commands Manual                              GREP(1)

NAME
       grep, egrep, fgrep - print lines matching a pattern

SYNOPSIS
       grep [OPTIONS] PATTERN [FILE...]
       grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]

DESCRIPTION
       grep  searches  the  named  input  FILEs  (or standard input if no files are named, or if a
       single hyphen-minus (-) is given as file name) for lines containing a match  to  the  given
       PATTERN.  By default, grep prints the matching lines.

       In  addition,  two  variant  programs  egrep and fgrep are available.  egrep is the same as
       grep -E.  fgrep is the same as grep -F.  Direct invocation as  either  egrep  or  fgrep  is
       deprecated,  but  is  provided  to  allow  historical applications that rely on them to run
       unmodified.
...

手册中提到,grep 工具还有两个孪生兄弟,分别是 egrepfgrep。其中,egrep 相当于 grep -E 的用法,使用的是扩展形式的正则表达式;fgrep 相当于 grep -F 的用法,根据固定模式进行内容匹配。

参数详解

对于任何的Linux操作系统,你可以很轻松的找到/etc/passwd文件,以下的多数演示将会以该文件进行演示。以下为用户的部分信息

#cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

grep 命令的格式相对比较简单。完整的命令格式如 grep [OPTIONS] PATTERN [FILE...],其中 [OPTIONS] 选项提供了众多的参数,如下

如果需要找到包含root用户和adm用户的行,可以通过grep -E "root|adm" /etc/passwd来查找所有满足的行

#grep -E "root|adm" /etc/passwd
root:x:0:0:root:/root:/bin/bash
adm:x:3:4:adm:/var/adm:/sbin/nologin
# grep -e root -e adm /etc/passwd
root:x:0:0:root:/root:/bin/bash
adm:x:3:4:adm:/var/adm:/sbin/nologin
# cat grep-file
adm
root
# grep -f grep-file /etc/passwd
root:x:0:0:root:/root:/bin/bash
adm:x:3:4:adm:/var/adm:/sbin/nologin
# cat grep-file
adm
root
LOGGER
Logging
# grep -i log grep-file
LOGGER
Logging
# grep -v root /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
# grep -w Logg grep-file
# grep -w Logging grep-file
Logging
# cat grep-file
adm
root
LOGGER
Logging
Logging record
# grep -x Logging grep-file
Logging
# grep -c Log grep-file
3
# grep -L root grep-file
# grep -L name grep-file
grep-file
# grep -l name grep-file
# grep -l root grep-file
grep-file
# grep -m 2 Log grep-file
Logging
Logging record
# grep -o roo -n grep-file
2:roo    # 2 为行号
# grep -o root -n grep-file
2:root
# grep -o root -n -q grep-file
# echo $?
0
# grep -s root  grep-
# grep  root  grep-
grep: grep-: 没有那个文件或目录
# cat grep-file
adm
root
LOGGER
Logging
Logging record
Loggingrecord
# grep -b root grep-file
4:root
# grep -b ad grep-file
0:adm
# grep -H root /etc/passwd grep-file
/etc/passwd:root:x:0:0:root:/root:/bin/bash
/etc/passwd:operator:x:11:0:operator:/root:/sbin/nologin
grep-file:root
# grep -n root grep-file
2:root
# grep -A 2 -n root grep-file
2:root
3-LOGGER
4-Logging
# grep -B 2 -n root grep-file
1-adm
2:root
# grep -C 2 -n root grep-file
1-11111
2-adm
3:root
4-LOGGER
5-Loggin

正则部分

了解 grep 的常用参数可以在工作中解决绝大部份的问题,但在一些情况下,需要对过滤出的内容按照某种规则进行精确匹配,还需要配合正则使用。以下介绍了几种常用的正则语法

# grep -E "^a" grep-file
adm
# grep -E "record$" grep-file
Logging record
Loggingrecord
# grep -E gr+ grep-file
Loggingrecord
# grep -E ad* grep-file
adm
# grep -E g{2} grep-file  # 匹配至少包含2个g的单词
Logging
Logging record
Loggingrecord
# grep -E o{1} grep-file
root
Logging
Logging record
Loggingrecord
# grep -E "o{2,}" grep-file
root
# cat grep-file
adm
root
LOGGER
Logging
Logggggggggggg
Logging record
Loggingrecord
# grep -E "g{,3}i" grep-file
Logging
Logging record
Loggingrecord
# grep -E "gg{1,3}i" grep-file
Logging
Logging record
Loggingrecord
上一篇 下一篇

猜你喜欢

热点阅读