shell脚本应用练习(4)

摘要:
除了显示与模板样式匹配的行之外,还指定一个字符串作为查找文件内容的模板样式。模板文件>让grep查找符合模板条件的文件内容,-L列出其文件内容不符合指定模板样式的文件名。查找特定字符(1)查看包含[root@localhost~]#grep-nith测试.txt3:

一:正则表达式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:基础正则表达式

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.

4:查找特定字符

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.

5:查找行首与行位

1)查看以the为行首的行

[root@localhost ~]# grep -n '^the' test.txt

4:the tongue is boneless but it breaks bones.12!

2)查询以小写字母开头的行

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!

9:woood #

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

6:查找任意字符和重复字符

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)查询至少包含两个字母ooo)字符串的行

[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

 

7:查找连续字符范围

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

2w开头,d结尾中间有2--5o

[root@localhost ~]# grep -n 'wo{2,5}d' test.txt

7:a wood cross!

10:#woood #

3w开头,d结尾中间有2个以上o

[root@localhost ~]# grep -n 'o{2,5}d' test.txt

7:a wood cross!

10:#woood #

11:#woooooooood #

二:文本处理器sed

1sed常用选项

-n :使用安静(silent)模式。在一般 sed 的用法中,所有来自 STDIN 的数据一般都会被列出到终端上。但如果加上 -n 参数后,则只有经过sed 特殊处理的那一行(或者动作)才会被列出来。
-e :直接在命令列模式上进行 sed 的动作编辑;
-f :直接将 sed 的动作写在一个文件内, -f filename 则可以运行 filename 内的 sed 动作;
-r sed 的动作支持的是延伸型正规表示法的语法。(默认是基础正规表示法语法)
-i :直接修改读取的文件内容,而不是输出到终端。

2sed常用操作

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 '/<wood>/p' test.txt

 

4:删除符合条件的文本

1)删除第3

[root@localhost ~]# nl test.txt | sed '3d' ##显示行号

[root@localhost ~]# sed '3d' test.txt ##不显示行号

2)删除3~5

[root@localhost ~]# nl test.txt |sed '3,5d'

3)删除包含cross的行

[root@localhost ~]# nl test.txt |sed '/cross/d'

注释:删除不包含cross的行

[root@localhost ~]# nl test.txt |sed '/cross/! d'

4)删除以小写字母开头的行

[root@localhost ~]# sed '/^[a-z]/d' test.txt

5)删除以点结尾的行

[root@localhost ~]# sed '/.$/d' test.txt

6)删除空行

[root@localhost ~]# sed '/^$/d' test.txt

5:替换符合条件的文本

1)将每行的第一个the换成THE

[root@localhost ~]# sed 's/the/THE/' test.txt

2)将每行中的第2l换成L

[root@localhost ~]# sed 's/l/L/2' test.txt

3)将文中所有的the换成THE

[root@localhost ~]# sed 's/the/THE/g' test.txt

4)将文中所有的o删除

[root@localhost ~]# sed 's/o//g' test.txt

5)在每行的行首插入#

[root@localhost ~]# sed 's/^/#/' test.txt

注释

在每行行尾添加#

[root@localhost ~]# sed 's/$/#/' test.txt

6)在包含the的每行的行首插入#

[root@localhost ~]# sed '/the/s/^/#/' test.txt

7)在每行的行尾插入字符串EOF

[root@localhost ~]# sed 's/$/EOF/' test.txt

8)将第3~5行中的所有the替换成THE

[root@localhost ~]# sed '3,5s/the/THE/g' test.txt

9)将包含the的所有行中的o都替换成O

[root@localhost ~]# sed '/the/s/o/O/g' test.txt

5:迁移符合条件的文本

H:复制到剪切板

g:将剪切板中的内容覆盖到指定行

G:将剪切板中的内容追加到指定行

w:保存文件

r:读取指定文件

a:追加指定内容

1)将包含the的行迁移至文件的末尾

[root@localhost ~]# sed '/the/{H;d};$G' test.txt

2)将第1~5行的内容转移至第17行后

[root@localhost ~]# sed '1,5{H;d};17G' test.txt

3)将包含the的行另存为文件out.txt

[root@localhost ~]# sed '/the/w out.txt' test.txt

4)将文件/etc/hostname的内容添加到包含the的每一行后

[root@localhost ~]# sed '/the/r /etc/hostname' test.txt

5)在第3行后插入一个新行,内容为#chkconfig:35 82 20

[root@localhost ~]# sed '3a#chkconfig:35 82 20' test.txt

6)在包含the的每行后插入一个新行,内容为New

[root@localhost ~]# sed '/the/aNew' test.txt

7)在第3行后插入多行内容

[root@localhost ~]# sed '3aNew1 New2' test.txt

注释:为换行,添加两行为New1New2

三:文本处理器awk

1awk常见用法

$0           表示整个当前行

$1           每行第一个字段

NF          字段数量变量

NR          每行的记录号,多文件记录递增

FNR        NR类似,不过多文件记录不递增,每个文件都从1开始

           制表符

          换行符

FS          BEGIN时定义分隔符

RS       输入的记录分隔符, 默认为换行符(即文本是按一行一行输入)

~            匹配,与==相比不是精确比较

!~           不匹配,不精确比较

==         等于,必须全部相等,精确比较

!=           不等于,精确比较

&&      逻辑与

||             逻辑或

+            匹配时表示1个或1个以上

/[0-9][0-9]+/   两个或两个以上数字

/[0-9][0-9]*/    一个或一个以上数字

FILENAME 文件名

OFS      输出字段分隔符, 默认也是空格,可以改为制表符等

ORS        输出的记录分隔符,默认为换行符,即处理结果也是一行一行输出到屏幕

-F'[:#/]'   定义三个分隔符

2awk案例

awk -F":" '{print}' /etc/passwd   //输出所有

awk -F":" '{print $0}' /etc/passwd    //输出所有

awk -F: 'NR==3,NR==6{print}' /etc/passwd    //显示第36

awk -F: 'NR>=3&&NR<=6{print}' /etc/passwd       //显示第36

awk -F: 'NR==3||NR==6{print}' /etc/passwd       //显示第3行和第6

awk '(NR%2)==1{print}' /etc/passwd   //显示奇数行

awk '(NR%2)==0{print}' /etc/passwd   //显示偶数行

awk '{print NR,$0}' /etc/passwd                                 //输出每行的行号

awk -F: '{print NR,NF,$NF," ",$0}' /etc/passwd      //依次打印行号,字段数,最后字段值,制表符,每行内容

awk -F: 'NR==5{print}'  /etc/passwd                         //显示第5

route -n|awk 'NR!=1{print}'                                       //不显示第一行

awk -F: '{print NF}' /etc/passwd                                //显示每行有多少字段

awk -F: '{print $NF}' /etc/passwd                              //将每行第NF个字段的值打印出来

 awk -F: 'NF==4 {print }' /etc/passwd                       //显示只有4个字段的行

awk -F: 'NF>2{print $0}' /etc/passwd                       //显示每行字段数量大于2的行

awk '/^root/{print}' /etc/passwd   //显示以root开头的行

awk '/nologin$/{print}' /etc/passwd    //显示以nologin结尾的行

awk 'BEGIN {x=0};//bin/bash$/{x++};END {print x}' /etc/passwd      //统计以/bin/bash结尾的行数

awk 'BEGIN{RS=""};END{print NR}' /etc/ssh/sshd_config    //统计以空行分隔的文本段落数

awk -F":" '{print $3}' /etc/passwd    //显示第三列

awk -F":" '{print $1 $3}' /etc/passwd                       //$1$3相连输出,无空格,

awk -F":" '{print $1,$3}' /etc/passwd                       //多了一个逗号,输出第1和第3个字段,有空格

awk -F":" '{print $1 " " $3}' /etc/passwd                  //$1$3之间手动添加空格分隔

awk -F: '$2=="!!" {print}' /etc/shadow   //统计密码为空的shadow记录

awk -F: 'NR==5{print}' /etc/passwd                         //显示第5

awk -F: '/bash$/{print | "wc -l"}' /etc/passwd //统计使用bash的用户的数量

awk 'BEGIN {while ("w" | getline) n++ ;{print n-2}}' //统计在线用户数

awk 'BEGIN {"hostname" | getline ; print $0}' //输出当前主机名

awk -F: '$1~/mail/ && $3>6 {print }' /etc/passwd         //逻辑与,$1匹配mail,并且$3>6

awk -F: '{if($1~/mail/ && $3>8) print }' /etc/passwd  

awk -F: '$1~/mail/ || $3>1000 {print }' /etc/passwd       //逻辑或,统计以mail开头或第3列大于1000的行

awk -F: '{if($1~/mail/ || $3>1000) print }' /etc/passwd 

免责声明:文章转载自《shell脚本应用练习(4)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇纯css3 实现的焦点图GMM的EM算法实现下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

一、tomcat基础介绍及安装部署

一、tomcat介绍Tomcat服务器是一个免费的开放源代码的Web应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP网页的首选。 Tomcat和Nginx、Apache(httpd)、lighttpd等Web服务器一样,具有处理HTML页面的功能,另外它还是一个Servlet和JSP容器,独立的Servlet容器是Tom...

sysbench的安装及使用

sysbench是一个模块化的、跨平台、多线程基准,主要用于评估测试各种不同系统参数下的数据库负载情况。它主要包括以下几种方式的测试:测试工具 文档顺序: 一、安装 二、测试 1、cpu性能2、磁盘io性能3、调度程序性能4、内存分配及传输速度5、POSIX线程性能6、数据库性能(OLTP基准测试)目前sysbench主要支持 MySQL,pgsql,or...

docker在Ubuntu下1小时快速学习

前言 由于工作原因,很多情况下需要快速学习新的知识,针对docker如果从头到尾看相关书籍学习会非常慢,所以整理了下docker的常用操作,只要跟着本文学习操作,一小时就能掌握docker大部分最常用操作方法,也可以当做工具手册随时查找学习,当然本文未涉及的部分,还是需要通过阅读书籍学习,这文章的目的是帮助需要快速上手应用的人。由于写该文章的时候还比较早,...

阿里云ubuntu16.04 安装桌面版[转]

转:https://blog.csdn.net/never_give_up_z/article/details/83190285 1.当我们购买了阿里云服务器后,登陆后,进行如下操作,点击云服务器。  2.点击数字,进去我们的控制台  3.我们来修改密码,设置自己想要设置的密码 充值密码:是重置ubuntu中root的密码 修改远程连接密码:是进行远...

Android开发走过的坑(持续更新)

1 华为 nova真机 打印不出Log 参考资料:http://www.apkbus.com/thread-585228-1-1.html 解决:针对权限问题,我们当然也可以解决的,华为手机在你的拨号界面,请拨*#*#2846579#*#*,我们就进入了工程菜单,接下来我们就可以操作了。点击ProjectMenu,进入后台设置,很容易看到的就是LOG设置。...

CentOS7下简单几步操作自建DNS(使用coredns快速搭建简单dns服务器)

本文介绍了如何使用CoreDNS快速搭建一个简单DNS服务器,从而对CoreDNS有一个初步的认识。 1、下载coredns 通过coredns的github,下载coredns。coredns的release版本地址:https://github.com/coredns/coredns/releases 这里我下载的是当前最新版本——1.6.9:http...