grep文本过滤器与sed编辑器

grep

grep 的格式 
grep [参数] 匹配条件 处理文件 
主要参数 [ ]: 
-c : 只输出匹配的行 
-i : 不区分大小写 
-h : 查询多文件时不显示文件名 
-l : 查询多文件时, 只输出包含匹配字符的文件名 
-n : 显示匹配的行号及行 
-v : 显示不包含匹配文本的所有行,即反转查找 
-E: 将范本样式为延伸的普通表示法来使用,意味着能使用扩展正则表达式
grep       匹配条件        处理文件
           root         passwd                   匹配passwd中含有root的行
          ^root         passwd                   匹配passwd中以root开始的行
           root$       passwd                   匹配passwd中以root结尾的行
          -i root       passwd                   匹配passwd中含有root的行  不区分大小写
     -E "root|ROOT"     passwd                   扩展正则表达式,相当于 egrep,| 表示或
     -v root            passwd                   反向过滤

例如

[root@desktop mnt]# cp /etc/passwd /mnt/
[root@desktop mnt]# ls
passwd
[root@desktop mnt]# grep root passwd         ##匹配passwd中含有root的行
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

[root@desktop mnt]# grep ^root passwd        ##匹配passwd中以root开始的行
root:x:0:0:root:/root:/bin/bash
[root@desktop mnt]# vim passwd 
在文件后面添加test:test:root
test:root:test
ROOT:test:test

[root@desktop mnt]# grep ^root passwd           ####匹配passwd中含有root的行
root:x:0:0:root:/root:/bin/bash

[root@desktop mnt]# grep root$ passwd           ## 匹配passwd中以root结尾的行
test:test:root

[root@desktop mnt]# grep -i ^root passwd           ####匹配passwd中以root开始的行,不区分大小写
root:x:0:0:root:/root:/bin/bash
ROOT:test:test

[root@desktop mnt]# grep -i root$ passwd         ####匹配passwd中以root结尾的行,不区分大小写
test:test:root

[root@desktop mnt]# grep -i -E "root|root$" passwd     
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
test:test:root
test:root:test
ROOT:test:test

grep 中的正则表达式
^westos
westos$
‘w….s’
‘w…..’
‘…..s’

[root@desktop mnt]# vim shi
[root@desktop mnt]# cat shi 
root
roooot
rootroot
rotroot
rt
rooooooooooot
rooooooort
rot

[root@desktop mnt]# grep '^ro' shi          ##匹配shi中以ro开始的行
root
roooot
rootroot
rotroot
rooooooooooot
rooooooort
rot

[root@desktop mnt]# grep 'ro$' shi       ##匹配shi中以ro结尾的行

[root@desktop mnt]# grep 'r..t' shi      ##匹配shi中含有r加两个字符t的行
root
rootroot
rotroot

[root@desktop mnt]# grep 'r...' shi      ##匹配shi中含有r加三个字符个字符的行
root
roooot
rootroot
rotroot
rooooooooooot
rooooooort

[root@desktop mnt]# grep '....t' shi        ##匹配shi中含有三个字符加t的行
roooot
rootroot
rotroot
rooooooooooot
rooooooort
grep中字符的匹配次数设定
*           字符出现0-任意次
?           字符出现0-1次
+           字符出现1-任意此
{n}     字符出现n次
{m,n}       字符出现最少m次,最多n次
{0,n}       字符出现0-n次
{m,}        字符最少出现m次
(xy\)\{n\}xy    关键字出现n次
*       关键字之间匹配任意字符
[root@desktop mnt]# grep -E 'r*t' shi    ##匹配shi中含有r加任意字符t的行
root
roooot
rootroot
rotroot
rt
rooooooooooot
rooooooort
rot

[root@desktop mnt]# grep -E 'ro*t' shi     ##匹配shi中含有ro加任意字符t的行
root
roooot
rootroot
rotroot
rt
rooooooooooot
rooooooort
rot

[root@desktop mnt]# grep -E 'ro?t' shi      ##匹配shi中含有ro加零或一个字符t的行
rotroot
rt
rooooooort
rot

[root@desktop mnt]# grep -E 'ro{1,}t' shi    ##匹配shi中含有ro加最少一个字符t的行
root
roooot
rootroot
rotroot
rooooooooooot
rot

[root@desktop mnt]# grep -E 'ro{1,3}t' shi     ##匹配shi中含有ro加最少一个字符,最多三个字符t的行
root
rootroot
rotroot
rot

[root@desktop mnt]# grep -E 'ro{,3}t' shi       ##匹配shi中含有ro加最多三个字符t的行
root
rootroot
rotroot
rt
rooooooort
rot

[root@desktop mnt]# grep -E 'r*t' shi      ##匹配shi中含有r加任意字符t的行
root
roooot
rootroot
rotroot
rt
rooooooooooot
rooooooort
rot

grep 中字符的匹配位置设定
^ 关键字
关键字 $
\< 关键字
关键字 >
\<关键字>

[root@desktop mnt]# grep -E "r...\>" shi       ##匹配shi中以r加三个字符结尾的行
root
rootroot
rotroot

[root@desktop mnt]# grep -E "...t\>" shi      ##匹配shi中以三个字符加t结尾的行
root
roooot
rootroot
rotroot
rooooooooooot
rooooooort

[root@desktop mnt]# grep -E "\<...t\>" shi       ##匹配shi中只含有三个任意字符加t的行
root







grep "ro\+t" shi与grep -E "ro+t" shi等同 建议用-E
-------------------------------------------


[root@desktop mnt]# grep  "ro\+t" shi    ##匹配shi中以ro加任意字符t的行
root
roooot
rootroot
rotroot
rooooooooooot
rot
[root@desktop mnt]# grep -E "ro+t" shi     ##匹配shi中以ro加任意字符t的行
root
roooot
rootroot
rotroot
rooooooooooot
rot

sed 行编辑器

sed 命令格式
两种形式
sed [options] ‘command’ file(s)
sed [options] -f scriptfile file(s)
对字符的处理
p 显示
d 删除
a 添加
c 替换
w 写入
i 插入

p 模式

这里写图片描述
sed -n ‘/^#/p’ fstab 显示以 # 号开头的行
这里写图片描述
sed -n ‘/^#/!p’ fstab 显示不以 # 号开头的行
这里写图片描述
sed -n ‘/0$/!p’ fstab 显示不以 0 结尾的行
这里写图片描述
cat -n fstab | sed -n ‘2,6p’显示 2-6 行
这里写图片描述
cat -n fstab | sed -n -e ‘2p’ -e ‘6p’与 cat -n fstab | sed -n -e ‘2p;6p’
显示第 2、第 6 行
这里写图片描述
cat -n fstab | sed -n -e ‘2!p’显示除了第 2 行的其他行
这里写图片描述
cat -n fstab | sed -ne ‘2!p;6!p’ | uniq -d 显示除了第 2、第 6 行的其他行
这里写图片描述
cat -n fstab | sed -n -e ‘2,6!p’不显示 2-6 行

d 模式

cat -n fstab | sed -e ‘2d;6d’删除第 2、第 6 行后再显示
这里写图片描述
cat -n fstab | sed -e ‘2,6d’删除第 2-6 行后再显示
这里写图片描述
sed -e ‘/^#/d’ fstab 删除以 # 号开头的行后再显示
这里写图片描述
sed -e ‘/^$/d’ fstab 删除空行后再显示
这里写图片描述
sed -e ‘/^$/d;/^#/d’ fstab 删除空行和以 # 号开头的行后再显示
这里写图片描述
sed -e ‘/UUID/!d’ fstab<==>sed -e ‘/^UUID/!d’ fstab
除了含有 UUID 的行(以 UUID 开头的行)不删除,其余的删除后再显示
这里写图片描述
sed -e ‘/UUID/d’ fstab 删除含有 UUID 的行后再显示
这里写图片描述

a 模式


sed '/hello/aworld' westos添加 world 到文件 westos(默认添加到最后行) 
sed 's/hello/hello world/g' westos全局替换,将 hello 替换成 hello world 
sed '/hello/a\world' westos等价于sed '/hello/aworld' westos 
sed '/hello/aworld westos' westos添加 hello world 到文件 westos 
sed '/hello/aworld\nwestos' westos 
添加 world westos 到文件 westos ,world 后有换行符,所以 westos 换行显示 
sed '/hello/aworld\n westos' westos 
world 后有换行符且有一空格,所以 westos 换行并空格显示 

这里写图片描述

c 模式

sed '/hello/chello world' westos将文件 westos 中的 hello 替换成 hello world 
sed '/hello/ci\nlike\nyou' westos将文件 westos 中的 hello 替换成 i like you 并换行显示 

这里写图片描述

w 模式

这里写图片描述
sed -n ‘/bash / p p a s s w d > f i l e s e d n / b a s h /wfile1’ passwd
这里写图片描述

sed ‘/hello/=’ westos
sed ‘/hello/=’ -i westos
参数 -i :直接修改读取的文件内容,而不是输出到终端。
这里写图片描述
将内容一行显示
这里写图片描述
换行
这里写图片描述
sed ‘6r westos’ fstab
在文件 fstab 的第 6 行后添加上文件 westos 的内容后显示
这里写图片描述
sed ‘$r westos’ fstab
在文件 fstab 的最后一行后添加上文件 westos 的内容后显示
这里写图片描述
sed ‘1r westos’ fstab 在文件 fstab 的第 1 行后添加上文件 westos 的内容后显示
这里写图片描述
sed -n ‘/^UUID/=’ fstab
sed ‘/^UUID/=’ fstab
这里写图片描述

sed 的其他用法 
此处只演示sed -f filename1 filename2,其余可自己测试
sed -f filename1 filename2
sed -n -e '/^UUID/p' -e '/^UUID/=' filename
sed -e 's/sbin/linux/;s/bash/westos/' filename
sed 's/^\//#/' filename
sed 's@^/@#@g' filename
sed 's/\//#/' filename
sed 's/\//#/g/' filename
sed 'G' filename
sed '$!G' filename
sed '=' filename | sed 'N;s/\n/ /'
sed -n '$p' filename

这里写图片描述
这里写图片描述
这里写图片描述

猜你喜欢

转载自blog.csdn.net/awoyaoc/article/details/80760794