关于此命令,有人认为写 cat 命令的人是因为喜欢猫,因此给此命令起名为“cat”,其实不然,cat 是 concatenate(连接、连续)的简写。
cat 命令的基本格式如下:[root@localhost ~]# cat [选项] 文件名
或者
[root@localhost ~]# cat 文件1 文件2 > 文件3
选项 | 含义 |
---|---|
-A | 相当于 -vET 选项的整合,用于列出所有隐藏符号; |
-E | 列出每行结尾的回车符 $; |
-n | 对输出的所有行进行编号; |
-b | 同 -n 不同,此选项表示只对非空行进行编号。 |
-T | 把 Tab 键 ^I 显示出来; |
-V | 列出特殊字符; |
-s | 当遇到有连续 2 行以上的空白行时,就替换为 1 行的空白行。 |
注意,cat 命令用于查看文件内容时,不论文件内容有多少,都会一次性显示。如果文件非常大,那么文件开头的内容就看不到了。不过 Linux 可以使用
PgUp+上箭头
组合键向上翻页,但是这种翻页是有极限的,如果文件足够长,那么还是无法看全文件的内容。因此,cat 命令适合查看不太大的文件。当然,在 Linux 中是可以使用其他的命令或方法来查看大文件的,我们以后再来学习。
【例 1】cat 命令本身非常简单,我们可以直接查看文件的内容。例如:
[root@localhost ~]# cat anaconda-ks.cfg # Kickstart file automatically generated by anaconda. #version=DEVEL install cdrom lang zh一CN.UTF-8 …省略部分内容...
[root@localhost ~]# cat -n anaconda-ks.cfg 1 # Kickstart file automatically generated by anaconda. 2 3 4 #version=DEVEL 5 install 6 cdrom …省略部分内容...
[root@localhost ~]# cat -A anaconda-ks.cfg # Kickstart file automatically generated by anaconda.$ $ $ #version=DEVEL$ install$ cdrom$ …省略部分内容…
[root@localhost base]# ls file1.txt file2.txt [root@localhost base]# cat file1.txt http://c.biancheng.net(file1.txt) [root@localhost base]# cat file2.txt is great(file2.txt) [root@localhost base]# cat file1.txt file2.txt > file3.txt [root@localhost base]# more file3.txt #more 命令可查看文件中的内容 http://c.biancheng.net(file1.txt) is great(file2.txt) [root@localhost base]# ls file1.txt file2.txt file3.txt
PgUp+上箭头
组合键向上翻页也无法看全文件中的内容,该怎么办呢?这就需要使用 more 命令。more 命令可以分页显示文本文件的内容,使用者可以逐页阅读文件中内容,此命令的基本格式如下:
[root@localhost ~]# more [选项] 文件名
选项 | 含义 |
---|---|
-f | 计算行数时,以实际的行数,而不是自动换行过后的行数。 |
-p | 不以卷动的方式显示每一页,而是先清除屏幕后再显示内容。 |
-c | 跟 -p 选项相似,不同的是先显示内容再清除其他旧资料。 |
-s | 当遇到有连续两行以上的空白行时,就替换为一行的空白行。 |
-u | 不显示下引号(根据环境变量 TERM 指定的终端而有所不同)。 |
+n | 从第 n 行开始显示文件内容,n 代表数字。 |
-n | 一次显示的行数,n 代表数字。 |
more 命令的执行会打开一个交互界面,因此读者有必要了解一些交互命令,常用的交互命令如表 2 所示。
交互指令 | 功能 |
---|---|
h 或 ? | 显示 more 命令交互命令帮助。 |
q 或 Q | 退出 more。 |
v | 在当前行启动一个编辑器。 |
:f | 显示当前文件的文件名和行号。 |
!<命令> 或 :!<命令> | 在子Shell中执行指定命令。 |
回车键 | 向下移动一行。 |
空格键 | 向下移动一页。 |
Ctrl+l | 刷新屏幕。 |
= | 显示当前行的行号。 |
' | 转到上一次搜索开始的地方。 |
Ctrf+f | 向下滚动一页。 |
. | 重复上次输入的命令。 |
/ 字符串 | 搜索指定的字符串。 |
d | 向下移动半页。 |
b | 向上移动一页。 |
【例 1】用分页的方式显示 anaconda-ks.cfg 文件的内容。
[root@localhost ~]# more anaconda-ks.cfg # Kickstart file automatically generated by anaconda. #version=DEVEL install cdrom …省略部分内容… --More--(69%) #在这里执行交互命令即可
【例 2】显示文件 anaconda-ks.cfg 的内容,每 10 行显示一屏,同时清楚屏幕,使用以下命令:
[root@localhost ~]# more -c -10 anaconda-ks.cfg #省略输出内容。
[root@localhost ~]# head [选项] 文件名
该命令常用选项以及各自的含义,如表 1 所示。选项 | 含义 |
---|---|
-n K | 这里的 K 表示行数,该选项用来显示文件前 K 行的内容;如果使用 "-K" 作为参数,则表示除了文件最后 K 行外,显示剩余的全部内容。 |
-c K | 这里的 K 表示字节数,该选项用来显示文件前 K 个字节的内容;如果使用 "-K",则表示除了文件最后 K 字节的内容,显示剩余全部内容。 |
-v | 显示文件名; |
【例 1】基本用法。
[root@localhost ~]# head anaconda-ks.cfg
[root@localhost ~]# head -n 20 anaconda-ks.cfg
[root@localhost ~]# head -20 anaconda-ks.cfg
不仅如此,为了方面用户浏览文本内容,less 命令还提供了以下几个功能:
- 使用光标键可以在文本文件中前后(左后)滚屏;
- 用行号或百分比作为书签浏览文件;
- 提供更加友好的检索、高亮显示等操作;
- 兼容常用的字处理程序(如 Vim、Emacs)的键盘操作;
- 阅读到文件结束时,less 命令不会退出;
- 屏幕底部的信息提示更容易控制使用,而且提供了更多的信息。
less 命令的基本格式如下:
[root@localhost ~]# less [选项] 文件名
选项 | 选项含义 |
---|---|
-N | 显示每行的行号。 |
-S | 行过长时将超出部分舍弃。 |
-e | 当文件显示结束后,自动离开。 |
-g | 只标志最后搜索到的关键同。 |
-Q | 不使用警告音。 |
-i | 忽略搜索时的大小写。 |
-m | 显示类似 more 命令的百分比。 |
-f | 强迫打开特殊文件,比如外围设备代号、目录和二进制文件。 |
-s | 显示连续空行为一行。 |
-b <缓冲区大小> | 设置缓冲区的大小。 |
-o <文件名> | 将 less 输出的内容保存到指定文件中。 |
-x <数字> | 将【Tab】键显示为规定的数字空格。 |
在使用 less 命令查看文件内容的过程中,和 more 命令一样,也会进入交互界面,因此需要读者掌握一些常用的交互指令,如表 2 所示。
交互指令 | 功能 |
---|---|
/字符串 | 向下搜索“字符串”的功能。 |
?字符串 | 向上搜索“字符串”的功能。 |
n | 重复*前一个搜索(与 / 成 ? 有关)。 |
N | 反向重复前一个搜索(与 / 或 ? 有关)。 |
b | 向上移动一页。 |
d | 向下移动半页。 |
h 或 H | 显示帮助界面。 |
q 或 Q | 退出 less 命令。 |
y | 向上移动一行。 |
空格键 | 向下移动一页。 |
回车键 | 向下移动一行。 |
【PgDn】键 | 向下移动一页。 |
【PgUp】键 | 向上移动一页。 |
Ctrl+f | 向下移动一页。 |
Ctrl+b | 向上移动一页。 |
Ctrl+d | 向下移动一页。 |
Ctrl+u | 向上移动半页。 |
j | 向下移动一行。 |
k | 向上移动一行。 |
G | 移动至最后一行。 |
g | 移动到第一行。 |
ZZ | 退出 less 命令。 |
v | 使用配置的编辑器编辑当前文件。 |
[ | 移动到本文档的上一个节点。 |
] | 移动到本文档的下一个节点。 |
p | 移动到同级的上一个节点。 |
u | 向上移动半页。 |
【例 1】使用 less 命令查看 /boot/grub/grub.cfg 文件中的内容。
[root@localhost ~]# less /boot/grub/grub.cfg # #DO NOT EDIT THIS FILE # #It is automatically generated by grub-mkconfig using templates from /etc/grub.d and settings from /etc/default/grub # ### BEGIN /etc/grub.d/00_header ### if [ -s $prefix/grubenv ]; then set have_grubenv=true load_env fi set default="0" if [ "$ {prev_saved_entry}" ]; then set saved_entry="${prev_saved_entry}" save_env saved_entry set prev_saved_entry= save_env prev_saved_entry set boot_once=true fi function savedefault { if [ -z "${boot_once}" ]; then :
[root@localhost ~]# tail [选项] 文件名
选项 | 含义 |
---|---|
-n K | 这里的 K 指的是行数,该选项表示输出最后 K 行,在此基础上,如果使用 -n +K,则表示从文件的第 K 行开始输出。 |
-c K | 这里的 K 指的是字节数,该选项表示输出文件最后 K 个字节的内容,在此基础上,使用 -c +K 则表示从文件第 K 个字节开始输出。 |
-f | 输出文件变化后新增加的数据。 |
【例 1】查看 /etc/passwd 文件最后 3 行的数据内容。
[root@localhost ~]# tail -n 3 /etc/passwd sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin oprofile:x:16:16:Special user account to be used by OProfile:/var/lib/oprofile:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin
[root@localhost ~]# tail -3 /etc/passwd sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin oprofile:x:16:16:Special user account to be used by OProfile:/var/lib/oprofile:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin
tail -n 3 /etc/passwd
命令和 tail -3 /etc/passwd
的效果是一样的。【例 2】查看 /etc/passwd 文件末尾 100 个字节的数据内容。
[root@localhost ~]# tail -c 100 /etc/passwd cpdump:x:72:72::/:/sbin/nologin
[root@localhost ~]#tail -f anaconda-ks.cfg @server-platform @server-policy pax oddjob sgpio certmonger pam_krb5 krb5-workstation perl-DBD-SQLite %end #光标不会退出文件,而会一直监听在文件的结尾处
[root@localhost ~]# echo 2222222222 >> anaconda-ks.cfg [root@localhost ~]# echo 3333333333 >> anaconda-ks.cfg
#在新终端中通过echo命令向文件中追加数据
[root@localhost ~]# tail -f anaconda-ks.cfg @server-platforin @server-policy pax oddjob sgpio certmonger pam_krb5 krb5-workstation perl-DBD-SQLite %end2222222222 33333333333 #在文件的结尾处监听到了新増数据
grep 命令作为 Linux 文本处理三剑客的一员,另外两个是 sed 和 awk,它们会在后续章节中作详细介绍。
grep 命令的由来可以追溯到 UNIX 诞生的早期,在 UNIX 系统中,搜索的模式(patterns)被称为正则表达式(regular expressions),为了要彻底搜索一个文件,有的用户在要搜索的字符串前加上前缀 global(全面的),一旦找到相匹配的内容,用户就像将其输出(print)到屏幕上,而将这一系列的操作整合到一起就是 global regular expressions print,而这也就是 grep 命令的全称。grep命令能够在一个或多个文件中,搜索某一特定的字符模式(也就是正则表达式),此模式可以是单一的字符、字符串、单词或句子。
正则表达式是描述一组字符串的一个模式,正则表达式的构成模仿了数学表达式,通过使用操作符将较小的表达式组合成一个新的表达式。正则表达式可以是一些纯文本文字,也可以是用来产生模式的一些特殊字符。为了进一步定义一个搜索模式,grep 命令支持如表 1 所示的这几种正则表达式的元字符(也就是通配符)。
通配符 | 功能 |
---|---|
c* | 将匹配 0 个(即空白)或多个字符 c(c 为任一字符)。 |
. | 将匹配任何一个字符,且只能是一个字符。 |
[xyz] | 匹配方括号中的任意一个字符。 |
[^xyz] | 匹配除方括号中字符外的所有字符。 |
^ | 锁定行的开头。 |
$ | 锁定行的结尾。 |
grep 命令是用来在每一个文件或中(或特定输出上)搜索特定的模式,当使用 grep 时,包含指定字符模式的每一行内容,都会被打印(显示)到屏幕上,但是使用 grep 命令并不改变文件中的内容。
grep 命令的基本格式如下:
[root@localhost ~]# grep [选项] 模式 文件名
选项 | 含义 |
---|---|
-c | 仅列出文件中包含模式的行数。 |
-i | 忽略模式中的字母大小写。 |
-l | 列出带有匹配行的文件名。 |
-n | 在每一行的最前面列出行号。 |
-v | 列出没有匹配模式的行。 |
-w | 把表达式当做一个完整的单字符来搜寻,忽略那些部分匹配的行。 |
注意,如果是搜索多个文件,grep 命令的搜索结果只显示文件中发现匹配模式的文件名;而如果搜索单个文件,grep 命令的结果将显示每一个包含匹配模式的行。
【例 1】假设有一份 emp.data 员工清单,现在要搜索此文件,找出职位为 CLERK 的所有员工,则执行命令如下:
[root@localhost ~]# grep CLERK emp.data #忽略输出内容 而在此基础上,如果只想知道职位为 CLERK 的员工的人数,可以使用“-c”选项,执行命令如下: [root@localhost ~]# grep -c CLERK emp.data #忽略输出内容
[root@localhost ~]# grep ^78 emp.data #忽略输出内容
sed 会根据脚本命令来处理文本文件中的数据,这些命令要么从命令行中输入,要么存储在一个文本文件中,此命令执行数据的顺序如下:
- 每次仅读取一行内容;
- 根据提供的规则命令匹配并修改数据。注意,sed 默认不会直接修改源文件数据,而是会将数据复制到缓冲区中,修改也仅限于缓冲区中的数据;
- 将执行结果输出。
sed 命令的基本格式如下:
[root@localhost ~]# sed [选项] [脚本命令] 文件名
选项 | 含义 |
---|---|
-e 脚本命令 | 该选项会将其后跟的脚本命令添加到已有的命令中。 |
-f 脚本命令文件 | 该选项会将其后文件中的脚本命令添加到已有的命令中。 |
-n | 默认情况下,sed 会在所有的脚本指定执行完毕后,会自动输出处理后的内容,而该选项会屏蔽启动输出,需使用 print 命令来完成输出。 |
-i | 此选项会直接修改源文件,要慎用。 |
成功使用 sed 命令的关键在于掌握各式各样的脚本命令及格式,它能帮你定制编辑文件的规则。
sed脚本命令
sed s 替换脚本命令
此命令的基本格式为:[address]s/pattern/replacement/flags
其中,address 表示指定要操作的具体行,pattern 指的是需要替换的内容,replacement 指的是要替换的新内容。关于指定具体操作行(address)的用法,这里先不做解释,文章后续会对其做详细介绍。
此命令中常用的 flags 标记如表 2 所示。flags 标记 | 功能 |
---|---|
n | 1~512 之间的数字,表示指定要替换的字符串出现第几次时才进行替换,例如,一行中有 3 个 A,但用户只想替换第二个 A,这是就用到这个标记; |
g | 对数据中所有匹配到的内容进行替换,如果没有 g,则只会在第一次匹配成功时做替换操作。例如,一行数据中有 3 个 A,则只会替换第一个 A; |
p | 会打印与替换命令中指定的模式匹配的行。此标记通常与 -n 选项一起使用。 |
w file | 将缓冲区中的内容写到指定的 file 文件中; |
& | 用正则表达式匹配的内容进行替换; |
\n | 匹配第 n 个子串,该子串之前在 pattern 中用 \(\) 指定。 |
\ | 转义(转义替换部分包含:&、\ 等)。 |
比如,可以指定 sed 用新文本替换第几处模式匹配的地方:
[root@localhost ~]# sed 's/test/trial/2' data4.txt This is a test of the trial script. This is the second test of the trial script.
如果要用新文件替换所有匹配的字符串,可以使用 g 标记:
[root@localhost ~]# sed 's/test/trial/g' data4.txt This is a trial of the trial script. This is the second trial of the trial script.
我们知道,-n 选项会禁止 sed 输出,但 p 标记会输出修改过的行,将二者匹配使用的效果就是只输出被替换命令修改过的行,例如:
[root@localhost ~]# cat data5.txt This is a test line. This is a different line. [root@localhost ~]# sed -n 's/test/trial/p' data5.txt This is a trial line.
w 标记会将匹配后的结果保存到指定文件中,比如:
[root@localhost ~]# sed 's/test/trial/w test.txt' data5.txt This is a trial line. This is a different line. [root@localhost ~]#cat test.txt This is a trial line.
[root@localhost ~]# sed 's/\/bin\/bash/\/bin\/csh/' /etc/passwd
sed d 替换脚本命令
此命令的基本格式为:[address]d
[root@localhost ~]# cat data1.txt The quick brown fox jumps over the lazy dog The quick brown fox jumps over the lazy dog The quick brown fox jumps over the lazy dog The quick brown fox jumps over the lazy dog [root@localhost ~]# sed 'd' data1.txt #什么也不输出,证明成了空文件
address 的具体写法后续会做详细介绍,这里只给大家举几个简单的例子:
- 通过行号指定,比如删除 data6.txt 文件内容中的第 3 行:
[root@localhost ~]# cat data6.txt This is line number 1. This is line number 2. This is line number 3. This is line number 4. [root@localhost ~]# sed '3d' data6.txt This is line number 1. This is line number 2. This is line number 4.
- 或者通过特定行区间指定,比如删除 data6.txt 文件内容中的第 2、3行:
[root@localhost ~]# sed '2,3d' data6.txt This is line number 1. This is line number 4.
- 也可以使用两个文本模式来删除某个区间内的行,但这么做时要小心,你指定的第一个模式会“打开”行删除功能,第二个模式会“关闭”行删除功能,因此,sed 会删除两个指定行之间的所有行(包括指定的行),例如:
[root@localhost ~]#sed '/1/,/3/d' data6.txt #删除第 1~3 行的文本数据 This is line number 4.
- 或者通过特殊的文件结尾字符,比如删除 data6.txt 文件内容中第 3 行开始的所有的内容:
[root@localhost ~]# sed '3,$d' data6.txt This is line number 1. This is line number 2.
sed a 和 i 脚本命令
a 命令表示在指定行的后面附加一行,i 命令表示在指定行的前面插入一行,这里之所以要同时介绍这 2 个脚本命令,因为它们的基本格式完全相同,如下所示:[address]a(或 i)\新文本内容
[root@localhost ~]# sed '3i\ > This is an inserted line.' data6.txt This is line number 1. This is line number 2. This is an inserted line. This is line number 3. This is line number 4.
[root@localhost ~]# sed '3a\ > This is an appended line.' data6.txt This is line number 1. This is line number 2. This is line number 3. This is an appended line. This is line number 4.
[root@localhost ~]# sed '1i\ > This is one line of new text.\ > This is another line of new text.' data6.txt This is one line of new text. This is another line of new text. This is line number 1. This is line number 2. This is line number 3. This is line number 4.
sed c 替换脚本命令
c 命令表示将指定行中的所有内容,替换成该选项后面的字符串。该命令的基本格式为:[address]c\用于替换的新文本
举个例子:[root@localhost ~]# sed '3c\ > This is a changed line of text.' data6.txt This is line number 1. This is line number 2. This is a changed line of text. This is line number 4.
在这个例子中,sed 编辑器会修改第三行中的文本,其实,下面的写法也可以实现此目的:
[root@localhost ~]# sed '/number 3/c\ > This is a changed line of text.' data6.txt This is line number 1. This is line number 2. This is a changed line of text. This is line number 4.
sed y 转换脚本命令
y 转换命令是唯一可以处理单个字符的 sed 脚本命令,其基本格式如下:[address]y/inchars/outchars/
举个简单例子:
[root@localhost ~]# sed 'y/123/789/' data8.txt This is line number 7. This is line number 8. This is line number 9. This is line number 4. This is line number 7 again. This is yet another line. This is the last line in the file.
转换命令是一个全局命令,也就是说,它会文本行中找到的所有指定字符自动进行转换,而不会考虑它们出现的位置,再打个比方:
[root@localhost ~]# echo "This 1 is a test of 1 try." | sed 'y/123/456/' This 4 is a test of 4 try.
sed p 打印脚本命令
p 命令表示搜索符号条件的行,并输出该行的内容,此命令的基本格式为:[address]p
p 命令常见的用法是打印包含匹配文本模式的行,例如:[root@localhost ~]# cat data6.txt This is line number 1. This is line number 2. This is line number 3. This is line number 4. [root@localhost ~]# sed -n '/number 3/p' data6.txt This is line number 3.
如果需要在修改之前查看行,也可以使用打印命令,比如与替换或修改命令一起使用。可以创建一个脚本在修改行之前显示该行,如下所示:
[root@localhost ~]# sed -n '/3/{ > p > s/line/test/p > }' data6.txt This is line number 3. This is test number 3.
sed w 脚本命令
w 命令用来将文本中指定行的内容写入文件中,此命令的基本格式如下:[address]w filename
下面的例子是将数据流中的前两行打印到一个文本文件中:
[root@localhost ~]# sed '1,2w test.txt' data6.txt This is line number 1. This is line number 2. This is line number 3. This is line number 4. [root@localhost ~]# cat test.txt This is line number 1. This is line number 2.
[root@localhost ~]# cat data11.txt Blum, R Browncoat McGuiness, A Alliance Bresnahan, C Browncoat Harken, C Alliance [root@localhost ~]# sed -n '/Browncoat/w Browncoats.txt' data11.txt cat Browncoats.txt Blum, R Browncoat Bresnahan, C Browncoat
sed r 脚本命令
r 命令用于将一个独立文件的数据插入到当前数据流的指定位置,该命令的基本格式为:[address]r filename
[root@localhost ~]# cat data12.txt This is an added line. This is the second added line. [root@localhost ~]# sed '3r data12.txt' data6.txt This is line number 1. This is line number 2. This is line number 3. This is an added line. This is the second added line. This is line number 4.
[root@localhost ~]# sed '$r data12.txt' data6.txt This is line number 1. This is line number 2. This is line number 3. This is line number 4. This is an added line. This is the second added line.
sed q 退出脚本命令
q 命令的作用是使 sed 命令在第一次匹配任务结束后,退出 sed 程序,不再进行对后续数据的处理。比如:
[root@localhost ~]# sed '2q' test.txt This is line number 1. This is line number 2.
[root@localhost ~]# sed '/number 1/{ s/number 1/number 0/;q; }' test.txt This is line number 0.
sed 脚本命令的寻址方式
前面在介绍各个脚本命令时,我们一直忽略了对 address 部分的介绍。对各个脚本命令来说,address 用来表明该脚本命令作用到文本中的具体行。默认情况下,sed 命令会作用于文本数据的所有行。如果只想将命令作用于特定行或某些行,则必须写明 address 部分,表示的方法有以下 2 种:
- 以数字形式指定行区间;
- 用文本模式指定具体行区间。
以上两种形式都可以使用如下这 2 种格式,分别是:
[address]脚本命令
或者
address {
多个脚本命令
}
以数字形式指定行区间
当使用数字方式的行寻址时,可以用行在文本流中的行位置来引用。sed 会将文本流中的第一行编号为 1,然后继续按顺序为接下来的行分配行号。在脚本命令中,指定的地址可以是单个行号,或是用起始行号、逗号以及结尾行号指定的一定区间范围内的行。这里举一个 sed 命令作用到指定行号的例子:
[root@localhost ~]#sed '2s/dog/cat/' data1.txt The quick brown fox jumps over the lazy dog The quick brown fox jumps over the lazy cat The quick brown fox jumps over the lazy dog The quick brown fox jumps over the lazy dog
[root@localhost ~]# sed '2,3s/dog/cat/' data1.txt The quick brown fox jumps over the lazy dog The quick brown fox jumps over the lazy cat The quick brown fox jumps over the lazy cat The quick brown fox jumps over the lazy dog
[root@localhost ~]# sed '2,$s/dog/cat/' data1.txt The quick brown fox jumps over the lazy dog The quick brown fox jumps over the lazy cat The quick brown fox jumps over the lazy cat The quick brown fox jumps over the lazy cat
用文本模式指定行区间
sed 允许指定文本模式来过滤出命令要作用的行,格式如下:/pattern/command
举个例子,如果你想只修改用户 demo 的默认 shell,可以使用 sed 命令,执行命令如下:
[root@localhost ~]# grep demo /etc/passwd demo:x:502:502::/home/Samantha:/bin/bash [root@localhost ~]# sed '/demo/s/bash/csh/' /etc/passwd root:x:0:0:root:/root:/bin/bash ... demo:x:502:502::/home/demo:/bin/csh ...
关于正则表达式,本节不做过多介绍,提供一个简单示例:
[root@localhost ~]# cat test.txt <html> <title>First Wed</title> <body> h1Helloh1 h2Helloh2 h3Helloh3 </body> </html> #使用正则表示式给所有第一个的h1、h2、h3添加<>,给第二个h1、h2、h3添加</> [root@localhost ~]# cat sed.sh /h[0-9]/{ s//\<&\>/1 s//\<\/&\>/2 } [root@localhost ~]# sed -f sed.sh test.txt <h1>Hello</h1> <h2>Hello</h2> <h3>Hello</h3>
曾有人推测 awk 命令的名字来源于 awkward 这个单词。其实不然,此命令的设计者有 3 位,他们的姓分别是 Aho、Weingberger 和 Kernighan,awk 就取自这 3 为大师姓的首字母。
和 sed 命令类似,awk 命令也是逐行扫描文件(从第 1 行到最后一行),寻找含有目标文本的行,如果匹配成功,则会在该行上执行用户想要的操作;反之,则不对行做任何处理。awk 命令的基本格式为:
[root@localhost ~]# awk [选项] '脚本命令' 文件名
选项 | 含义 |
---|---|
-F fs | 指定以 fs 作为输入行的分隔符,awk 命令默认分隔符为空格或制表符。 |
-f file | 从脚本文件中读取 awk 脚本指令,以取代直接在命令行中输入指令。 |
-v var=val | 在执行处理过程之前,设置一个变量 var,并给其设备初始值为 val。 |
awk 的强大之处在于脚本命令,它由 2 部分组成,分别为匹配规则和执行命令,如下所示:
'匹配规则{执行命令}'
在 awk 程序执行时,如果没有指定执行命令,则默认会把匹配的行输出;如果不指定匹配规则,则默认匹配文本中所有的行。
举个简单的例子:[root@localhost ~]# awk '/^$/ {print "Blank line"}' test.txt
/^$/
是一个正则表达式,功能是匹配文本中的空白行,同时可以看到,执行命令使用的是 print 命令,此命令经常会使用,它的作用很简单,就是将指定的文本进行输出。因此,整个命令的功能是,如果 test.txt 有 N 个空白行,那么执行此命令会输出 N 个 Blank line。awk 使用数据字段变量
awk 的主要特性之一是其处理文本文件中数据的能力,它会自动给一行中的每个数据元素分配一个变量。默认情况下,awk 会将如下变量分配给它在文本行中发现的数据字段:
- $0 代表整个文本行;
- $1 代表文本行中的第 1 个数据字段;
- $2 代表文本行中的第 2 个数据字段;
- $n 代表文本行中的第 n 个数据字段。
前面说过,在 awk 中,默认的字段分隔符是任意的空白字符(例如空格或制表符)。 在文本行中,每个数据字段都是通过字段分隔符划分的。awk 在读取一行文本时,会用预定义的字段分隔符划分每个数据字段。
所以在下面的例子中,awk 程序读取文本文件,只显示第 1 个数据字段的值:
[root@localhost ~]# cat data2.txt One line of test text. Two lines of test text. Three lines of test text. [root@localhost ~]# awk '{print $1}' data2.txt One Two Three
awk 脚本命令使用多个命令
awk 允许将多条命令组合成一个正常的程序。要在命令行上的程序脚本中使用多条命令,只要在命令之间放个分号即可,例如:[root@localhost ~]# echo "My name is Rich" | awk '{$4="Christine"; print $0}' My name is Christine
除此之外,也可以一次一行地输入程序脚本命令,比如说:
[root@localhost ~]# awk '{ > $4="Christine" > print $0}' My name is Rich My name is Christine
注意,此例中因为没有在命令行中指定文件名,awk 程序需要用户输入获得数据,因此当运行这个程序的时候,它会一直等着用户输入文本,此时如果要退出程序,只需按下 Ctrl+D 组合键即可。
awk从文件中读取程序
跟 sed 一样,awk 允许将脚本命令存储到文件中,然后再在命令行中引用,比如:[root@localhost ~]# cat awk.sh {print $1 "'s home directory is " $6} [root@localhost ~]# awk -F: -f awk.sh /etc/passwd root's home directory is /root bin's home directory is /bin daemon's home directory is /sbin adm's home directory is /var/adm lp's home directory is /var/spool/lpd ... Christine's home directory is /home/Christine Samantha's home directory is /home/Samantha Timothy's home directory is /home/Timothy
awk BEGIN关键字
awk 中还可以指定脚本命令的运行时机。默认情况下,awk 会从输入中读取一行文本,然后针对该行的数据执行程序脚本,但有时可能需要在处理数据前运行一些脚本命令,这就需要使用 BEGIN 关键字。BEGIN 会强制 awk 在读取数据前执行该关键字后指定的脚本命令,例如:
[root@localhost ~]# cat data3.txt Line 1 Line 2 Line 3 [root@localhost ~]# awk 'BEGIN {print "The data3 File Contents:"} > {print $0}' data3.txt The data3 File Contents: Line 1 Line 2 Line 3
awk END关键字
和 BEGIN 关键字相对应,END 关键字允许我们指定一些脚本命令,awk 会在读完数据后执行它们,例如:[root@localhost ~]# awk 'BEGIN {print "The data3 File Contents:"} > {print $0} > END {print "End of File"}' data3.txt The data3 File Contents: Line 1 Line 2 Line 3 End of File