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

目录
相关文章
|
28天前
|
存储 Shell Linux
【Shell 命令集合 网络通讯 】Linux 显示Unix-to-Unix Copy (UUCP) 系统的状态信息 uustat命令 使用指南
【Shell 命令集合 网络通讯 】Linux 显示Unix-to-Unix Copy (UUCP) 系统的状态信息 uustat命令 使用指南
26 0
|
2月前
|
Ubuntu Linux Shell
【Linux操作系统】探秘Linux奥秘:shell 编程的解密与实战
【Linux操作系统】探秘Linux奥秘:shell 编程的解密与实战
57 0
|
2天前
|
监控 Shell 开发工具
Shell编程
Shell编程
|
22天前
|
存储 Java Shell
bigdata-04-shell编程基础
bigdata-04-shell编程基础
10 0
|
24天前
|
Shell Linux C++
【Shell 编程设计】 编写自己的清理后台的Shell脚本
【Shell 编程设计】 编写自己的清理后台的Shell脚本
29 1
|
24天前
|
存储 Shell 数据安全/隐私保护
【Shell 编程指南】Shell read命令 (从标准输入读取数值)
【Shell 编程指南】Shell read命令 (从标准输入读取数值)
22 0
|
24天前
|
Shell C语言 C++
【Shell 编程指南】shell中的(),{}几种语法用法
【Shell 编程指南】shell中的(),{}几种语法用法
17 0
|
24天前
|
Shell 程序员 Linux
【Shell 编程指南】shell运算操作符之(())
【Shell 编程指南】shell运算操作符之(())
19 0
|
2月前
|
Unix Shell Linux
在Unix/Linux Shell中,管道(`|`)和重定向
在Unix/Linux Shell中,管道(`|`)和重定向
23 1
|
3月前
|
Shell
Shell 编程快速入门 之 函数基础知识
Shell 编程快速入门 之 函数基础知识
67 0
Shell 编程快速入门 之 函数基础知识