#grep文本过滤命令

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

##1、grep匹配字符

#####格式:

| grep | 匹配条件| 处理文本 | 解释 |
| ------------- |:-------------? -----?
| grep | root | passwd | //把passwd含有root的行导出来 |
| grep | ^root | passwd| //找出root开头的行
| grep | rootgrep只过滤出某一行_linux”| passwd | //过滤以root开头或者以root结尾的行,|表示或
| grep | -E -v “^root| root$” | passwd | //-v 反向过滤
###### ## -E 正则表达式

#####相关参数:

-a

将 binary 文件以 text 文件的方式搜寻数据

-c

计算找到 ‘搜寻字符串’ 的次数

-i

忽略大小写的不同,所以大小写视为相同

-n

顺便输出行号

-v

反向选择,亦即显示出没有 ‘搜寻字符串’ 内容的那一行!

–color=auto

可以将找到的关键词部分加上颜色的显示喔!

#####实验:
######1.将/etc/passwd,有出现 root 的行取出来

# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
或
# cat /etc/passwd | grep root 
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nolo

######2.将/etc/passwd,有出现 root 的行取出来,同时显示这些行在/etc/passwd的行号

# grep -n root /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
30:operator:x:11:0:operator:/root:/sbin/nologin

######3.将/etc/passwd,将没有出现 root 的行取出来

[root@client mnt]# 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
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin

grep只过滤出某一行_正则表达式_02


##2、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

##3、grep中字符的匹配位置设定

^关键字            关键词开头
关键字$            关键结尾
<关键字           关键字结尾不扩展
>关键字            关键字开头不扩展
<关键字>\        精确匹配关键字

实验:

[root@node1 mnt]# grep ^root passwd  //找出root开头的行
root:x: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=$(sed -n "${LINEMAX}P" $1)   // 截取当前循环行的数据
        PASSWD=$(sed -n "${LINEMAX}P" $2)
        useradd "$USER"                     // 添加用户
        echo "$PASSWD" | passwd --stdin "$USER"
done
[root@client mnt]# sh  creat_user.sh userfile passwdfile   //运行脚本
Changing password for user user1.
passwd: all authentication tokens updated successfully.
Changing password for user user2.
passwd: all authentication tokens updated successfully.
Changing password for user user3.
passwd: all authentication tokens updated successfully.

grep只过滤出某一行_运维_03


grep只过滤出某一行_运维_04


grep只过滤出某一行_linux_05


#####练习2:写一个脚本,显示系统中存在的本地用户

#####思路:利用用户使用的shell(在/etc/shells文件中,除去匿名用户的)在/etc/passwd文件中去匹配,在截取对应的行数据。

#!/bin/bash   //脚本内容
SHELL=$(echo `grep -v nologin /etc/shells` | sed 's/ /|/g')
grep -E "$SHELL" /etc/passwd | cut -d : -f 1

grep只过滤出某一行_grep只过滤出某一行_06


grep只过滤出某一行_linux_07