这里咱们就以存放本机全部用户的/etc/passwd
文件作实例java
[root@localhost ~]# grep -n "root" /etc/passwd //-n表示显示行号 1:root:x:0:0:root:/root:/bin/bash 10:operator:x:11:0:operator:/root:/sbin/nologin
有重复的字符时,可以使用“[ ]”来进行集合匹配,每次只匹配“[ ]”中的一个字符。正则表达式
[root@localhost ~]# grep -n "[fn]tp" /etc/passwd 12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin 27:ntp:x:38:38::/etc/ntp:/sbin/nologin
在“[ ]”中括号中添加“^”表示进行反向选择(有必定的基础的朋友确定知道“^[ ]”表示定位行首,这里“^”内外位置意思将彻底不一样。)bash
[root@localhost ~]# grep -n "^[^root]" /etc/passwd //匹配除了以root开头的全部选项 2:bin:x:1:1:bin:/bin:/sbin/nologin 3:daemon:x:2:2:daemon:/sbin:/sbin/nologin ...... 42:named:x:25:25:Named:/var/named:/sbin/nologin
在正则表达式中一个元字符,因此在这里须要用转义字符“\”将具备特殊意义的字符转化成普通字符。ide
[root@localhost ~]# grep -n '\.$' test.txt 1:he was short and fat. 2:He was wearing a blue polo shirt with black pants. 3:The home of Football on BBC Sport online. 5:google is the best tools for search keyword.
在正则表达式中小数点(.)也是一个元字符,表明任意一个字符。工具
[root@localhost ~]# grep -n "r..t" /etc/passwd //(.)小数点这里表明任一字符 1:root:x:0:0:root:/root:/bin/bash 10:operator:x:11:0:operator:/root:/sbin/nologin 12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
在上述结果中,“root”字符串“r..t”匹配规则。若想要查询 oo、ooo、ooooo 等资料,则须要使用星号(*)元字符。但须要注意的是,“*”表明的是重复零个或多个前面的单字符。“o*”表示拥有零个(即为空字符)或大于等于一个“o”的字符google
[root@localhost ~]# grep -n "oo*" /etc/passwd 1:root:x:0:0:root:/root:/bin/bash 2:bin:x:1:1:bin:/bin:/sbin/nologin 3:daemon:x:2:2:daemon:/sbin:/sbin/nologin 4:adm:x:3:4:adm:/var/adm:/sbin/nologin 5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin ......
例如,查找三到五个 o 的连续字符,这个时候就须要使用基础正则表达式中的限定范围的字符“{ }”。由于“{ }”在 Shell 中具备特殊 意义,因此在使用“{ }”字符时,须要利用转义字符“\”,将“{ }”字符转换成普通字符。spa
[root@localhost ~]# grep -n "0\{2,\}" /etc/passwd //表示中间包含2以上o的字符串 11:games:x:12:100:games:/usr/games:/sbin/nologin 41:zhy:x:1000:1000:zhy:/home/zhy:/bin/bash
此外,grep 命令仅支持基础正则表达式,若是使用扩展正则表达式,须要使用 egrep 或 awk 命令。awk 命令在后面的进行讲解,这里咱们直接使用 egrep 命令。egrep 命令与 grep 命令的用法基本类似。(grep命令能用的egrep命令一样可以使用)code
扩展正则表达式元字符 | 做用 |
---|---|
+ | 做用:重复一个或者一个以上的前一个字符 |
? | 做用:零个或者一个的前一个字符 |
| | 做用:使用或者(or)的方式找出多个字符 |
() | 做用:查找“组”字符串 |
()+ | 做用:辨别多个重复的组 |
[root@localhost ~]# egrep -n "10+" /etc/passwd //使用“+”扩展元字符 11:games:x:12:100:games:/usr/games:/sbin/nologin 31:qemu:x:107:107:qemu user:/:/sbin/nologin 41:zhy:x:1000:1000:zhy:/home/zhy:/bin/bash [root@localhost ~]# egrep -n "10?" /etc/passwd //使用“?”扩展元字符 2:bin:x:1:1:bin:/bin:/sbin/nologin 9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 10:operator:x:11:0:operator:/root:/sbin/nologin 11:games:x:12:100:games:/usr/games:/sbin/nologin [root@localhost ~]# egrep -n 'root|zhy' /etc/passwd //使用“|”扩展元字符 1:root:x:0:0:root:/root:/bin/bash 10:operator:x:11:0:operator:/root:/sbin/nologin 41:zhy:x:1000:1000:zhy:/home/zhy:/bin/bash [root@localhost ~]# egrep -n '(f|n)tp' /etc/passwd //使用“()”扩展元字符,可与“|”一块儿使用 12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin 27:ntp:x:38:38::/etc/ntp:/sbin/nologin
sed是一个很好的文件处理工具,自己是一个管道命令,主要是以行为单位进行处理,能够将数据行进行替换、删除、新增、选取等特定工做ip
1.读取:sed 从输入流(文件、管道、标准输入)中读取一行内容并存储到临时的缓冲区中(又称模式空间,pattern space)。
2.执行:默认状况下,全部的 sed 命令都在模式空间中顺序地执行,除非指定了行的地址,不然 sed 命令将会在全部的行上依次执行。
3.显示:发送修改后的内容到输出流。再发送数据后,模式空间将会被清空。在全部的文件内容都被处理完成以前,上述过程将重复执行,直至全部内容被处理完。字符串
sed [选项] '操做' 参数
sed [选项] -f scriptfile 参数 // scriptfile 表示脚本文件
-e :表示用指定命令或者脚原本处理输入的文本文件。
-f :表示用指定的脚本文件来处理输入的文本文件。
-h :显示帮助。
-n:表示仅显示处理后的结果。
-i:直接编辑文本文件。
a:增长,在当前行下面增长一行指定内容。
c:替换,将选定行替换为指定内容。
d:删除,删除选定的行
i:插入,在选定行上面插入一行指定内容。
p:打印,其一般与“-n”选项一块儿使用
s:替换,替换指定字符。
y:字符转换。
cat
命令:[root@localhost ~]# sed -n 'p' /etc/passwd //效果等同cat命令 root:x:0:0:root:/root:/bin/bash 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 ......
[root@localhost ~]# sed -n '10p' /etc/passwd //输出第10行内容 operator:x:11:0:operator:/root:/sbin/nologin [root@localhost ~]# sed -n '2,4p' /etc/passwd //输出2~4行内容 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
[root@localhost ~]# sed -n 'n;p' /etc/passwd //输出奇数行,偶数行为p;n bin:x:1:1:bin:/bin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync halt:x:7:0:halt:/sbin:/sbin/halt ......
[root@localhost ~]# sed -n '/root/p' /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]# sed -n '/\<root\>/p' /etc/passwd //\< \>表明单词边界 root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin
sed 's/the/THE/' test.txt //将每行中的第一个the 替换为 THE
sed 's/l/L/3' test.txt //将每行中的第 3 个l 替换为L
sed 's/the/THE/g' test.txt //将文件中的全部the 替换为THE
sed 's/o//g' test.txt //将文件中的全部o 删除(替换为空串)
sed 's/^/#/' test.txt //在每行行首插入#:号
sed '/the/s/^/#/' test.txt //在包含the 的每行行首插入#号
sed 's/$/EOF/' test.txt //在每行行尾插入字符串EOF
sed '3,5s/the/THE/g' test.txt //将第 3~5 行中的全部the 替换为 THE
sed '/the/s/o/O/g' test.txt //将包含the 的全部行中的o 都替换为 O
sed '/the/{H;d};$G' test.txt //将包含the 的行迁移至文件末尾,{;}用于多个操做
sed '1,5{H;d};17G' test.txt //将第 1~5 行内容转移至第 17 行后
sed '/the/w out.file' test.txt //将包含the 的行另存为文件out.file
sed '/the/r /etc/hostname' test.txt //将文件/etc/hostname 的内容添加到包含the 的每行之后
sed '3aNew' test.txt //在第 3 行后插入一个新行,内容为 New
sed '/the/aNew' test.txt //在包含the 的每行后插入一个新行,内容为 New
sed '3aNew1\nNew2' test.txt //在第 3 行后插入多行内容,中间的\n 表示换行