UNIX Shell 编程(5)

简介: 版权声明:本文为博主chszs的原创文章,未经博主允许不得转载。 https://blog.csdn.net/chszs/article/details/4076089 UNIX Shell 编程(5) 过滤器tr用来转换来自标准输入的字符。
版权声明:本文为博主chszs的原创文章,未经博主允许不得转载。 https://blog.csdn.net/chszs/article/details/4076089

UNIX Shell 编程(5)

 

过滤器tr用来转换来自标准输入的字符。格式:
tr from-chars to-chars
from-chars 和 to-chars 是一个或多个字符。例如:
[root@localhost programs]# cat intro
The Unix operating system was pioneered by Ken 
Thompson and Dennis Ritchie at Bell Laboratories 
in the late 1960s. One of the primary goals in 
the design of the Unix system was to create an 
environment that promoted efficient program 
developments.
[root@localhost programs]# tr e x < intro
Thx Unix opxrating systxm was pionxxrxd by Kxn 
Thompson and Dxnnis Ritchix at Bxll Laboratorixs 
in thx latx 1960s. Onx of thx primary goals in 
thx dxsign of thx Unix systxm was to crxatx an 
xnvironmxnt that promotxd xfficixnt program 
dxvxlopmxnts.

-s选项
用于压缩to-chars中重复的字符。例如:
[root@localhost programs]# cat lotsapaces 
This is an example of a
file that contains a lot
of blank spaces
[root@localhost programs]# tr -s ' ' < lotsapaces 
This is an example of a
file that contains a lot
of blank spaces

-d选项
用于删除输入流中的字符,格式为:
tr -d from-chars
删除所有空格
[root@localhost programs]# tr -d ' ' < lotsapaces 
Thisisanexampleofa
filethatcontainsalot
ofblankspaces
用sed命令也可实现:
[root@localhost programs]# sed 's/ //g' lotsapaces 
Thisisanexampleofa
filethatcontainsalot
ofblankspaces

tr命令综合实例:
————————————————————————————————————
tr 'X' 'x' 把所有大些X转换为小写x
tr '()' '{}' 把所有小括号换为大括号
tr '[A-Z]' '[N-ZA-M]' 把A-M字母转换为N-Z,把N-Z转换为A-M
tr -d '/14' 删除所有换页字符
tr -d '[0-9]' 删除所有数字  
————————————————————————————————————


grep命令可以从一个或多个文件中搜索特定的字符串模式,格式为:
grep pattern files
例如,从文件intro找出含Unix的行:
[root@localhost programs]# grep Unix intro
The Unix operating system was pioneered by Ken 
the design of the Unix system was to create an 

-v选项 显示不包含的行
例如,显示intro文件中所有不包括Unix的行:
[root@localhost programs]# grep -v 'Unix' intro
Thompson and Dennis Ritchie at Bell Laboratories 
in the late 1960s. One of the primary goals in 
environment that promoted efficient program 
developments.

例如,显示当前目录下所有文件中包含Unix或unix的行:
[root@localhost programs]# grep '[Uu]nix' *
intro:The Unix operating system was pioneered by Ken 
intro:the design of the Unix system was to create an 

-l选项 只给出包含给定模式的文件列表,而不给出文件中的匹配行。
[root@localhost programs]# grep -l '[Uu]nix' *
intro

-n选项 文件中符合指定模式的每一行前都加上该行在文件中的相对行号。
[root@localhost programs]# grep -n 'Unix' *
intro:1:The Unix operating system was pioneered by Ken 
intro:4:the design of the Unix system was to create an 


sort命令:
[root@localhost programs]# cat names
Tony
Emanuel
Lucy
Ralph
Fred
[root@localhost programs]# sort names
Emanuel
Fred
Lucy
Ralph
Tony

-u选项 在输出结果中去除重复的行

-r选项 颠倒排序顺序
[root@localhost programs]# sort -r names
Tony
Ralph
Lucy
Fred
Emanuel

sort的输出重定向:可用-o选项,也可用>符号
[root@localhost programs]# sort -r names > sorted_name1
[root@localhost programs]# sort names -o sorted_name2

-n选项 按第一列进行排序
如:
[root@localhost programs]# cat data2
5 27
2 12
3 33
23 2
-5 11
15 6
14 -9
[root@localhost programs]# sort -n data2
-5 11
2 12
3 33
5 27
14 -9
15 6
23 2

[root@localhost programs]# uniq names
Tony
Emanuel
Lucy
Ralph
Fred
Tony
[root@localhost programs]# sort names | uniq
Emanuel
Fred
Lucy
Ralph
Tony

目录
相关文章
|
1月前
|
Unix Linux Shell
【Shell 编程指南 日期命令】Date命令:显示与设置系统时间和日期
【Shell 编程指南 日期命令】Date命令:显示与设置系统时间和日期
73 0
|
1月前
|
Shell C++ 开发者
【Shell 编程指南】Shell运算操作符之$[]详解
【Shell 编程指南】Shell运算操作符之$[]详解
29 0
|
1月前
|
Ubuntu Linux Shell
【Linux操作系统】探秘Linux奥秘:shell 编程的解密与实战
【Linux操作系统】探秘Linux奥秘:shell 编程的解密与实战
85 0
|
1月前
|
存储 Shell Linux
【Shell 命令集合 网络通讯 】Linux 显示Unix-to-Unix Copy (UUCP) 系统的状态信息 uustat命令 使用指南
【Shell 命令集合 网络通讯 】Linux 显示Unix-to-Unix Copy (UUCP) 系统的状态信息 uustat命令 使用指南
33 0
|
20小时前
|
机器学习/深度学习 Unix Java
技术笔记:Linux之Shell脚本编程(一)
技术笔记:Linux之Shell脚本编程(一)
|
10天前
|
Shell Perl
7操作系统基础-Shell编程
7操作系统基础-Shell编程
7 0
|
1月前
|
监控 关系型数据库 Shell
Shell脚本入门:从基础到实践,轻松掌握Shell编程
Shell脚本入门:从基础到实践,轻松掌握Shell编程
|
1月前
|
存储 Shell C语言
shell脚本 编程 变量 基本入门(详解)
shell脚本 编程 变量 基本入门(详解)
|
1月前
|
监控 Shell 开发工具
Shell编程
Shell编程
|
1月前
|
Shell
Shell脚本编程基础
Shell脚本编程基础