玩转Linux操作系统
说明:本文中对Linux命令的讲解都是基于名为CentOS的Linux发行版本,我自己使用的是阿里云服务器,系统版本为CentOS Linux release 7.6.1810。不同的Linux发行版本在Shell命令和工具程序上会有一些差别,但是这些差别是很小的。
操作系统发展史
只有硬件没有软件的计算机系统被称之为“裸机”,我们很难用“裸机”来完成计算机日常的工作(如存储和运算),所以必须用特定的软件来控制硬件的工作。最靠近计算机硬件的软件是系统软件,其中最为重要的就是“操作系统”。“操作系统”是控制和管理整个计算机硬件和软件资源、实现资源分配和任务调配、为系统用户以及其他软件提供接口和环境的程序的集合。
没有操作系统(手工操作)
在计算机诞生之初没有操作系统的年代,人们先把程序纸带(或卡片)装上计算机,然后启动输入机把程序送入计算机,接着通过控制台开关启动程序运行。当程序执行完毕,打印机输出计算的结果,用户卸下并取走纸带(或卡片)。第二个用户上机,重复同样的步骤。在整个过程中用户独占机器,CPU等待手工操作,资源利用率极低。
批处理系统
首先启动计算机上的一个监督程序,在监督程序的控制下,计算机能够自动的、成批的处理一个或多个用户的作业。完成一批作业后,监督程度又从输入机读取作业存入磁带机。按照上面的步骤重复处理任务。监督程序不停的处理各个作业,实现了作业的自动转接,减少了作业的建立时间和手工操作时间,提高了计算机资源的利用率。 批处理系统又可以分为单道批处理系统、多道批处理系统、联机批处理系统、脱机批处理系统。
分时系统和实时系统
分时系统是把处理器的运行时间分成很短的时间片,按时间片轮流把处理机分配给各联机作业使用。 若某个作业在分配给它的时间片内不能完成其计算,则该作业暂时中断,把处理机让给另一作业使用,等待下一轮调度时再继续其运行。由于计算机速度很快,作业运行轮转得很快,给每个用户的感觉是他独占了一台计算机。而每个用户可以通过自己的终端向系统发出各种操作控制命令,在充分的人机交互情况下,完成作业的运行。为了解决分时系统不能及时响应用户指令的情况,又出现了能够在在严格的时间范围内完成事件处理,及时响应随机外部事件的实时系统。
通用操作系统
-
1960s:IBM的System/360系列的机器有了统一的操作系统OS/360。
-
1965年:AT&T的贝尔实验室加入GE和MIT的合作计划开始开发MULTICS。
-
1969年:MULTICS项目失败,Ken Tompson赋闲在家,为了玩“Space Travel”游戏用汇编语言在当时已经被淘汰的PDP-7上开发了Unics。
注:很难想象,Unix这么伟大的操作系统,居然是一个赋闲在家的程序员(关键是老婆回娘家还带上了孩子)在一台被淘汰的设备上为了玩游戏开发出来的。
-
1970年~1971年:Ken Thompson和Dennis Ritchie用B语言在PDP-11上重写了Unics,并在Brian Kernighan的建议下将其更名为Unix。
-
1972年~1973年:Dennis Ritchie发明了C语言来取代可移植性较差的B语言,并开启了用C语言重写Unix的工作。
-
1974年:Unix推出了里程碑意义的第5版,几乎完全用C语言来实现。
-
1979年:从Unix第7版开始,AT&T发布新的使用条款,将Unix私有化。
-
1987年:Andrew S. Tanenbaum教授为了能在课堂上为学生讲解操作系统运作的细节,决定在不使用任何AT&T的源代码前提下,自行开发与Unix兼容的操作系统以避免版权上的争议,该系统被命名为Minix。
-
1991年:Linus Torvalds就读于芬兰赫尔辛基大学期间,尝试在Minix上做一些开发工作,但因为Minix只是作为教学用途的操作系统,功能并不强大,为了方便在学校的新闻组和邮件系统中读写和下载文件,Linus编写了磁盘驱动程序和文件系统,这些东西形成了Linux系统内核的雏形。
下图是Unix操作系统家族的图谱。
Linux概述
Linux是一个通用操作系统。一个操作系统要负责任务调度、内存分配、处理外围设备I/O等操作。操作系统通常由内核(运行其他程序,管理像磁盘、打印机等硬件设备的核心程序)和系统程序(设备驱动、底层库、shell、服务程序等)两部分组成。
Linux内核是芬兰人Linus Torvalds开发的,于1991年9月发布。而Linux操作系统作为Internet时代的产物,它是由全世界许多开发者共同合作开发的,是一个自由的操作系统(注意自由和免费并不是同一个概念,想了解二者的差别可以点击这里)。
Linux系统优点
- 通用操作系统,不跟特定的硬件绑定。
- 用C语言编写,可移植性强,有内核编程接口。
- 支持多用户和多任务,支持安全的分层文件系统。
- 大量的实用程序,完善的网络功能以及强大的支持文档。
- 可靠的安全性和良好的稳定性,对开发者更友好。
Linux系统发行版本
基础命令
Linux系统的命令通常都是如下所示的格式:
命令名称 [命名参数] [命令对象]
-
获取登录信息 - w / who / last/ lastb。
[root ~]# w 23:31:16 up 12:16, 2 users, load average: 0.00, 0.01, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 182.139.66.250 23:03 4.00s 0.02s 0.00s w jackfrue pts/1 182.139.66.250 23:26 3:56 0.00s 0.00s -bash [root ~]# who root pts/0 2018-04-12 23:03 (182.139.66.250) jackfrued pts/1 2018-04-12 23:26 (182.139.66.250) [root ~]# who am i root pts/0 2018-04-12 23:03 (182.139.66.250) [root ~]# who mom likes root pts/0 2018-04-12 23:03 (182.139.66.250) [root ~]# last root pts/0 117.136.63.184 Sun May 26 18:57 still logged in reboot system boot 3.10.0-957.10.1. Mon May 27 02:52 - 19:10 (-7:-42) root pts/4 117.136.63.184 Sun May 26 18:51 - crash (08:01) root pts/4 117.136.63.184 Sun May 26 18:49 - 18:49 (00:00) root pts/3 117.136.63.183 Sun May 26 18:35 - crash (08:17) root pts/2 117.136.63.183 Sun May 26 18:34 - crash (08:17) root pts/0 117.136.63.183 Sun May 26 18:10 - crash (08:42)
-
查看自己使用的Shell - ps。
Shell也被称为“壳”或“壳程序”,它是用户与操作系统内核交流的翻译官,简单的说就是人与计算机交互的界面和接口。目前很多Linux系统默认的Shell都是bash(Bourne Again SHell),因为它可以使用tab键进行命令和路径补全、可以保存历史命令、可以方便的配置环境变量以及执行批处理操作。
[root ~]# ps PID TTY TIME CMD 3531 pts/0 00:00:00 bash 3553 pts/0 00:00:00 ps
-
查看命令的说明和位置 - whatis / which / whereis。
[root ~]# whatis ps ps (1) - report a snapshot of the current processes. [root ~]# whatis python python (1) - an interpreted, interactive, object-oriented programming language [root ~]# whereis ps ps: /usr/bin/ps /usr/share/man/man1/ps.1.gz [root ~]# whereis python python: /usr/bin/python /usr/bin/python2.7 /usr/lib/python2.7 /usr/lib64/python2.7 /etc/python /usr/include/python2.7 /usr/share/man/man1/python.1.gz [root ~]# which ps /usr/bin/ps [root ~]# which python /usr/bin/python
-
清除屏幕上显示的内容 - clear。
-
查看帮助文档 - man / info / –help / apropos。
[root@izwz97tbgo9lkabnat2lo8z ~]# ps --help Usage: ps [options] Try 'ps --help <simple|list|output|threads|misc|all>' or 'ps --help <s|l|o|t|m|a>' for additional help text. For more details see ps(1). [root@izwz97tbgo9lkabnat2lo8z ~]# man ps PS(1) User Commands PS(1) NAME ps - report a snapshot of the current processes. SYNOPSIS ps [options] DESCRIPTION ...
-
查看系统和主机名 - uname / hostname。
[root@izwz97tbgo9lkabnat2lo8z ~]# uname Linux [root@izwz97tbgo9lkabnat2lo8z ~]# hostname izwz97tbgo9lkabnat2lo8z [root@iZwz97tbgo9lkabnat2lo8Z ~]# cat /etc/centos-release CentOS Linux release 7.6.1810 (Core)
说明:
cat
是连接文件内容并打印到标准输出的命令,后面会讲到该命令;/etc
是Linux系统上的一个非常重要的目录,它保存了很多的配置文件;centos-release
是该目录下的一个文件,因为我自己使用的Linux发行版本是CentOS 7.6,因此这里会有一个这样的文件。 -
时间和日期 - date / cal。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# date Wed Jun 20 12:53:19 CST 2018 [root@iZwz97tbgo9lkabnat2lo8Z ~]# cal June 2018 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 [root@iZwz97tbgo9lkabnat2lo8Z ~]# cal 5 2017 May 2017 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
-
重启和关机 - reboot / shutdown。
[root ~]# shutdown -h +5 Shutdown scheduled for Sun 2019-05-26 19:34:27 CST, use 'shutdown -c' to cancel. [root ~]# Broadcast message from root (Sun 2019-05-26 19:29:27 CST): The system is going down for power-off at Sun 2019-05-26 19:34:27 CST! [root ~]# shutdown -c Broadcast message from root (Sun 2019-05-26 19:30:22 CST): The system shutdown has been cancelled at Sun 2019-05-26 19:31:22 CST! [root ~]# shutdown -r 23:58 Shutdown scheduled for Sun 2019-05-26 23:58:00 CST, use 'shutdown -c' to cancel. [root ~]# shutdown -c Broadcast message from root (Sun 2019-05-26 19:31:06 CST): The system shutdown has been cancelled at Sun 2019-05-26 19:32:06 CST!
说明:在执行
shutdown
命令时会向登录系统的用户发出警告,可以在命令后面跟上警告消息来替换默认的警告消息,也可以在-h
参数后通过now
来表示立刻关机。 -
退出登录 - exit / logout。
-
查看历史命令 - history。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# history
...
452 ls
453 cd Python-3.6.5/
454 clear
455 history
[root@iZwz97tbgo9lkabnat2lo8Z ~]# !454
说明:查看到历史命令之后,可以用
!历史命令编号
来重新执行该命令;通过history -c
可以清除历史命令。
实用程序
文件和文件夹操作
-
创建/删除空目录 - mkdir / rmdir。
[root ~]# mkdir abc [root ~]# mkdir -p xyz/abc [root ~]# rmdir abc
-
创建/删除文件 - touch / rm。
[root ~]# touch readme.txt [root ~]# touch error.txt [root ~]# rm error.txt rm: remove regular empty file ‘error.txt’? y [root ~]# rm -rf xyz
touch
命令用于创建空白文件或修改文件时间。在Linux系统中一个文件有三种时间:- 更改内容的时间 - mtime。
- 更改权限的时间 - ctime。
- 最后访问时间 - atime。
rm
的几个重要参数:-i
:交互式删除,每个删除项都会进行询问。-r
:删除目录并递归的删除目录中的文件和目录。-f
:强制删除,忽略不存在的文件,没有任何提示。
-
切换和查看当前工作目录 - cd / pwd。
说明:
cd
命令后面可以跟相对路径(以当前路径作为参照)或绝对路径(以/
开头)来切换到指定的目录,也可以用cd ..
来返回上一级目录。请大家想一想,如果要返回到上上一级目录应该给cd
命令加上什么样的参数呢? -
查看目录内容 - ls。
-l
:以长格式查看文件和目录。-a
:显示以点开头的文件和目录(隐藏文件)。-R
:遇到目录要进行递归展开(继续列出目录下面的文件和目录)。-d
:只列出目录,不列出其他内容。-S
/-t
:按大小/时间排序。
-
查看文件内容 - cat / tac / head / tail / more / less / rev / od。
[root ~]# wget http://www.sohu.com/ -O sohu.html --2018-06-20 18:42:34-- http://www.sohu.com/ Resolving www.sohu.com (www.sohu.com)... 14.18.240.6 Connecting to www.sohu.com (www.sohu.com)|14.18.240.6|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 212527 (208K) [text/html] Saving to: ‘sohu.html’ 100%[==================================================>] 212,527 --.-K/s in 0.03s 2018-06-20 18:42:34 (7.48 MB/s) - ‘sohu.html’ saved [212527/212527] [root ~]# cat sohu.html ... [root ~]# head -10 sohu.html <!DOCTYPE html> <html> <head> <title>搜狐</title> <meta name="Keywords" content="搜狐,门户网站,新媒体,网络媒体,新闻,财经,体育,娱乐,时尚,汽车,房产,科技,图片,论坛,微博,博客,视频,电影,电视剧"/> <meta name="Description" content="搜狐网为用户提供24小时不间断的最新资讯,及搜索、邮件等网络服务。内容包括全球热点事件、突发新闻、时事评论、热播影视剧、体育赛事、行业动态、生活服务信息,以及论坛、博客、微博、我的搜狐等互动空间。" /> <meta name="shenma-site-verification" content="1237e4d02a3d8d73e96cbd97b699e9c3_1504254750"> <meta charset="utf-8"/> <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1"/> [root ~]# tail -2 sohu.html </body> </html> [root ~]# less sohu.html ... [root ~]# cat -n sohu.html | more ...
说明:上面用到了一个名为
wget
的命令,它是一个网络下载器程序,可以从指定的URL下载资源。 -
拷贝/移动文件 - cp / mv。
[root ~]# mkdir backup [root ~]# cp sohu.html backup/ [root ~]# cd backup [root backup]# ls sohu.html [root backup]# mv sohu.html sohu_index.html [root backup]# ls sohu_index.html
-
文件重命名 - rename。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# rename .htm .html *.htm
-
查找文件和查找内容 - find / grep。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# find / -name "*.html" /root/sohu.html /root/backup/sohu_index.html [root@izwz97tbgo9lkabnat2lo8z ~]# find . -atime 7 -type f -print [root@izwz97tbgo9lkabnat2lo8z ~]# find . -type f -size +2k [root@izwz97tbgo9lkabnat2lo8z ~]# find . -type f -name "*.swp" -delete [root@iZwz97tbgo9lkabnat2lo8Z ~]# grep "<script>" sohu.html -n 20:<script> [root@iZwz97tbgo9lkabnat2lo8Z ~]# grep -E \<\/?script.*\> sohu.html -n 20:<script> 22:</script> 24:<script src="//statics.itc.cn/web/v3/static/js/es5-shim-08e41cfc3e.min.js"></script> 25:<script src="//statics.itc.cn/web/v3/static/js/es5-sham-1d5fa1124b.min.js"></script> 26:<script src="//statics.itc.cn/web/v3/static/js/html5shiv-21fc8c2ba6.js"></script> 29:<script type="text/javascript"> 52:</script> ...
说明:
grep
在搜索字符串时可以使用正则表达式,如果需要使用正则表达式可以用grep -E
或者直接使用egrep
。 -
创建链接和查看链接 - ln / readlink。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# ls -l sohu.html -rw-r--r-- 1 root root 212131 Jun 20 19:15 sohu.html [root@iZwz97tbgo9lkabnat2lo8Z ~]# ln /root/sohu.html /root/backup/sohu_backup [root@iZwz97tbgo9lkabnat2lo8Z ~]# ls -l sohu.html -rw-r--r-- 2 root root 212131 Jun 20 19:15 sohu.html [root@iZwz97tbgo9lkabnat2lo8Z ~]# ln /root/sohu.html /root/backup/sohu_backup2 [root@iZwz97tbgo9lkabnat2lo8Z ~]# ls -l sohu.html -rw-r--r-- 3 root root 212131 Jun 20 19:15 sohu.html [root@iZwz97tbgo9lkabnat2lo8Z ~]# ln -s /etc/centos-release sysinfo [root@iZwz97tbgo9lkabnat2lo8Z ~]# ls -l sysinfo lrwxrwxrwx 1 root root 19 Jun 20 19:21 sysinfo -> /etc/centos-release [root@iZwz97tbgo9lkabnat2lo8Z ~]# cat sysinfo CentOS Linux release 7.4.1708 (Core) [root@iZwz97tbgo9lkabnat2lo8Z ~]# cat /etc/centos-release CentOS Linux release 7.4.1708 (Core)
说明:链接可以分为硬链接和软链接(符号链接)。硬链接可以认为是一个指向文件数据的指针,就像Python中对象的引用计数,每添加一个硬链接,文件的对应链接数就增加1,只有当文件的链接数为0时,文件所对应的存储空间才有可能被其他文件覆盖。我们平常删除文件时其实并没有删除硬盘上的数据,我们删除的只是一个指针,或者说是数据的一条使用记录,所以类似于“文件粉碎机”之类的软件在“粉碎”文件时除了删除文件指针,还会在文件对应的存储区域填入数据来保证文件无法再恢复。软链接类似于Windows系统下的快捷方式,当软链接链接的文件被删除时,软链接也就失效了。
-
压缩/解压缩和归档/解归档 - gzip / gunzip / xz。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# wget http://download.redis.io/releases/redis-4.0.10.tar.gz
--2018-06-20 19:29:59-- http://download.redis.io/releases/redis-4.0.10.tar.gz
Resolving download.redis.io (download.redis.io)... 109.74.203.151
Connecting to download.redis.io (download.redis.io)|109.74.203.151|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1738465 (1.7M) [application/x-gzip]
Saving to: ‘redis-4.0.10.tar.gz’
100%[==================================================>] 1,738,465 70.1KB/s in 74s
2018-06-20 19:31:14 (22.9 KB/s) - ‘redis-4.0.10.tar.gz’ saved [1738465/1738465]
[root@iZwz97tbgo9lkabnat2lo8Z ~]# ls redis*
redis-4.0.10.tar.gz
[root@iZwz97tbgo9lkabnat2lo8Z ~]# gunzip redis-4.0.10.tar.gz
[root@iZwz97tbgo9lkabnat2lo8Z ~]# ls redis*
redis-4.0.10.tar
- 归档和解归档 - tar。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# tar -xvf redis-4.0.10.tar
redis-4.0.10/
redis-4.0.10/.gitignore
redis-4.0.10/00-RELEASENOTES
redis-4.0.10/BUGS
redis-4.0.10/CONTRIBUTING
redis-4.0.10/COPYING
redis-4.0.10/INSTALL
redis-4.0.10/MANIFESTO
redis-4.0.10/Makefile
redis-4.0.10/README.md
redis-4.0.10/deps/
redis-4.0.10/deps/Makefile
redis-4.0.10/deps/README.md
...
说明:归档(也称为创建归档)和解归档都使用
tar
命令,通常创建归档需要-cvf
三个参数,其中c
表示创建(create),v
表示显示创建归档详情(verbose),f
表示指定归档的文件(file);解归档需要加上-xvf
参数,其中x
表示抽取(extract),其他两个参数跟创建归档相同。
- 将标准输入转成命令行参数 - xargs。
下面的命令会将查找当前路径下的html文件,然后通过xargs
将这些文件作为参数传给rm
命令,实现查找并删除文件的操作。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# find . -type f -name "*.html" | xargs rm -f
下面的命令将a.txt文件中的多行内容变成一行输出到b.txt文件中,其中<
表示从a.txt中读取输入,>
表示将命令的执行结果输出到b.txt中。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# xargs < a.txt > b.txt
说明:这个命令就像上面演示的那样常在管道(实现进程间通信的一种方式)和重定向(重新指定输入输出的位置)操作中用到,后面的内容中会讲到管道操作和输入输出重定向操作。
-
显示文件或目录 - basename / dirname。
-
其他相关工具。
- sort - 对内容排序
- uniq - 去掉相邻重复内容
- tr - 替换指定内容为新内容
- cut / paste - 剪切/黏贴内容
- split - 拆分文件
- file - 判断文件类型
- wc - 统计文件行数、单词数、字节数
- iconv - 编码转换
[root ~]# cat foo.txt
grape
apple
pitaya
[root ~]# cat bar.txt
100
200
300
400
[root ~]# paste foo.txt bar.txt
grape 100
apple 200
pitaya 300
400
[root ~]# paste foo.txt bar.txt > hello.txt
[root ~]# cut -b 4-8 hello.txt
pe 10
le 20
aya 3
0
[root ~]# cat hello.txt | tr '\t' ','
grape,100
apple,200
pitaya,300
,400
[root ~]# split -l 100 sohu.html hello
[root ~]# wget https://www.baidu.com/img/bd_logo1.png
[root ~]# file bd_logo1.png
bd_logo1.png: PNG image data, 540 x 258, 8-bit colormap, non-interlaced
[root ~]# wc sohu.html
2979 6355 212527 sohu.html
[root ~]# wc -l sohu.html
2979 sohu.html
[root ~]# wget http://www.qq.com -O qq.html
[root ~]# iconv -f gb2312 -t utf-8 qq.html
管道和重定向
-
管道的使用 - |。
例子:查找当前目录下文件个数。
[root ~]# find ./ | wc -l 6152
例子:列出当前路径下的文件和文件夹,给每一项加一个编号。
[root ~]# ls | cat -n 1 dump.rdb 2 mongodb-3.6.5 3 Python-3.6.5 4 redis-3.2.11 5 redis.conf
例子:查找record.log中包含AAA,但不包含BBB的记录的总数
[root ~]# cat record.log | grep AAA | grep -v BBB | wc -l
-
输出重定向和错误重定向 - > / >> / 2>。
[root ~]# cat readme.txt banana apple grape apple grape watermelon pear pitaya [root ~]# cat readme.txt | sort | uniq > result.txt [root ~]# cat result.txt apple banana grape pear pitaya watermelon
-
输入重定向 - <。
[root ~]# echo 'hello, world!' > hello.txt [root ~]# wall < hello.txt [root ~]# Broadcast message from root (Wed Jun 20 19:43:05 2018): hello, world! [root ~]# echo 'I will show you some code.' >> hello.txt [root ~]# wall < hello.txt [root ~]# Broadcast message from root (Wed Jun 20 19:43:55 2018): hello, world! I will show you some code.
-
多重定向 - tee。
下面的命令除了在终端显示命令
ls
的结果之外,还会追加输出到ls.txt
文件中。[root ~]# ls | tee -a ls.txt
别名
-
alias
[root ~]# alias ll='ls -l' [root ~]# alias frm='rm -rf' [root ~]# ll ... drwxr-xr-x 2 root root 4096 Jun 20 12:52 abc ... [root ~]# frm abc
-
unalias
[root ~]# unalias frm [root ~]# frm sohu.html -bash: frm: command not found
文本处理
-
字符流编辑器 - sed。
sed是操作、过滤和转换文本内容的工具。假设有一个名为fruit.txt的文件,内容如下所示。
[root ~]# cat -n fruit.txt 1 banana 2 grape 3 apple 4 watermelon 5 orange
接下来,我们在第2行后面添加一个pitaya。
[root ~]# sed '2a pitaya' fruit.txt banana grape pitaya apple watermelon orange
注意:刚才的命令和之前我们讲过的很多命令一样并没有改变fruit.txt文件,而是将添加了新行的内容输出到终端中,如果想保存到fruit.txt中,可以使用输出重定向操作。
在第2行前面插入一个waxberry。
[root ~]# sed '2i waxberry' fruit.txt banana waxberry grape apple watermelon orange
删除第3行。
[root ~]# sed '3d' fruit.txt banana grape watermelon orange
删除第2行到第4行。
[root ~]# sed '2,4d' fruit.txt banana orange
将文本中的字符a替换为@。
[root ~]# sed 's#a#@#' fruit.txt b@nana gr@pe @pple w@termelon or@nge
将文本中的字符a替换为@,使用全局模式。
[root ~]# sed 's#a#@#g' fruit.txt b@n@n@ gr@pe @pple w@termelon or@nge
-
模式匹配和处理语言 - awk。
awk是一种编程语言,也是Linux系统中处理文本最为强大的工具,它的作者之一和现在的维护者就是之前提到过的Brian Kernighan(ken和dmr最亲密的伙伴)。通过该命令可以从文本中提取出指定的列、用正则表达式从文本中取出我们想要的内容、显示指定的行以及进行统计和运算,总之它非常强大。
假设有一个名为fruit2.txt的文件,内容如下所示。
[root ~]# cat fruit2.txt 1 banana 120 2 grape 500 3 apple 1230 4 watermelon 80 5 orange 400
显示文件的第3行。
[root ~]# awk 'NR==3' fruit2.txt 3 apple 1230
显示文件的第2列。
[root ~]# awk '{print $2}' fruit2.txt banana grape apple watermelon orange
显示文件的最后一列。
[root ~]# awk '{print $NF}' fruit2.txt 120 500 1230 80 400
输出末尾数字大于等于300的行。
[root ~]# awk '{if($3 >= 300) {print $0}}' fruit2.txt 2 grape 500 3 apple 1230 5 orange 400
上面展示的只是awk命令的冰山一角,更多的内容留给读者自己在实践中去探索。