linux---gerp文本过滤器和sed行编辑器

一.grep文本过滤命令

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

1.grep匹配字符

grep root passwd                                              ##过滤文件中带有root的

grep ^root passwd                                           ##过滤开头带由root的文件

grep root$ passwd                                           ##过滤结尾带有root的文件

grep -i root passwd                                         ##过率带有root大小写的文件

grep -E “root|ROOT” passwd                        ##展开过滤带有root或者ROOT的

grep   -v   -i -E  “^root|root$”  passwd         ##-v反向过滤


## -E 正则表达式

-a                        ##将binary文件以text文件的方式搜索数据

-c                        ##计算找到‘搜索字符串’的次数

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

-n                        ##顺便输出行号

-v                        ##反向选择,亦即显示出没有‘搜索字符串’内容的哪一行!


grep中字符的匹配位置设置

^关键字              ##关键字开头

关键字$              ##关键字结尾

/<关键字            ##关键字结尾不扩展

关键字/>            ##关键字开头不扩展

/< 关键字/>       ##精确匹配关键字



实验:

1.将passwd文件中,有出现root的行取出来

linux---gerp文本过滤器和sed行编辑器

2..将passwd文件中, 以root开头的文件提取出来

linux---gerp文本过滤器和sed行编辑器


3.编辑passwd文件,加入一行 ,提取出以“root” 大小写开头的行提取出来

linux---gerp文本过滤器和sed行编辑器

linux---gerp文本过滤器和sed行编辑器

4.将passwd文件中,以root开头或者以root结尾的行数提取出来,不区分大小写

linux---gerp文本过滤器和sed行编辑器


2.gerp中字符的匹配次数设定

*                                ##字符出现[0-任意次]

\?                             ##字符出现[0-1次]

\+                             ## 字符出现[1-任意次]

\{n/}                          ##字符出现[n次]

|{m,n\}                       ## 字符出现[最少出现m次,最多出现n次]

\{0,n\}                        ##字符出现[0-n次]

\{m,\}                         ##字符出现[至少m次]

\(xy\)\{n\}xy                ##关键字出现[n次]


实验:

        vim test

linux---gerp文本过滤器和sed行编辑器

      grep 'r..t' test           ##显示以‘r’开头‘t’结尾,中间有两个字符的

linux---gerp文本过滤器和sed行编辑器

      grep 'r...t' test        ##显示以‘r’开头‘t’结尾,中间有三个字符的

linux---gerp文本过滤器和sed行编辑器

      grep 'r....t' test        ##显示以‘r’开头‘t’结尾,中间有四个字符的

linux---gerp文本过滤器和sed行编辑器

      grep -E 'r*t' test       ##搜索r和t中的任意字符

linux---gerp文本过滤器和sed行编辑器

      grep -E 'ro*t' test    ##搜索ro和t中的任意字符

linux---gerp文本过滤器和sed行编辑器

      grep -E 'ro?t' test    ##搜索字符出现0次到1次

linux---gerp文本过滤器和sed行编辑器

     grep -E 'ro{1,}t' test    ##搜索字符出现1次到任意次

linux---gerp文本过滤器和sed行编辑器

     grep -E 'ro{1,3}t' test   ##搜索字符出现1次到3次

linux---gerp文本过滤器和sed行编辑器

     grep -E 'ro{3,}t' test     ##搜索字符出现3次到任意次

linux---gerp文本过滤器和sed行编辑器


     grep -E '(root){2,}' test    ##搜索(root)字符出现2次到任意次

linux---gerp文本过滤器和sed行编辑器

     grep -E 'r.*t' test         ##搜索r和t中任意字符

linux---gerp文本过滤器和sed行编辑器


二.sed行编辑器


stream editor

用来操作纯 ASCII 玛的文本

处理时,把当前处理的行存储在临时缓冲区中,称为 “模式空间”(pattern space)可以指定仅仅处理哪些行 sed 符合模式条件的处理 不符合条件的不予处理

处理完成之后把缓冲区的内容送往屏幕

接着处理下一行,这样不断重复,知道文件末尾

1.sed命令格式:

sed [options] 'command' file(s)

sed [options] -f scriptfile file(s)


2.sed对字符的处理

p           显示

d           删除

a           添加

c           替换

w          写入

i            插入

2.1  p(显示)操作模式

sed -n    ‘/^#/p/’ fstab                        ##显示以"#"开头的行

sed -n    'UUID$/P'  fstab                        ##显示以“UUID”结尾的行

sed -n    '^UUID/p' fstab                        ##显示以“UUID”开头的行

sed -n    '2,6p'   fstab                             ##显示2到6行

sed -n    '2,6!p'  fstab                             ##不显示2到6行


实验:

[[email protected] mnt]# sed -n '/^#/p' /etc/fstab    ##显示以fstab文件中“#”开头的行

linux---gerp文本过滤器和sed行编辑器

[[email protected] mnt]# sed -n '/1$/P' /etc/fstab   ##显示以fstab文件中以“1”结尾的行

linux---gerp文本过滤器和sed行编辑器

[[email protected] mnt]# sed -n '2,6p' /etc/fstab    ##显示以fstab文件中 第2行到第6行

linux---gerp文本过滤器和sed行编辑器

[[email protected] mnt]# sed -n '2,6!p' /etc/fstab    ##不显示fstab文件中的第2行和第6行

linux---gerp文本过滤器和sed行编辑器

[[email protected] mnt]# sed -n '2p;6p' /etc/fstab    ##显示以fstab文件中第2行和第6行

linux---gerp文本过滤器和sed行编辑器


2.2 d(删除)操作模式

r[email protected] mnt]# cat -n fstab | sed '/^UUID/d' fstab  (显示fstab删除以UUID开头的那一行)


linux---gerp文本过滤器和sed行编辑器


[r[email protected] mnt]# cat -n fstab | sed '/^#/d' fstab  (显示fstab删除以#开头剩下的行数)

[email protected] mnt]# cat -n fstab | sed '/^UUID/!d' fstab  (显示删除除了UUID开头的那一行,剩下的,除了UUID开头的都删除)

linux---gerp文本过滤器和sed行编辑器


[[email protected] mnt]# cat -n fstab | sed '/^$/d' fstab

linux---gerp文本过滤器和sed行编辑器


2.3 a(添加)操作模式、c(改变)操作模式、

[[email protected] mnt]# cat westos

 linux---gerp文本过滤器和sed行编辑器

[[email protected] mnt]# sed '/hello/aworld' westos  (显示添加一行world)

linux---gerp文本过滤器和sed行编辑器


[[email protected] mnt]# sed '/hello/ahello world' westos (添加以行hello world)

linux---gerp文本过滤器和sed行编辑器

[[email protected] mnt]# sed 's/hello/hello world/g' westos  (将hello 变成helloworld)

linux---gerp文本过滤器和sed行编辑器

[[email protected] mnt]# sed '/hello/aworld\nwestos' westos

linux---gerp文本过滤器和sed行编辑器

[email protected] mnt]# sed '/hello/aworld\nwestos\nhehe' westos

linux---gerp文本过滤器和sed行编辑器


2.4sed的其他用法

[email protected] mnt]# sed '/^UUID/=' fstab  (显示fstab里边以UUID开头的那一行并且显示所有过程和结果)

linux---gerp文本过滤器和sed行编辑器

[[email protected] mnt]# sed -n '/^UUID/=' fstab  (显示fstab里以UUID开头的输出结果)

linux---gerp文本过滤器和sed行编辑器

[[email protected] mnt]# sed -ne '/^UUID/p' -e '/^UUID/=' fstab  (显示UUID开头这一行的结果和行)

linux---gerp文本过滤器和sed行编辑器

[email protected] mnt]# sed -e 's/See/green/;s/by/hehe/' fstab ( 将fstab中的See改为greenby改为hehe并且输出全部的

linux---gerp文本过滤器和sed行编辑器

[email protected] mnt]# sed 'G' fstab  (将fstab变为每行空一行输出,包括最后以行完毕也空一行)

linux---gerp文本过滤器和sed行编辑器

[[email protected] mnt]# sed '$!G' fstab  (将fstab变为每行空一行输出,最后一行结束之后不空行)

linux---gerp文本过滤器和sed行编辑器

[[email protected] mnt]# sed '=' fstab | sed 'N;s/\n/ /' (将换行符号改为空格,并标出行数输出)

linux---gerp文本过滤器和sed行编辑器

[[email protected] mnt]# sed -n '$p' fstab   (输出最后一行)

linux---gerp文本过滤器和sed行编辑器

 

2.5 w(写入)操作模式

[[email protected] mnt]# sed '/^UUID/w file' fstab  (将fstab文件中UUID开头的一行输入到file文件中,并显示fstab内容。)

 linux---gerp文本过滤器和sed行编辑器

[[email protected] mnt]# sed -n '/^UUID/w file1' fstab  (不显示输出过程,但是输入到file1中)

linux---gerp文本过滤器和sed行编辑器

[[email protected] mnt]# sed  '6r file' fstab  (将file文件中的内容添加到fstab文件第六行的后边)

linux---gerp文本过滤器和sed行编辑器


2.6   i操作模式

s //g (g表示全文 没g,表示替换每一行的第一个)

 

sed 's/nologin/hehe/g' passwd  (将passwd文件里的所有nologin替换为hehe,但是这种替换不改变源文件)

linux---gerp文本过滤器和sed行编辑器

[[email protected] mnt]# sed '3,5s/nologin/hehe/g' passwd (将passwd文件中的第三行到第五行的所有nologin替换为hehe)

linux---gerp文本过滤器和sed行编辑器

[[email protected] mnt]# sed '/adm/,/sync/s/nologin/westos/g' passwd  (重adm行到sync行的所有nologin替换为hehe)

linux---gerp文本过滤器和sed行编辑器

[[email protected] mnt]# sed -e '/adm/,/sync/s/nologin/hehe/g;s/sbin/xixixi/g' passwd  (将adm行到sync行所有的nologin替换为hehe,并且 将所有的sbin替换为xixixi)

 linux---gerp文本过滤器和sed行编辑器

[[email protected] mnt]# vim file

linux---gerp文本过滤器和sed行编辑器

s/sbin/hehe/g   将所有的sbin替换为hehe

s/root/xixi/g   将所有的root替换为xixi

[[email protected] mnt]# sed -f file passwd

 linux---gerp文本过滤器和sed行编辑器

[[email protected] mnt]# sed -f file -i passwd  (i操作模式并不会改变passwd文件的内容,-i时保存passwd文件的内容)

linux---gerp文本过滤器和sed行编辑器

 

[[email protected] mnt]# sed  -i 's/nologin/hehe/g' passwd   (-i一样可一保存文件内容)

linux---gerp文本过滤器和sed行编辑器

[[email protected] mnt]# cat passwd