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

转载于:https://www.cnblogs.com/yangjin-55/archive/2009/04/15/2786798.html

UNIX Shell 编程(5)相关推荐

  1. 《Linux与unix Shell编程指南》 总结

    <Linux与unix Shell编程指南> 总结 标签: linux网络dstcron服务器 2011-01-10 18:27 3742人阅读 评论(0) 收藏 举报 分类: Linux ...

  2. 2008年度LINUX与UNIX SHELL编程指北系列大片

    2008年度LINUX与UNIX SHELL编程 指北 系列大片 前言:这个世界是公平的,机会是平等的,学习的机会人人都有,只要你有不甘平庸的上进之心,定会成功! 主演:rootman rootman ...

  3. 【编程实践】Linux / UNIX Shell编程极简教程

    不同于一般的介绍Linux Shell 的文章,本文并未花大篇幅去介绍 Shell 语法,而是以面向"对象" 的方式引入大量的实例介绍 Shell 日常操作,"对象&qu ...

  4. linux与shell编程指南,LINUX与UNIX SHELL编程指南

    目      录 译者序 前言 第一部分   shell 第1章   文件安全与权限1 1.1   文件1 1.2   文件类型2 1.3   权限2 1.4   改变权限位4 1.4.1   符号模 ...

  5. Linux Unix shell 编程指南学习笔记(第五部分)

    第二十五章 深入讨论 << 当shell 看到 << 的时候,它知道下一个词是一个分界符.该分界符后面的内容都被当做输入,直到shell又看到该分界符(位于单独的一行).比方: ...

  6. 《LINUX与UNIX SHELL编程指南》学习笔记

    linux下文件的几种类型:目录,符号链接.套接字文件.....要注意,说到的文件类型是一个不一样的概念.linux下文件的类型不是后缀名上进行区别. "-"表示该文件是一个普通类 ...

  7. LINUX与UNIX SHELL编程指南 学习笔记

    第1章文件安全与权限 1.2 文件类型 还记得前面一节所提到的文件权限位前面的那个字符吗?我们现在就解释一下这个横杠 所代表的意思,文件类型有七种,它可以从ls -l命令所列出的结果的第一位看出,这七 ...

  8. Unix shell 编程相关命令

    1. 命令移动到命令开头和结尾 1.ctrl+a 开头 2.ctrl+e 结尾

  9. SHELL编程学习笔记

    SHELL编程学习笔记 本文描述unix shell的各种应用实例,根据查阅资料和自我总结,作为自己今后复习的模板.本文搜集整理常用的shell应用实例,以例子详述unixshell部分命令的使用,着 ...

  10. 进程和程序:编写shell——《Unix/Linux编程实践教程》读书笔记(第8章)

    2019独角兽企业重金招聘Python工程师标准>>> 1.Unix shell的功能 shell是一个管理进程和运行程序的程序.所有常用的shell都有3个主要功能: (1)运行程 ...

最新文章

  1. Log4net中的RollingFileAppender解析
  2. OA工作流程怎么梳理
  3. 湖南雅礼培训 1.1
  4. kali linux 设置固定IP地址步骤
  5. 02.elasticsearch bucket aggregation查询
  6. Spring中WebApplicationInitializer的理解
  7. Qt工作笔记-对*QObject::sender()的认识
  8. 30分钟快速上手Docker,看这篇就对了!
  9. Spring配置中的classpath:与classpath*:的区别研究(转)
  10. python 找到装饰器_Python 装饰器的总结(一)
  11. 并查集(ACWING三道题)
  12. Chrome 下载文件出现已禁止
  13. python open gbk_python3 open txt的UnicodeDecodeError: 'gbk' codec问题解决方案
  14. java8分组求和_Java8 stream 中利用 groupingBy 进行多字段分组求和案例
  15. vue2.x使用Relation Graph 人物关系图谱
  16. 《设计模式之禅》-原型模式
  17. ESXI 6.7 虚拟机配置端口聚合
  18. Spring Data JPA方法定义规范
  19. 聊天机器人简介与体验网址
  20. Win11图片打不开怎么办?Win11无法打开图片的修复方法

热门文章

  1. BZOJ1468Tree——点分治
  2. [洛谷P3621] [APIO2007] 风铃
  3. Node.js:连接 MySQL
  4. 【Java集合系列一】ArrayList解析
  5. 电子表格中的数据清洗
  6. python实训day7
  7. php模拟顺序栈基本操作
  8. Python文件与函数练习题
  9. Java学习关于时间操作的应用类--Date类、Calendar类及其子类
  10. 【数据库】Mean web开发 05-Linux上配置MongoDB自动启动及远程连接