Sed 简介
sed 是一种新型的,非交互式的编辑器。它能执行与编辑器 vi 和 ex 相同的编辑任务。sed 编辑器没有提供交互式使用方式,使用者只能在命令行输入编辑命令、指定文件名,然后在屏幕上查看输出。 sed 编辑器没有破坏性,它不会修改文件,除非使用 shell 重定向来保存输出结果。默认情况下,所有的输出行都被打印到屏幕上。
sed 工作过程
sed 编辑器逐行处理文件(或输入),并将输出结果发送到屏幕。 sed 的命令就是在 vi和 ed/ex 编辑器中见到的那些。 sed 把当前正在处理的行保存在一个临时缓存区中,这个缓存区称为模式空间或临时缓冲。sed 处理完模式空间中的行后(即在该行上执行 sed 命令后),就把改行发送到屏幕上(除非之前有命令删除这一行或取消打印操作)。 sed 每处理完输入文件的最后一行后, sed 便结束运行。 sed 把每一行都存在临时缓存区中,对这个副本进行编辑,所以不会修改或破坏源文件。如图 1: sed 处理过程。
从上图可以看出 sed 不是破坏性的,它不会修改正在编辑的文件。
sed修改的文件重定向输出
sed [选项] 'sed命令' input-file > output-file |
Sed 命令格式(三种)
1、在Shell 命令行输入命令调用sed
sed [选项] 'sed命令' 输入文件 |
|
Note:sed命令要用单引号引起来。
2、将sed命令插入到脚本文件中;然后通过sed命令调用它。
sed [选项] -f sed脚本文件 输出文件 |
3、将sed命令插入到脚本文件中;最常用的方法就是设置该脚本文件为可执行;然后直接执行该脚本文件。
./sed脚本文件 输入文件 |
第二种的sed的脚本文件和第三种不同;
sed命令的常用选项以及意义
选项 | 说明 |
-n | 加入-n 后只打印被 sed 特殊处理的行;不打印所有的行到标准的输出。 |
-e | 如果只是传递一个编辑命令给sed,-e选项可以省略。多重编辑的时候,需要使用 -e选项。 |
-f | 表示一个sed脚本文件正在执行。 |
-r | Sed 使用扩展正则。 |
-i | 直接修改文档读取的内容,不在屏幕上输出。 |
sed命令通常是由sed定位文本和sed编辑命令两部分组成。sed编辑命令会对定位的文本行进行各种处理。
Sed 定位文本(两种方式)
- 使用行号,指定一行;或者指定行号的范围。
- 使用正则表达式。
选项 | 说明 |
x | x 为指定的行号 |
x,y | 指定从x 到 y的行号范围 |
/regexp/ | 查询包含匹配正则的行;通过/ /之间的正则来匹配 |
/regexp/regexp/ | 查询包含两个模式的行 |
x,/regexp/ | 从x 行号到与regexp匹配的行之间的行 |
x,y! | 查询不包括 x和y行号的行 |
$ | 这个$符表示匹配最后一行 |
Sed 的正则表达式
元字符 | 功 能 | 示 例 | 示例的匹配对象 |
^ | 行首定位符 | /^love/ | 匹配所有以 love 开头的行 |
$ | 行尾定位符 | /love$/ | 匹配所有以 love 结尾的行 |
. | 匹配除换行外的单 个字符 | /l..e/ | 匹配包含字符 l、后跟两个任意 字符、再跟字母 e 的行 |
* | 匹配零个或多个前 导字符 | /*love/ | 匹配在零个或多个空格紧跟着 模式 love 的行 |
[] | 匹配指定字符组内 任一字符 | /[Ll]ove/ | 匹配包含 love 和 Love 的行 |
[^] | 匹配不在指定字符 组内任一字符 | /[^A-KM-Z]ove/ | 匹配包含 ove,但 ove 之前的那 个字符不在 A 至 K 或 M 至 Z 间 的行 |
\(..\) | 保存已匹配的字符 | ||
& | 保存查找串以便在 替换串中引用 | s/love/**&**/ | 符号&代表查找串。字符串 love 将替换前后各加了两个**的引 用,即 love 变成**love** |
\< | 词首定位符 | /\<love/ | 匹配包含以 love 开头的单词的 行 |
\> | 词尾定位符 | /love\>/ | 匹配包含以 love 结尾的单词的 行 |
x\{m\} | 连续 m 个 x | /o\{5\}/ | 分别匹配出现连续 5 个字母 o、 至少 5 个连续的 o、或 5~10 个 连续的 o 的行 |
x\{m,\} | 至少 m 个 x | /o\{5,\}/ | |
x\{m,n\} | 至少 m 个 x,但不 超过 n 个 x | /o\{5,10\}/ |
Sed 编辑命令
sed 编辑命令告诉 sed 如何处理由地址指定的各输入行。如果没有指定地址, sed 就会处理输入的所有的行。
命 令 | 说 明 |
p | 打印匹配行信息 |
= | 打印文件的行号 |
a\ | 在定位行号之后追加文本信息 |
i\ | 在定位行号之前插入文本信息 |
d | 删除定位行 |
c\ | 用新文本修改(替换)定位文本 |
s | 用一个字符串替换另一个 |
r | 从另一个文件中读取文本 |
w | 将文本写入到一个文件 |
y | 变换字符;将字符转换为另一字符(不能对正则表达式使用 y 命令) |
q | 第一个模式匹配完成后 结束或退出 sed |
l | 显示与八进制ASCII码等价的控制字符 |
{} | 在定位行执行的命令组 |
n | 读取下一个输入行,用下一个命令处理新的行 |
h | 把模式缓冲区的内容复制到暂存缓存区 |
H | 把模式缓冲区的内容追加到暂存缓存区 |
x | 交换模式缓冲区和暂存缓存区内的容 |
g | 取出暂存缓冲区里的内容,将其复制到模式缓冲区,覆盖该处原有内容 |
G | 取出暂存缓冲区里的内容,将其复制到模式缓冲区,追加在原有内容后面 |
! | 对所选行以外的所有行应用命令 |
4.2 sed使用实例
下面给出测试文件作为输入文件:ceshi.txt
1 2 3 4 5 6 7 8 9 10 | [hadoop@netcloud01 ysj]$ cat ceshi.txt northwest NW Charles Main 3.0 .98 3 34 western WE Sharon Gray 5.3 .97 5 23 southwest SW Lewis Dalsass 2.7 .8 2 18 southern SO Suan Chin 5.1 .95 4 15 southeast SE Patricia Hemenway 4.0 .7 4 17 eastern EA TB Savage 4.4 .84 5 20 northeast NE AM Main Jr. 5.1 .94 3 13 north NO Margot Weber 4.5 .89 5 9 central CT Ann Stephens 5.7 .94 5 13 |
4.2.1 sed命令选项的实例
这里需要结合几个编辑命令才能体现其作用;但是重点还是了解命令选项的情况。
1、sed命令的 -n选项
- 不使用 -n 不但打印出被sed命令特殊处理的行信息;还会打印文本的全部内容
- 加入 -n 只会打印被sed特殊处理的行信息。
//不带 -n 不仅打印出被sed处理后的行信息,还会打印所有的行信息
[hadoop@netcloud01 ysj]$ sed '1p' ceshi.txt
northwest NW Charles Main 3.0 .98 3 34
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
//带 -n 只打印第一行信息
[hadoop@netcloud01 ysj]$ sed -n '1p' ceshi.txt
northwest NW Charles Main 3.0 .98 3 34
sed命令打印范围行
//打印测试文件的 2,3,4行信息
[hadoop@netcloud01 ysj]$ sed -n '2,4p' ceshi.txt
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
sed命令打印匹配模式行(正则表达式)
// 打印行中包含north字符的行信息
[hadoop@netcloud01 ysj]$ sed -n '/north/p' ceshi.txt
northwest NW Charles Main 3.0 .98 3 34
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
2、sed命令的 -e 选项
如果只传递一个编辑命令给sed, -e选项是可以省略的,只有当向sed命令传递多个编辑命令的时候 -e 才能有用武之地。
// 简单的打印行号命令
[hadoop@netcloud01 ysj]$ sed -n '/north/=' ceshi.txt
1
7
8
// 打印行信息和行号 传递两个编辑命令给sed 使用 -e 选项
[hadoop@netcloud01 ysj]$ sed -n -e '/north/=' -e '/north/p' ceshi.txt
1
northwest NW Charles Main 3.0 .98 3 34
7
northeast NE AM Main Jr. 5.1 .94 3 13
8
north NO Margot Weber 4.5 .89 5 9
Note:sed不支持同时带多个编辑命令的用法。 sed -n '/north/p=' ceshi.txt
带多个编辑命令sed的一般格式 sed [选项] -e 编辑命令1 -e 编辑命令2 ... -e 编辑命令n 输入文件
3、sed命令的 -f 选项
-f 选项只有调用sed脚本文件的时候才起作用。
追加,插入,修改,删除和替换文本等功能往往需要多条sed命令来完成,所有需要将这些命令写入sed脚本,然后调用sed脚本完成。
// sed '指定位置 a\追加内容' ceshi.txt
// 使用 a\ 编辑命令 在与17字符相匹配行的后面追加文本 This is our append content!
// 如果没有指定位置 会在每行进行追加。
[hadoop@netcloud01 ysj]$ sed '/17/a\This is our append content!' ceshi.txt
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
This is our append content!
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
将以上的实现的功能添加到sed脚本中去执行。 append.sed
// sed 脚本的第一行和bash shell脚本一样;都是以 #!符号开头 后面是解释器的路径
// 一般在/bin 目录下 如果不知道在哪个目录下 使用 which sed命令查找。
// -f 在脚本文件中不能少,否则报错。如果追加的内容有多行,需要使用 \ 进行换行。
// 授予脚本文件为可执行权限 (chmod u+x ceshi.txt);然后执行脚本(执行方法和bash脚本一样)
[hadoop@netcloud01 ysj]$ vi append.sed
#! /bin/sed -f
/17/a\
This is our append content! \
This is our append content!
[hadoop@netcloud01 ysj]$ chmod u+x append.sed
[hadoop@netcloud01 ysj]$ ./append.sed ceshi.txt
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
This is our append content!
This is our append content!
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
4.2.2 sed 文本定位的实例
1、匹配元字符
需要使用转义字符 "\" 屏蔽其特殊的含义。
// 匹配 . 符号
[hadoop@netcloud01 ysj]$ sed -n '/\./p' ceshi.txt
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
// $在正则表达式中表示行尾,但是在sed命令中表示最后一行
// sed的编辑命令可以放在单引号内,也可以放在单引号外面 如 :p
[hadoop@netcloud01 ysj]$ sed -n '$p' ceshi.txt
central CT Ann Stephens 5.7 .94 5 13
[hadoop@netcloud01 ysj]$ sed -n '$'p ceshi.txt
central CT Ann Stephens 5.7 .94 5 13
// 使用元字符进行任意字符的匹配
[hadoop@netcloud01 ysj]$ sed -n '/.*east/'p ceshi.txt
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
2、!符号
!符号表示取反。x,y! 表示匹配不在x和y行号范围内的行。
[hadoop@netcloud01 ysj]$ sed -n '3,5!p' ceshi.txt
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
3、使用行号和关键字匹配限定行的范围
使用 x,/parttern/ 或者 /parttern/,y 两种形式,去限制行号和关键字匹配行之间的范围。实际上这两种形式与 x,y是一样的,只是x或y用 /parttern/代替而已。
// 打印与Margot Weber匹配的行到最后一行
[hadoop@netcloud01 ysj]$ sed -n '/Margot Weber/,$p' ceshi.txt
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
// 打印出第7行到与Margot Weber匹配的行
[hadoop@netcloud01 ysj]$ sed -n '7,/Margot Weber/p' ceshi.txt
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
4.2.3 sed 编辑命令的实例
sed编辑命令是sed编程中最常见的命令,只有深刻的了解了这些命令的用法,才能自如的利用sed进行Shell编程。
上面我们介绍了文本追加命令( \a),现在我们从插入文本命令( \i )开始。
1、插入文本 ( i )
插入文本和追加文本类似,只是插入的顺序不同,追加文本是在匹配行的后面插入,而插入文本是在匹配行的前面插入。
格式:sed '指定地址 i\ text' 输入文件
新建 insert.sed 脚本
#! /bin/sed -f
/Margot Weber/i\ #表示此处换行插入文本
ysj #插入文本的内容
授权执行: chmod u+x insert.sed ./insert.sed ceshi.txt
可以看到在Margot Weber行前面插入了新的文本。
[hadoop@netcloud01 ysj]$ ./insert.sed ceshi.txt
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
ysj
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
2、修改文本 ( c )
将匹配的文本行用新的文本替代。
命令格式: sed '指定地址 c\text' 输入文件
新建 modify.sed 脚本
[hadoop@netcloud01 ysj]$ vi modify.sed
#! /bin/sed -f
/Margot Weber/c\ # c\ 表示此处是换行修改文本
This is my modify line! # 修改的文本内容
授权执行: chmod u+x modify.sed ./modify.sed ceshi.txt
[hadoop@netcloud01 ysj]$ ./modify.sed ceshi.txt
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
This is my modify line!
central CT Ann Stephens 5.7 .94 5 13
3、删除文本 ( d )
sed删除命令可以将指定的行或者将指定的行的范围进行删除 并打印剩余的文本信息;不使用 -n 选项。
命令格式: sed '指定地址 d' 输入文件
删除文本文件第一行的数据: sed '1d' ceshi.txt
删除文本文件最后一行的数据: sed '$d' ceshi.txt
删除文本文件第1行到第10行的数据: sed '1,10d' ceshi.txt
删除文本文件第5行到最后一行的数据: sed '5,$d' ceshi.txt
删除与关键字匹配的行:Margot
sed '/[Mm][Aa][Rr][Gg][Oo][Tt]/d' ceshi.txt
[hadoop@netcloud01 ysj]$ sed '/[Mm][Aa][Rr][Gg][Oo][Tt]/d' ceshi.txt
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
central CT Ann Stephens 5.7 .94 5 13
4、替换文本 ( s )
替换文本和修改文本相似,但是替换文本可以替换一个字符串,而修改文本是对整行进行新修改。
另外替换文本的替换选项使得文本替换更加灵活。
命令格式: sed 's/被替换的字符串/新字符串/[替换选项]' 输入文件
替换选项:
选项 | 说明 |
g | g 表示在行内全局替换;也就是说如果每一行里出现多个west,所有的 west 都会被替换为 north。如果没有 g 命令,则只将每一行的第一 west 替换为 north。 |
p | 和 -n 结合使用 只打印替换行 |
w | 表示输出定向到一个文件 |
Note:
1)默认情况下 sed s命令将替换后的全部文本都输出
2)如果要求只输出替换的行,需要结合使用 -n 和p 选项。
3)如果缺少p选项,将不会打印任何内容,使用 -n 无效。
// 默认情况下打印全部的输入内容
[hadoop@netcloud01 ysj]$ sed 's/Charles/CHARLES/' ceshi.txt
northwest NW CHARLES Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
// 用了 -n参数 但是没有加 p选项 结果是不打印任何内容
[hadoop@netcloud01 ysj]$ sed -n 's/Charles/CHARLES/' ceshi.txt
// -n p 结合使用只打印替换行
[hadoop@netcloud01 ysj]$ sed -n 's/Charles/CHARLES/p' ceshi.txt
northwest NW CHARLES Main 3.0 .98 3 34
w 选项(其后面加文件名表示将输出定向到这个文件,如果输出文件没有创建,sed命令自动建立输出文件。)
sed -n 's/oldworld/newword/w 输出文件名' 输入文件名
[hadoop@netcloud01 ysj]$ sed -n 's/Charles/CHARLES/w output.txt' ceshi.txt
[hadoop@netcloud01 ysj]$ cat output.txt
northwest NW CHARLES Main 3.0 .98 3 34
Note:sed替换文本的时候经常使用到 &符号 ;&符号可用来保存被替换的字符串以供调用。
//使用sed -n 's/northwest/(&)/pg' ceshi.txt
//等同于 sed -n 's/northwest/(northwest)/pg' ceshi.txt
[hadoop@netcloud01 ysj]$ sed -n 's/northwest/(&)/pg' ceshi.txt
(northwest) NW Charles Main 3.0 .98 3 34
5、写入一个新文本 ( w )
格式: sed '指定位置 w 输出文件' 输入文件
sed -n '1,5 w output.txt' ceshi.txt :将前5行的数据输出到 output.txt 文件中
sed -n '/Charles/w output.txt' ceshi.txt :将匹配Charles行的数据输出到 output.txt 文件中
[hadoop@netcloud01 ysj]$ cat output.txt
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
[hadoop@netcloud01 ysj]$ sed -n '/Charles/w output.txt' ceshi.txt
[hadoop@netcloud01 ysj]$ cat output.txt
northwest NW Charles Main 3.0 .98 3 34
6、从文件中读入文本 (r)
格式: sed -n '指定位置 r 输入文件 ' 文本文件
创建一个输入文件: otherfile.txt
This is the first line of the otherfile.
This is the second line of the otherfile.
sed -n '/Charles/r otherfile.txt' ceshi.txt :在匹配到Charles行的后面追加otherfile.txt 文本文件中的内容。
[hadoop@netcloud01 ysj]$ sed '/Charles/r otherfile.txt' ceshi.txt
northwest NW Charles Main 3.0 .98 3 34
This is the first line of the otherfile.
This is the second line of the otherfile.
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
7、退出命令 (q)
sed '指定位置 q' 文本文件
只要找到指定位置的内容就打印之前的数据信息,并退出。
8、变换命令 (y)
格式: sed 'y/被变换的字符串序列/变换的字符序列/' 输入文件
Note:被变换的字符串序列和变换的字符序列等长度。否则会报错。
sed y 将被变换的字符序列逐个用变换的字符序列中的字符替换。
9、显示控制字符 (l)
sed -n '1,$l' 输入文件 :显示输入文件中第一行到最后一行 控制字符(空格、F1键、Shift键)。
10、在定位行执行命令组 ( { })
它的作用和 sed -e 类似 都是为了在定位行执行多个编辑命令:
sed -n -e '/Charles/p' -e '/Charles/=' ceshi.txt :打印匹配内容的行号和具体信息
等价于: sed -n '/Charles/{p;=}' ceshi.txt
[hadoop@netcloud01 ysj]$ sed -n '/Charles/{p;=}' ceshi.txt
northwest NW Charles Main 3.0 .98 3 34
1
4.2.4 sed 高级编辑命令例子
1、处理匹配行的下一行
sed编辑命令 n的 意义就是读取下一个输入行,用n后面的命令处理该行。此时有多个编码命令,所有编辑命令 n 需要和 {}结合使用。
sed '/Charles/ {n; s/5.3/9.9/}' ceshi.txt :找出关键字Charles的匹配行,然后在匹配行的下一行执行s/t/T/p 命令
将t字符用T替换
[hadoop@netcloud01 ysj]$ sed -n '/Charles/{n;s/t/T/p}' ceshi.txt
wesTern WE Sharon Gray 5.3 .97 5 23
2、利用分号分割多个编辑命令
等同于 -e 和 {}
4.3 awk编程
awk是一种编程语言,gawk是目前最新的版本,当前的Linux版本用的都是gawk,这里我们对awk和gawk不在区分,统称awk。
awk可以实现数据的查找,抽取文件中的数据,创建管道流命令等功能。
4.3.1 awk调用方法
调用awk方法和sed类似。也有三种方式。一种是shell命令行,另外两种是将awk程序写入脚本文件,然后执行此脚本。
- 在shell命令行输入命令调用awk格式:
awk [-F 域分隔符] 'awk程序段' 输入文件
- 将awk程序段插入脚本文件,然后通过awk命令调用它。
awk -f awk脚本文件 输入文件
- 将sed命令插入脚本文件后,设置脚本文件可执行,然后直接执行此脚本文件。
./awk脚本文件 输入文件
4.3.2 awk编程例子
1、awk模式匹配
任何awk语句都由模式和动作组成。模式决定着动作何时触发,动作执行对输入行的处理。
[hadoop@netcloud01 ysj]$ awk '/^$/{print "This is a blank line."}' ceshi.txt
This is a blank line.
使用shell命令行对输入文件ceshi.txt的空白行打印 This is a blank line.
将上述命令写到脚本文件中。
[hadoop@netcloud01 ysj]$ vi scr.awk
/^$/{print "This is a blank line."}
[hadoop@netcloud01 ysj]$ awk -f scr.awk ceshi.txt
This is a blank line.
直接执行awk脚本,在脚本文件中开始处添加 #!
[hadoop@netcloud01 ysj]$ cat scr.awk
#! /bin/awk -f
/^$/{print "This is a blank line."}
[hadoop@netcloud01 ysj]$ chmod u+x scr.awk
[hadoop@netcloud01 ysj]$ ./scr.awk ceshi.txt
This is a blank line.
模式匹配不只是像这么简单,在进一步讲解awk模式匹配前,先了解awk的记录和域。
2、记录和域
awk将每个输入文件行定义为记录,行中的每个字符定义为域,分割域的符号叫分隔符(空格,Tab或其他字符)。
两个或者多个连续的分隔符都当做一个分隔符处理。对文本文件分域处理是Linux系统中很多命令都使用的方法。
生产环境案例
在实际生产中,在修改配置文件的时候,有一些空格、空行、带“ #”开头的注释都要删除或替换,下面为大家介绍几个实用的例子
案例 23:
1 2 3 4 5 |
|
1 2 3 4 5 6 7 8 9 10 |
|
案例24:删除文本中空行和空格组成的行及#号注释的行
1 2 3 4 5 6 7 8 |
|
案例 25:从 Google 上下载下来的配置文件往往都带有数字,现在需要删除所有行的首数字。
1 2 3 4 5 6 7 8 |
|
总结
本章内容总结了 sed 命令的用法,前面部分是 sed 命令的语法,后面部分则主要以实际案例来说明 sed 的用法,最后面一点介绍了 sed 命令在生产实践中的运用。所谓学为练,练为战,希望大家能够将 sed 命令勤加练习,必将会在工作中有所用途,尤其是频繁的分析日志文件, Awk+Sed 是比较好的组合。最后希望本文对大家有所帮助,真正达到熟练的程度这就靠大家在工作中归纳总结了。