Shell 脚本中的 grep sed awk 的用法

grep 文本过滤命令

Global search regular expression and print out the line

全面搜索研究正则表达式并显示出来
grep是一种强大的文本搜索工具,根据用户指定的“模式”对目标文本进行匹配检查,打印匹配到的行
由正则表达式或者字符及基本文字字符所编写的过滤条件
grep -Ei “root|ROOT” passwd

grep 中的正则表达式
^westos 以westos开头的
westos$ 以westos结尾的
‘w…s’=‘w???s’ ‘.’表示单独匹配到一个字符
‘w…’
‘…s’
相关参数
-a 将 binary 文件以 text 文件的方式搜寻数据
-c 计算找到 ‘搜寻字符串’ 的次数
-i 忽略大小写的不同,所以大小写视为相同
-n 顺便输出行号
-v 反向选择,亦即显示出没有 ‘搜寻字符串’ 内容的那一行!
–color=auto 可以将找到的关键词部分加上颜色的显示喔!
实验
将/etc/passwd,有出现 root 的行取出来
grep root /etc/passwd
root❌0:0:root:/root:/bin/bash
operator❌11:0:operator:/root:/sbin/nologin

cat /etc/passwd | grep root
root❌0:0:root:/root:/bin/bash
operator❌11:0:operator:/root:/sbin/nolo

将/etc/passwd,有出现 root 的行取出来,同时显示这些行在/etc/passwd的行号
grep -n root /etc/passwd
1:root❌0:0:root:/root:/bin/bash
30:operator❌11:0:operator:/root:/sbin/nologin

将/etc/passwd,将没有出现 root 的行取出来
[root@client mnt]# grep -v root /etc/passwd
bin❌1:1:bin:/bin:/sbin/nologin
daemon❌2:2:daemon:/sbin:/sbin/nologin
adm❌3:4:adm:/var/adm:/sbin/nologin
lp❌4:7:lp:/var/spool/lpd:/sbin/nologin
sync❌5:0:sync:/sbin:/bin/sync
shutdown❌6:0:shutdown:/sbin:/sbin/shutdown
halt❌7:0:halt:/sbin:/sbin/halt
mail❌8:12:mail:/var/spool/mail:/sbin/nologin
games❌12?games:/usr/games:/sbin/nologin
ftp❌14:50:FTP User:/var/ftp:/sbin/nologin
nobody❌99:99:Nobody:/:/sbin/nologin
dbus❌81:81:System message bus:/:/sbin/nologin
polkitd❌999:998:User for polkitd:/:/sbin/nologin
在这里插入图片描述
grep中字符匹配字数的设定

  • 字符出现零到任意次
  • 字符出现零到任意次
    .* 关键字之间匹配任意字符
    ? 字符出现零到一次
  • 字符出现1到任意次
    {n,m} 字符至少出现n次,至多出现m次
    {,m} 字符出现0到m次
    {n,} 字符出现n以上
    grep -E ‘ro*t’ test //搜索含有0-任意o的以t结尾的
    grep -E ‘ro{1,}’ test //搜索含有1-任意o的
    grep -E ‘ro{1,2}’ test //搜索含有1,2个o的
    grep -E ‘ro{,2}’ test //搜索0-2个o的
    grep -E ‘ro+t’ test //搜索1-任意o的
    grep “ro+t” test //\表示转义
    grep -E ‘(root){1,2}’ test //搜索含有1个或2个连续root的
    grep -E ‘root’ test //搜索含有root的
    grep -E ‘(root){2,}’ test //搜索2个以上root连续的
    grep -E ‘r…t’ test //搜索r和t中间有两个字符的
    grep -E ‘r…t’ test //搜索r和t中间含有3个字符的
    grep -E ‘r?t’ test //字符出现0-一次
    grep -E ‘r.t’ test //搜索r和t中任意字符的
    实验:
    [root@node1 mnt]# vim test
    [root@node1 mnt]# grep -E 'ro
    t’ test //搜索含有0-任意o的以t结尾的
    root
    rootoroot
    rooot
    root
    rootrootrroot
    rootrootrrrootroot
    rot
    rt
    [root@node1 mnt]# grep -E ‘ro{1,}’ test //搜索含有1-任意o的
    root
    rootoroot
    rooot
    root
    rootrootrroot
    rootrootrrrootroot
    rot
    [root@node1 mnt]# grep -E ‘ro{1,2}’ test //搜索含有1,2个o的
    root
    rootoroot
    rooot
    root
    rootrootrroot
    rootrootrrrootroot
    rot
    [root@node1 mnt]# grep -E ‘ro{,2}’ test //搜索0-2个o的
    root
    rootoroot
    rooot
    root
    rootrootrroot
    rootrootrrrootroot
    rot
    rt
    rht
    rst
    [root@node1 mnt]# grep -E ‘ro+t’ test //搜索1-任意o的
    root
    rootoroot
    rooot
    root
    rootrootrroot
    rootrootrrrootroot
    rot
    [root@node1 mnt]# grep -E ‘(root){1,2}’ test //搜索含有1个或2个连续root的
    root
    rootoroot
    root
    rootrootrroot
    rootrootrrrootroot
    [root@node1 mnt]# grep -E ‘root’ test //搜索含有root的
    root
    rootoroot
    root
    rootrootrroot
    rootrootrrrootroot
    [root@node1 mnt]# grep -E ‘(root){2,}’ test //搜索2个以上root连续的
    rootrootrroot
    rootrootrrrootroot
    [root@node1 mnt]# grep -E ‘r…t’ test //搜索r和t中间有两个字符的
    root
    rootoroot
    root
    rootrootrroot
    rootrootrrrootroot
    [root@node1 mnt]# grep -E ‘r…t’ test //搜索r和t中间含有3个字符的
    rooot
    rootrootrroot
    rootrootrrrootroot
    [root@node1 mnt]# grep -E ‘r?t’ test //字符出现0-一次
    root
    rootoroot
    rooot
    root
    rootrootrroot
    rootrootrrrootroot
    rot
    rt
    rht
    rst
    [root@node1 mnt]# grep -E ‘r.*t’ test //搜索r和t中任意字符的
    root
    rootoroot
    rooot
    root
    rootrootrroot
    rootrootrrrootroot
    rot
    rt
    rht
    rst

grep中的字符匹配位置设定
^关键字
关键字$
<关键字
关键字>
<关键字>
实验:
[root@node1 mnt]# grep ^root passwd //找出root开头的行
root❌0:0:root:/root:/bin/bash
root:hello:root
[root@node1 mnt]# grep root$ passwd //找出以root结尾的行
root:hello:root
hello:root:root
[root@node1 mnt]# grep -E ‘r…t>’ test //后面加>,防止搜索做拓展搜索
root
rootoroot
root
rootrootrroot
rootrootrrrootroot
[root@node1 mnt]# grep -E ‘<r…t’ test //前面加<,防止搜索做拓展搜索
root
rootoroot
root
rootrootrroot
rootrootrrrootroot

练习1:写一个脚本利用循环添加用户并配置密码
[root@client mnt]# vim userfile //建立用户文件
[root@client mnt]# cat userfile
user1
user2
user3
[root@client mnt]# vim passwdfile //建立密码文件
[root@client mnt]# cat passwdfile
user123
user234
user345
[root@client mnt]# vim creat_user.sh //编辑脚本
#!/bin/bash
MAX_LINE=$( wc -l $1 | cut -d " " -f 1) //定义一个变量显示行数,用来确定循环次数

for LINEMAX in seq 1 $MAX_LINE //循环
do
USER= ( s e d − n " (sed -n " (sed

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值