L25.linux命令每日一练 -- 第三章 文件过滤及内容编辑处理命令 -- rev和tr命令

3.19:rev:反向输出文件内容

3.19.1 命令详解

【命令星级】 ★★☆☆☆

【功能说明】

​ rev命令可以按行反向输出文件内容。

【语法格式】

rev [file]
rev [文件]

​ 说明:在rev命令和文件之间至少要有一个空格。

3.19.2 使用范例

​ **范例3-64:**字符串反转。

[root@centos7 ~]# echo {1..10}
1 2 3 4 5 6 7 8 9 10
[root@centos7 ~]# echo {1..10} |rev		#上面的字符反着写了。
01 9 8 7 6 5 4 3 2 1

​ **范例3-65:**文本反转。

[root@centos7 ~]# cat >neteagle3.txt<<EOF
> I am neteagle teacher!
> I tech Linux.
> 
> I like badminton ball,billiard ball and chinese chess!
> my blog is http://www.neteagles.cn
> our site is http://www.etiantian.org
> my god,i am not nets,but NETEAGLE!
> EOF
[root@centos7 ~]# rev neteagle3.txt 	#大家可以和前面学习过的tac命令对比一下。
!rehcaet elgaeten ma I
.xuniL hcet I

!ssehc esenihc dna llab draillib,llab notnimdab ekil I
nc.selgaeten.www//:ptth si golb ym
gro.naitnaite.www//:ptth si etis ruo
!ELGAETEN tub,sten ton ma i,dog ym

[root@centos7 ~]# tac neteagle3.txt 
my god,i am not nets,but NETEAGLE!
our site is http://www.etiantian.org
my blog is http://www.neteagles.cn
I like badminton ball,billiard ball and chinese chess!

I tech Linux.
I am neteagle teacher!

3.20 tr:替换或删除字符

3.20.1 命令详解

【命令星级】 ★★★★★

【功能说明】

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

【语法格式】

tr [option] [SET1] [SET2]
tr [选项] [字符1] [字符2]

【选项说明】

​ 表3-20针对tr命令的参数选项进行了说明。

​ 表3-20 tr命令的参数选项及说明

在这里插入图片描述

3.20.2 使用范例

​ 以下是后面的范例中要用到的实践文本:

[root@centos7 ~]# cat >neteagle.txt<<EOF
> I am neteagle teacher!
> I teach Linux.
> 
> I like badminton ball,billiard ball and chinese chess! 
> my blog is https://www.neteagles.cn
> our site is http://www.etiantian.org
> my qq num is 19661891.
> 
> not 19000661891.
> my god,i am not nets,but NETEAGLE!
> EOF

​ **范例3-66:**将文件中出现的"abc"替换为“xyz”。

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

I like yxdminton yxll,yillixrd yxll xnd zhinese zhess! 
my ylog is https://www.netexgles.zn
our site is http://www.etixntixn.org
my qq num is 19661891.

not 19000661891.
my god,i xm not nets,yut NETEAGLE!

​ **提示:**凡是在文本中出现的“a”全部转换成“x”,"b"全部转换成“y”,“c”全部转换成“z”,而不是仅仅将字符串“abc”替换为字符串“xyz”。

​ **范例3-67:**使用tr命令“统一”字母大小写。

[root@centos7 ~]# tr '[a-z]' '[A-Z]' <neteagle.txt	#小写转大写。
I AM NETEAGLE TEACHER!
I TEACH LINUX.

I LIKE BADMINTON BALL,BILLIARD BALL AND CHINESE CHESS! 
MY BLOG IS HTTPS://WWW.NETEAGLES.CN
OUR SITE IS HTTP://WWW.ETIANTIAN.ORG
MY QQ NUM IS 19661891.

NOT 19000661891.
MY GOD,I AM NOT NETS,BUT NETEAGLE!
#[a-z]是26个小写字母的缩写,[A-Z]是26个大写字母的缩写。因此本例是将a替换为A,b替换为B...z替换为Z。
[root@centos7 ~]# tr '[A-Z]' '[a-z]' <neteagle.txt	#大写转小写。
i am neteagle teacher!
i teach linux.

i like badminton ball,billiard ball and chinese chess! 
my blog is https://www.neteagles.cn
our site is http://www.etiantian.org
my qq num is 19661891.

not 19000661891.
my god,i am not nets,but neteagle!

​ **范例3-68:**将数字0-9替换为a-j。

[root@centos7 ~]# tr '[0-9]' '[a-j]' <neteagle.txt	#数字0替换为a,1替换为b ...一一对应。
I am neteagle teacher!
I teach Linux.

I like badminton ball,billiard ball and chinese chess! 
my blog is https://www.neteagles.cn
our site is http://www.etiantian.org
my qq num is bjggbijb.

not bjaaaggbijb.
my god,i am not nets,but NETEAGLE!

​ **范例3-69:**删除文件中出现的neteagle中的每个字符。

[root@centos7 ~]# tr -d 'neteagle' <neteagle.txt	#使用参数-d删除字符。
I m  chr!
I ch Liux.

I ik bdmio b,biird b d chis chss! 
my bo is hps://www.s.c
our si is hp://www.ii.or
my qq um is 19661891.

o 19000661891.
my od,i m o s,bu NETEAGLE!

​ **提示:**凡是在文件中出现的“n”、“e”、“t”、“e”、“a”、“g”、“l”、“e”字符都会被删除!而不是仅仅是删除neteagle字符串。

​ **范例3-70:**删除文件中出现的换行“\n”、制表“\t”字符。

[root@centos7 ~]# tr -d '\n\t' <neteagle.txt	#使用-d参数删除所有换行符河制表符,所有行都变成一行了,字母都连接在一起。
I am neteagle teacher!I teach Linux.I like badminton ball,billiard ball and chinese chess! my blog is https://www.neteagles.cnour site is http://www.etiantian.orgmy qq num is 19661891.not 19000661891.my god,i am not nets,but NETEAGLE![root@centos7 ~]# 

​ **范例3-71:**删除连续字符(-s)的例子。

[root@centos7 ~]# echo 'nnnneeettteeaaaaaggglllleeee' |tr -s neteagle	#使用-s参数将连续的字符压缩成一个。
neteagle

​ **范例3-72:**取反功能(-c)的例子。

[root@centos7 ~]# tr '0-9' '*' <neteagle.txt 	#将所有数字均替换为*。
I am neteagle teacher!
I teach Linux.

I like badminton ball,billiard ball and chinese chess! 
my blog is https://www.neteagles.cn
our site is http://www.etiantian.org
my qq num is ********.

not ***********.
my god,i am not nets,but NETEAGLE!
[root@centos7 ~]# tr -c '0-9' '*' <neteagle.txt 	#使用参数-c,除了数字,其他的字符包括换行符都会替换为*。
*************************************************************************************************************************************************************************************19661891*******19000661891*************************************[root@centos7 ~]# 

猜你喜欢

转载自blog.csdn.net/qq_25599925/article/details/125357022