美文网首页
作业-第03周-预习--Day09-文本过滤工具

作业-第03周-预习--Day09-文本过滤工具

作者: MineG | 来源:发表于2019-03-16 17:08 被阅读0次

Day09 预习笔记

1. grep:文本过滤工具

~~grep命令是Linux系统中最重要的命令之一,其功能是从文本文件或管道数据流中筛选匹配的行及数据,如果配合正则表达式技术一起使用,则功能更加强大。

grep命令的常用参数说明:

  • ~~-v 显示不匹配的行(取反)
  • ~~-n显示匹配行及行号
  • ~~-i 不区分大小写(只适合用于单字符),默认是区分大小写的
  • ~~-c 只统计匹配的行数,注意不是匹配的次数
  • ~~-E 使用扩展的egrep
  • ~~--color=auto 给grep过滤的匹配字符串加颜色
  • ~~-w 只匹配过滤的单词
  • ~~-o 只输出匹配的内容

使用范例:
1.1 ''–v''参数实践

[root@oldboyedu  ~]#cat 1.txt
1
2
3
[root@oldboyedu  ~]# grep -v "2" 1.txt
1
3
[root@oldboyedu  ~]#

1.2 ''-n''参数实践

[root@oldboyedu  ~]# cat 2.txt
aa
aa
bb
cc
dd
ee
ff
bb
cc
dd
ee
ff
[root@oldboyedu  ~]# grep -n "cc" 2.txt
4:cc
9:cc
[root@oldboyedu  ~]#

1.3 ''-i''参数实践

[root@oldboyedu  ~]# cat 3.txt
aa
bb
cc
Ab
Ba
cA
bC
[root@oldboyedu  ~]# grep "c" 3.txt
cc
cA
[root@oldboyedu  ~]# grep -i "c" 3.txt
cc
cA
bC
[root@oldboyedu  ~]#

1.4 ''-E''和''--color''的参数实践

[root@oldboyedu  ~]# cat 3.txt
aa
bb
cc
Ab
Ba
cA
bC
[root@oldboyedu  ~]# grep -Ei "a|c" 3.txt
aa
cc
Ab
Ba
cA
bC
[root@oldboyedu  ~]# grep -Ei --color=auto "a|c" 3.txt
aa
cc
Ab
Ba
cA
bC
[root@oldboyedu  ~]#

1.5 ''-c''参数实战

[root@oldboyedu  ~]# cat 3.txt
aa
bb
cc
Ab
Ba
cA
bC
[root@oldboyedu  ~]# grep -c "c" 3.txt
2
[root@oldboyedu  ~]#

1.6 ''-o''参数实战

[root@oldboyedu  ~]# cat 3.txt
aa
bb
cc
Ab
Ba
cA
bC
[root@oldboyedu  ~]# grep -o "c" 3.txt
c
c
c
[root@oldboyedu  ~]#

2. tr:替换或删除字符

~~tr命令从标准输入中替换、缩减或者删除字符、并将结果写到标准输出

tr命令的常用参数说明:

  • ~~-d 删除字符
  • ~~-s 保留连续字符的第一个字符,删除其他字符
  • ~~-c 使用第一个字符串(set1)的补集,取反

使用范例
实践文本如下:

[root@oldboyedu  ~]# cat oldboy.txt
I am oldboy teacher!
I teach linux.

I like badminton ball,billiard ball and chinese chess!
my blog is http://oldboy.blog.51cto.com
our site is http://www.etiantian.org
my qq num is 49000448

not 4900000448
my god ,i am not oldbey,but OLDBOY!
[root@oldboyedu  ~]#

2.1 将文件中出现的“abc”替换为“xyz”。

[root@oldboyedu  ~]# tr 'abc' 'xyz' < oldboy.txt     
                            #tr命令接文件比较特殊,需要输入重定向符号“<”             
I xm oldyoy texzher!
I texzh linux.

I like yxdminton yxll,yillixrd yxll xnd zhinese zhess!
my ylog is http://oldyoy.ylog.51zto.zom
our site is http://www.etixntixn.org
my qq num is 49000448

not 4900000448
my god ,i xm not oldyey,yut OLDBOY!
[root@oldboyedu  ~]#

说明:凡是在文本中出现的“a”转换为“x”,“b”换换为“y”,“c”转换为“z”,而不是仅仅将字符串“abc”替换为字符串“xyz”

2.2 使用tr命令“统一”字母大小写。

[root@oldboyedu  ~]# tr '[a-z]' '[A-Z]' <oldboy.txt
I AM OLDBOY TEACHER!
I TEACH LINUX.

I LIKE BADMINTON BALL,BILLIARD BALL AND CHINESE CHESS!
MY BLOG IS HTTP://OLDBOY.BLOG.51CTO.COM
OUR SITE IS HTTP://WWW.ETIANTIAN.ORG
MY QQ NUM IS 49000448

NOT 4900000448
MY GOD ,I AM NOT OLDBEY,BUT OLDBOY!
[root@oldboyedu  ~]#

2.3 将数字0-9替换为a-j。(也是一一对应)

[root@oldboyedu  ~]# tr '[0-9]' '[a-j]' <oldboy.txt
I am oldboy teacher!
I teach linux.

I like badminton ball,billiard ball and chinese chess!
my blog is http://oldboy.blog.fbcto.com
our site is http://www.etiantian.org
my qq num is ejaaaeei

not ejaaaaaeei
my god ,i am not oldbey,but OLDBOY!
[root@oldboyedu  ~]#

2.4 删除文件中出现oldboy中的每个字符。

[root@oldboyedu  ~]# tr -d 'oldboy' <oldboy.txt
I am  teacher!
I teach inux.

I ike amintn a,iiar a an chinese chess!
m g is http://.g.51ct.cm
ur site is http://www.etiantian.rg
m qq num is 49000448

nt 4900000448
m g ,i am nt e,ut OLDBOY!
[root@oldboyedu  ~]#

说明:凡是在文件中出现的“o”,“l”,“d”,“b”,“y”字符都会被删除,而不是仅仅删除“oldboy”字符串。

相关文章

网友评论

      本文标题:作业-第03周-预习--Day09-文本过滤工具

      本文链接:https://www.haomeiwen.com/subject/bbzrmqtx.html