Linux 之 grep命令

grep是一个强大的命令行工具,用于从文本文件或管道数据中筛选匹配特定模式的行。文章展示了如何使用grep进行不区分大小写的搜索、统计匹配行数、显示行号、反向匹配以及递归查找等操作。同时,还介绍了正则表达式的基础知识,如量词、字符集和边界,以及在grep中的应用。
摘要由CSDN通过智能技术生成

grep 从文本文件或管道数据流中筛选匹配的行和数据。

grep 搜索命名输入 FILE(如果未命名文件,或者如果给出单个连字符减号 (-) 作为文件名,则搜索标准输入)以查找包含与给定 PATTERN 匹配的行。 默认情况下,grep 打印匹配的行。

    -i 不区分大小写
    -n 显示行号
    -o only-match
    -A 在什么之后 after
    -B 在什么之前 before
    -C center 上下文
    -E 扩展正则

    -v 取反

    -r 递归查找,从文件夹里查找所有的文件

grep的示例

# 只显示匹配的行
[root@ansible_nfs ~]# cat /etc/passwd|egrep -o "root"
root
root
root
root

# 统计行数
[root@ansible_nfs ~]# cat /etc/passwd|egrep -o "root"|wc -l
4

# 显示行号
[root@ansible_nfs ~]# cat /etc/passwd|egrep -n "root"
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin

# 不区分大小写
[root@ansible_nfs cjhli]# cat passwd|egrep  -i  "root"
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
HUAWEI ROOT

# 取反
[root@ansible_nfs cjhli]# cat passwd|egrep -v [0-9]
APPLE
HUAWEI ROOT

[root@ansible_nfs ~]# cat passwd|egrep  [^0-9]
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin

# 递归查找
[root@ansible_nfs cjhli]# egrep "root" -r /shell
/shell/7-1/cjh3/passwd:root:x:0:0:root:/root:/bin/bash
/shell/7-1/cjh3/passwd:operator:x:11:0:operator:/root:/sbin/nologin
/shell/7-1/cjhli/passwd:root:x:0:0:root:/root:/bin/bash
/shell/7-1/cjhli/passwd:operator:x:11:0:operator:/root:/sbin/nologin

[root@ansible_nfs cjhli]# cat test.txt |egrep -C  1 SANXING
xiaomi huawei
SANXING vivo
1234 > #

[root@ansible_nfs cjhli]# cat test.txt |egrep -A  1 SANXING
SANXING vivo
1234 > #

[root@ansible_nfs cjhli]# cat test.txt |egrep -B  1 SANXING
xiaomi huawei
SANXING vivo

[root@ansible_nfs cjhli]# cat /etc/ssh/sshd_config |egrep -n -v  "^$|^#"
22:HostKey /etc/ssh/ssh_host_rsa_key
24:HostKey /etc/ssh/ssh_host_ecdsa_key
25:HostKey /etc/ssh/ssh_host_ed25519_key
32:SyslogFacility AUTHPRIV
47:AuthorizedKeysFile	.ssh/authorized_keys
65:PasswordAuthentication yes
69:ChallengeResponseAuthentication no
79:GSSAPIAuthentication yes
80:GSSAPICleanupCredentials no
96:UsePAM yes
101:X11Forwarding yes
126:AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
127:AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
128:AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
129:AcceptEnv XMODIFIERS
132:Subsystem	sftp	/usr/libexec/openssh/sftp-server

? 0次或1次
+ 1次或多次
* 0次或多次
.* 所有内容
^$ 空行

[root@ansible_nfs cjhli]# echo "abc abbc ac"|egrep "ab?c"
abc abbc ac

[root@ansible_nfs cjhli]# echo "abc abbc ac"|egrep "ab+c"
abc abbc ac

[root@ansible_nfs cjhli]# echo "abc abbc ac"|egrep "ab*c"
abc abbc ac

[root@ansible_nfs cjhli]# echo "abc abbc abbbbc abbbbbc ac"|egrep "ab{4}c"
abc abbc abbbbc abbbbbc ac

[root@ansible_nfs cjhli]# echo "abc abbc abbbbc abbbbbc ac"|egrep "ab{4,}c"
abc abbc abbbbc abbbbbc ac

单词的边界

\b
\<
\>

[root@ansible_nfs cjhli]# cat /var/log/messages |egrep "\<[a-Z]{16,18}\>"
Jul  3 08:10:18 ansible_nfs firewalld[728]: WARNING: AllowZoneDrifting is enabled. This is considered an insecure configuration option. It will be removed in a future release. Please consider disabling it now.
Jul  3 08:10:18 ansible_nfs NetworkManager[761]: <info>  [1688343018.0799] settings: Loaded settings plugin: NMSKeyfilePlugin (internal)

密码验证脚本

[root@ansible_nfs cjhli]# cat user_passwd.sh 
#!/ibin/bash

#将第一个位置变量的内容赋值
u_pwd=$1

#统计密码的长度
pwd_l=${#u_pwd}

#判断长度是否大于8
if (( $pwd_l >= 8 ));then
    #判断是否包含数字,大写,特殊字符
    if echo $u_pwd |egrep "[0-9]"|egrep "[A-Z]"|egrep "[^0-Z]" &>/dev/null;then
        echo "符合密码复杂性要求"
    else
        echo "不符合密码复杂性要求"
    fi
else
    echo "密码长度少于8,请重新输入"
fi

[root@ansible_nfs cjhli]# bash user_passwd.sh fdifji
密码长度少于8,请重新输入

[root@ansible_nfs cjhli]# bash user_passwd.sh fivnhbijf1
不符合密码复杂性要求

[root@ansible_nfs cjhli]# bash user_passwd.sh fivnhbijf1A
不符合密码复杂性要求

[root@ansible_nfs cjhli]# bash user_passwd.sh fivnhbijf1A@@
符合密码复杂性要求

什么是正则表达式(regular expression)?
    正则表达式:按照某种正确规则,将字母,数字,特殊符号组合成一个公式,用来表达某个意思

[0-9] 取0-9之间的任意一个字符
[^0-9] 取0-9之间的任意一个字符
[abc] 取a或b或c
[a-z] 取a-z之间的任意一个字符

$ 以什么结尾
^ 以什么开头

[root@ansible_nfs 7-1]# cat /etc/passwd|grep "^gitlab.*sh$"
gitlab-psql:x:993:990::/var/opt/gitlab/postgresql:/bin/sh
gitlab-prometheus:x:992:989::/var/opt/gitlab/prometheus:/bin/sh

[root@ansible_nfs 7-1]# cat /etc/passwd|grep "[1-9]{3}"
[root@ansible_nfs 7-1]# cat /etc/passwd|egrep "[1-9]{3}"
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
zabbix:x:997:995:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
gitlab-www:x:996:993::/var/opt/gitlab/nginx:/bin/false
git:x:995:992::/var/opt/gitlab:/bin/sh
gitlab-redis:x:994:991::/var/opt/gitlab/redis:/bin/false
gitlab-psql:x:993:990::/var/opt/gitlab/postgresql:/bin/sh
gitlab-prometheus:x:992:989::/var/opt/gitlab/prometheus:/bin/sh
mysqlrouter:x:991:988:MySQL Router:/var/lib/mysqlrouter:/bin/false

[root@ansible_nfs 7-1]# awk -F: '$3 ~ /[1-9]{3}/ {print $1,$3}' /etc/passwd
systemd-network 192
polkitd 999
chrony 998
zabbix 997
nfsnobody 65534
gitlab-www 996
git 995
gitlab-redis 994
gitlab-psql 993
gitlab-prometheus 992
mysqlrouter 991

[root@ansible_nfs cjhli]# awk -F: '$4 ~ /[1-9]{4}/ {print $1,$4}' /etc/passwd
nfsnobody 65534
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

韩未零

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值