一:正则表达式grep
-a 不要忽略二进制数据。
-A<显示列数> 除了显示符合范本样式的那一行之外,并显示该行之后的内容。
-b 在显示符合范本样式的那一行之外,并显示该行之前的内容。
-c 计算符合范本样式的列数。
-C<显示列数>或-<显示列数> 除了显示符合范本样式的那一列之外,并显示该列之前后的内容。
-d<进行动作> 当指定要查找的是目录而非文件时,必须使用这项参数,否则grep命令将回报信息并停止动作。-e<范本样式> 指定字符串作为查找文件内容的范本样式。
-E 将范本样式为延伸的普通表示法来使用,意味着使用能使用扩展正则表达式。
-f<范本文件> 指定范本文件,其内容有一个或多个范本样式,让grep查找符合范本条件的文件内容,格式为每一列的范本样式。
-F 将范本样式视为固定字符串的列表。
-G 将范本样式视为普通的表示法来使用。
-h 在显示符合范本样式的那一列之前,不标示该列所属的文件名称。
-H 在显示符合范本样式的那一列之前,标示该列的文件名称。
-i 忽略字符大小写的差别。
-l 列出文件内容符合指定的范本样式的文件名称。
-L 列出文件内容不符合指定的范本样式的文件名称。
-n 在显示符合范本样式的那一列之前,标示出该列的编号。
-q 不显示任何信息。
-R/-r 此参数的效果和指定"-d recurse"参数相同。
-s 不显示错误信息。
-v 反转查找。
-w 只显示全字符合的列。
-x 只显示全列符合的列。
-y 此参数效果跟"-i"相同。
-o 只输出文件中匹配到的部分。
1:正则表达式概念
正则表达式是使用单个字符串来描述、匹配一系列符合某个句法规则的字符串,简单来说,
是一种匹配字符串的方法,通过一些特殊符号,实现快速查找、删除、替换某个特定字符串。
2:基础正则表达式
基础正则表达式是常用正则表达式最基础的部分。在 Linux 系统中常见的文件
处理工具中 grep 与 sed 支持基础正则表达式,而 egrep 与 awk 支持扩展正则表达式。
3:创建测试文件
root@localhost \~\]#****cat test.txt**** he was short and fat. he was weating a blue polo shirt with black pants. The home of Football on BBC Sport online. the tongue is boneless but it breaks bones.12! google is the best tools for search keyword. PI=3.14 a wood cross! Actions speak louder than words
#woood #
#woooooooood #
AxyzxyzxyzxyzC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.
3:查找特定字符
(1)查看包含the的行
root@localhost \~\]# ****grep -ni 'the' test.txt**** 3:The home of Football on BBC Sport online. 4:the tongue is boneless but it breaks bones.12! 5:google is the best tools for search keyword.
注释:
-n:显示行号
-i:不区分大小写
-v:不包含指定字符
(2)利用[ ]查找集合字符
root@localhost \~\]# ****grep -n 'sh\[io\]rt' test.txt**** 1:he was short and fat. 2:he was weating a blue polo shirt with black pants.
注释:
\]:中括号内不管写几个字符,都只匹配一个,表示匹配其中的任何一个字符
(3)查找字母oo前不是字母w的内容
> \[root@localhost \~\]#****grep -n '\[\^w\]oo' test.txt****
>
> 3:The home of Football on BBC Sport online.
>
> 5:google is the best tools for search keyword.
>
> 9:#woood #
>
> 10:#woooooooood #
>
> 12:I bet this place is really spooky late at night!
(4)查看字母oo前不是小写字母的内容
> \[root@localhost \~\]# ****grep -n '\[\^a-z\]oo' test.txt****
>
> 3:The home of Football on BBC Sport online.
注释:字母前直接用\^,表示取反
\[ \]前用\^,表示以括号中的字符开头
### **4:查找行首与行位**
(1)查看以the为行首的行
> \[root@localhost \~\]# ****grep -n '\^the' test.txt****
>
> 4:the tongue is boneless but it breaks bones.12!
(2)查询以小写字母开头的行
> \[root@localhost \~\]# ****grep -n '\^\[a-z\]' test.txt****
>
> 1:he was short and fat.
>
> 2:he was weating a blue polo shirt with black pants.
>
> 4:the tongue is boneless but it breaks bones.12!
>
> 5:google is the best tools for search keyword.
>
> 7:a wood cross!
(3)查询以大写字母开头的行
> \[root@localhost \~\]#****grep -n '\^\[A-Z\]' test.txt****
>
> 3:The home of Football on BBC Sport online.
>
> 6:PI=3.14
>
> 8:Actions speak louder than words
>
> 11:AxyzxyzxyzxyzC
>
> 12:I bet this place is really spooky late at night!
>
> 13:Misfortunes never come alone/single.
>
> 14:I shouldn't have lett so tast.
(4)查看以非字母开头的行
> \[root@localhost \~\]# ****grep -n '\^\[\^a-zA-Z\]' test.txt****
>
> 9:#woood #
>
> 10:#woooooooood #
(5)查看以点结尾的行
> \[root@localhost \~\]# ****grep -n '\\.$' test.txt****
>
> 1:he was short and fat.
>
> 2:he was weating 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.
>
> 13:Misfortunes never come alone/single.
>
> 14:I shouldn't have lett so tast.
(5)查询空行
> \[root@localhost \~\]# ****grep -n '\^$' test.txt****
注释:查询非空行
> \[root@localhost \~\]# ****grep -n -v '\^$' test.txt****
### **5:查找任意字符和重复字符**
(1)查找包含四字符的单词的行,单词以w开头,以d结尾
> \[root@localhost \~\]#****grep -n 'w..d' test.txt****
>
> 5:google is the best tools for search keyword.
>
> 7:a wood cross!
>
> 8:Actions speak louder than words
注释:
一个点代表一个字符
(2)查询至少包含两个字母o(oo)字符串的行
> \[root@localhost \~\]# ****grep -n 'ooo\*' test.txt****
>
> 3:The home of Football on BBC Sport online.
>
> 5:google is the best tools for search keyword.
>
> 7:a wood cross!
>
> 10:#woood #
>
> 11:#woooooooood #
>
> 13:I bet this place is really spooky late at night!
注释:
ooo\*:前两个o是条件,表示包含两个o;然后是o\*,表示后面有零个或多个重复o
(3)查找行,行中单词包含w开头和d结尾,中间至少一个字母o
> \[root@localhost \~\]# ****grep -n 'woo\*d' test.txt****
>
> 7:a wood cross!
>
> 10:#woood #
>
> 11:#woooooooood #
(4)查询以w开头,d结尾,中间字符可有可无
> \[root@localhost \~\]# ****grep -n 'w.\*d' test.txt****
>
> 1:he was short and fat.
>
> 5:google is the best tools for search keyword.
>
> 7:a wood cross!
>
> 8:Actions speak louder than words
>
> 10:#woood #
>
> 11:#woooooooood #
(5)查询包含数字的行
> \[root@localhost \~\]# ****grep -n '\[0-9\]\[0-9\]\*' test.txt****
>
> 4:the tongue is boneless but it breaks bones.12!
>
> 6:PI=3.14
### **6:查找连续字符范围**
(1)查询包含两个o的字符
> \[root@localhost \~\]# ****grep -n 'o\\{2\\}' test.txt****
>
> 3:The home of Football on BBC Sport online.
>
> 5:google is the best tools for search keyword.
>
> 7:a wood cross!
>
> 10:#woood #
>
> 11:#woooooooood #
>
> 13:I bet this place is really spooky late at night!
注释:
'o\\{2\\}':表示两个字母o
(2)w开头,d结尾中间有2--5个o
> \[root@localhost \~\]# ****grep -n 'wo\\{2,5\\}d' test.txt****
>
> 7:a wood cross!
>
> 10:#woood #
(3)w开头,d结尾中间有2个以上o
> \[root@localhost \~\]# ****grep -n 'wo\\{2,\\}d' test.txt****
>
> 7:a wood cross!
>
> 10:#woood #
>
> 11:#woooooooood #
### **7:扩展正则表达式**
> \[root@localhost \~\]#****grep -v '\^$' /etc/ssh/sshd_config \| grep -v '\^#'****
用扩展正则表达式表示:
> \[root@localhost \~\]#****egrep -v '\^$\|\^#' /etc/ssh/sshd_config****
## **二:文本处理器sed**
sed(Stream EDitor)是一个强大而简单的文本解析转换工具,可以读取文本,并根据
指定的条件对文本内容进行编辑(删除、替换、添加、移动等),最后输出所有行或者仅输
出处理的某些行。sed 也可以在无交互的情况下实现相当复杂的文本处理操作,被广泛应用
于 Shell 脚本中,用以完成各种自动化处理任务。
sed 的工作流程主要包括读取、执行和显示三个过程。
* 读取:sed 从输入流(文件、管道、标准输入)中读取一行内容并存储到临时的缓冲区中(又称模式空间,pattern space)。
* 执行:默认情况下,所有的 sed 命令都在模式空间中顺序地执行,除非指定了行的地址,否则 sed 命令将会在所有的行上依次执行。
* 显示:发送修改后的内容到输出流。在发送数据后,模式空间将会被清空。
### **1:sed常用选项**
-n :使用安静(silent)模式。在一般 sed 的用法中,所有来自 STDIN 的数据一般都会被列出到终端上。但如果加上 -n 参数后,则只有经过sed 特殊处理的那一行(或者动作)才会被列出来。
-e :直接在命令列模式上进行 sed 的动作编辑;
-f :直接将 sed 的动作写在一个文件内, -f filename 则可以运行 filename 内的 sed 动作;
-r :sed 的动作支持的是延伸型正规表示法的语法。(默认是基础正规表示法语法)
-i :直接修改读取的文件内容,而不是输出到终端。
### **2:sed常用操作**
a :新增行, a 的后面可以是字串,而这些字串会在新的一行出现(目前的下一行)
c :取代行, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行
d :删除行,因为是删除,所以 d 后面通常不接任何参数,直接删除地址表示的行;
i :插入行, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
p :打印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行
s :替换,可以直接进行替换的工作,通常这个 s 的动作可以搭配正规表示法,例如 1,20s/old/new/g 一般是替换符合条件的字符串而不是整行
y:字符转换
### **3:输出符合条件的文本**
(1)输出所有内容
> \[root@localhost \~\]# ****sed -n 'p' test.txt****
(2)输出第三行
> \[root@localhost \~\]# ****sed -n '3p' test.txt****
(3)输出3\~5行
> \[root@localhost \~\]# ****sed -n '3,5p' test.txt****
(4)输出所有奇数行
> \[root@localhost \~\]#****sed -n 'p;n' test.txt****
(5)输出所有偶数行
> \[root@localhost \~\]# ****sed -n 'n;p' test.txt****
(6)输出第1\~5行之间的奇数行
> \[root@localhost \~\]# ****sed -n '1,5{p;n}' test.txt****
(7)输出第10行至文件尾之间的偶数行
> \[root@localhost \~\]# ****sed -n '10,${n;p}' test.txt****
注释:
此命令中,读取的第一行是文件的第10行,读取的第二行,是文件的第11行,依次类推
(8)输出包含the的行
> \[root@localhost \~\]# ****sed -n '/the/p' test.txt****
(9)输出从第4行开始至第一个包含the的行
> \[root@localhost \~\]#****sed -n ' 4,/the/p' test.txt****
(10)输出包含the的行所在的行号
> \[root@localhost \~\]# ****sed -n '/the/=' test.txt****
注释:
=用来输出行号
(11)输出以PI开头的行
> \[root@localhost \~\]#****sed -n '/\^PI/p' test.txt****
(12)输出包含单词wood的行
> \[root@localhost \~\]# ****sed -n '/\\\