Linux学习-Linux三剑客(一)

一、Linux三剑客之-grep

1、grep命令详解

grep 命令的基本格式如下:

[root@localhost ~]# grep [选项] 模式 文件名

grep 命令支持的正则表达式的元字符

通配符 功能
c* 将匹配 0 个(即空白)或多个字符 c(c 为任一字符)。
. 将匹配任何一个字符,且只能是一个字符。
[xyz] 匹配方括号中的任意一个字符。
[^xyz] 匹配除方括号中字符外的所有字符。
^ 锁定行的开头。
$ 锁定行的结尾。

命令常用的选项以及各自的含义

选项 含义
-c 仅列出文件中包含模式的行数。
-i 忽略模式中的字母大小写。
-l 列出带有匹配行的文件名。
-n 在每一行的最前面列出行号。
-v 列出没有匹配模式的行。
-w 把表达式当做一个完整的单字符来搜寻,忽略那些部分匹配的行。

假设有一份 emp.data 员工清单,现在要搜索此文件,找出职位为 CLERK 的所有员工,则执行命令如下:

[root@localhost ~]# grep CLERK emp.data

如果只想知道职位为 CLERK 的员工的人数,可以使用“-c”选项,执行命令如下:

[root@localhost ~]# grep -c CLERK emp.data

搜索 emp.data 文件,使用正则表达式找出以 78 开头的数据行

[root@localhost ~]# grep ^78 emp.data

二、Linux三剑客之-sed

1、sed命令

Vim 采用的是交互式文本编辑模式,你可以用键盘命令来交互性地插入、删除或替换数据中的文本。但 sed 命令不同,它采用的是流编辑模式,最明显的特点是,在 sed 处理数据之前,需要预先提供一组规则,sed 会按照此规则来编辑数据。

sed 会根据脚本命令来处理文本文件中的数据,这些命令要么从命令行中输入,要么存储在一个文本文件中,此命令执行数据的顺序如下:

  1. 每次仅读取一行内容;
  2. 根据提供的规则命令匹配并修改数据。注意,sed 默认不会直接修改源文件数据,而是会将数据复制到缓冲区中,修改也仅限于缓冲区中的数据;
  3. 将执行结果输出。

sed 命令的基本格式如下:

[root@localhost ~]# sed [选项] [脚本命令] 文件名

sed 命令常用选项及含义

选项 含义
-e 脚本命令 该选项会将其后跟的脚本命令添加到已有的命令中。
-f 脚本命令文件 该选项会将其后文件中的脚本命令添加到已有的命令中。
-n 默认情况下,sed 会在所有的脚本指定执行完毕后,会自动输出处理后的内容,而该选项会屏蔽启动输出,需使用 print 命令来完成输出。
-i 此选项会直接修改源文件,要慎用。

2、sed脚本命令
sed s 替换脚本命令
命令的基本格式为:

[address]s/pattern/replacement/flags
address 表示指定要操作的具体行,pattern 指的是需要替换的内容,replacement 指的是要替换的新内容。

sed s命令flags标记及功能

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.
使用数字 2 作为标记的结果就是,sed 编辑器只替换每行中第 2 次出现的匹配模式。

替换所有匹配的字符串:g标记

[root@localhost ~]# sed ‘s/test/trial/g’ data4.txt

只输出被替换命令修改过的行:-n ,p

[root@localhost ~]# sed -n ‘s/test/trial/p’ data5.txt

-n 选项会禁止 sed 输出,但 p 标记会输出修改过的行,这是将二者匹配使用的效果

将匹配后的结果保存到指定文件中:w标记

sed ‘s/test/trial/w test.txt’ data5.txt

正斜线进行转义:

[root@localhost ~]# sed ‘s/\/bin\/bash/\/bin\/csh/’ /etc/passwd

sed d 替换脚本命令
命令的基本格式为:

[address]d

如果需要删除文本中的特定行,可以用 d 脚本命令,它会删除指定行中的所有内容。但使用该命令时要特别小心,如果你忘记指定具体行的话,文件中的所有内容都会被删除。

 sed ‘d’ data1.txt

通过行号指定,删除 data6.txt 文件内容中的第 3 行:

[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.

删除两个指定行之间的所有行(包括指定的行):

[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  等价实现

sed y 转换脚本命令
y 转换命令是唯一可以处理单个字符的 sed 脚本命令

基本格式如下:

[address]y/inchars/outchars/

转换命令会对 inchars 和 outchars 值进行一对一的映射,即 inchars 中的第一个字符会被转换为 outchars 中的第一个字符,第二个字符会被转换成 outchars 中的第二个字符…这个映射过程会一直持续到处理完指定字符。如果 inchars 和 outchars 的长度不同,则 sed 会产生一条错误消息。

[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.
inchars 模式中指定字符的每个实例都会被替换成 outchars 模式中相同位置的那个字符。

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.

用 -n 选项和 p 命令配合使用,我们可以禁止输出其他行,只打印包含匹配文本模式的行。

如果需要在修改之前查看行,也可以使用打印命令,比如与替换或修改命令一起使用。可以创建一个脚本在修改行之前显示该行,如下所示:

[root@localhost ~]# sed -n ‘/3/{
> p
> s/line/test/p
> }’ data6.txt
This is line number 3.  (原来的行文本)
This is test number 3.  (新的行文本)

sed 命令会查找包含数字 3 的行,然后执行两条命令。首先,脚本用 p 命令来打印出原始行;然后它用 s 命令替换文本,并用 p 标记打印出替换结果。输出同时显示了原来的行文本和新的行文本。
sed w 脚本命令:通过使用 w 脚本命令,sed 可以实现将包含文本模式的数据行写入目标文件。
命令的基本格式如下:

[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.

不想让行直接输出,可以用 -n 选项

[root@localhost ~]# sed -n ‘/Browncoat/w Browncoats.txt’ data11.txt
cat Browncoats.txt
Blum, R       Browncoat
Bresnahan, C  Browncoat

sed r 脚本命令
该命令的基本格式为:

[address]r filename

命令会将 filename 文件中的内容插入到 address 指定行的后面

[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.

可以看到,sed 命令在打印输出第 2 行之后,就停止了,是 q 命令造成的,再比如:

[root@localhost ~]# sed ‘/number 1/{ s/number 1/number 0/;q; }’ test.txt
This is line number 0.

sed 命令会在匹配到 number 1 时,将其替换成 number 0,然后直接退出。

3、sed 脚本命令的寻址方式

默认情况下,sed 命令会作用于文本数据的所有行。如果只想将命令作用于特定行或某些行,则必须写明 address 部分,表示的方法有以下 2 种:

  1. 以数字形式指定行区间;
  2. 用文本模式指定具体行区间。

以上两种形式都可以使用如下这 2 种格式,分别是:

[address]脚本命令
或者
address {
多个脚本命令
}

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,$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

注意,必须用正斜线将要指定的 pattern 封起来,sed 会将该命令作用到包含指定文本模式的行上。

只修改用户 demo 的默认 shell

[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
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容