sed
sed 编辑器 sed编辑器被称作流编辑器(stream editor),和普通的交互式文本编辑器恰好相反。在交互式文本编辑器中(比如vim),你可以用键盘命令来交互式地插入、删除或替换数据中的文本。流编辑器则会在编辑器处理数据之前基于预先提供的一组规则来编辑数据流。sed编辑器可以根据命令来处理数据流中的数据,这些命令要么从命令行中输入,要么存储在一个命令文本文件中。sed编辑器会执行下列操作
(1) 一次从输入中读取一行数据。
(2) 根据所提供的编辑器命令匹配数据。
(3) 按照命令修改流中的数据。
(4) 将新的数据输出到STDOUT。
在流编辑器将所有命令与一行数据匹配完毕后,它会读取下一行数据并重复这个过程。在流编辑器处理完流中的所有数据行后,它就会终止。
sed命令的格式如下。 sed options script file
sed [options] 'command' file(s) sed [options] -f scriptfile file(s) d :删除符合条件的行 p : 显示符合条件的行 -n :只显示符合条件的行,和p 配合使用 -n或--quiet或——silent:仅显示script处理后的结果 s :查找并且替换 sed '1,2d' /etc/fstab |wc -l # 删除指定文件fstab 中的1.2两行,将模式空间打印在屏幕上 sed '/oot/d' /etc/fstab |wc -l # 删除包含 oot 的行 选定行的范围 '/字符串/' sed ' /^//d' /etc/fstab |wc -l #删除以 / 开头的行 ,需要用 进行转义 sed -n ' /^//p' /etc/fstab #只显示匹配的行,显示处理后的结果
------------------------------------------------------------
sed -e 's/brown/green/; s/dog/cat/' data1.txt
$ sed -e '
> s/brown/green/
> s/fox/elephant/
> s/dog/cat/' data1.txt
#使用多个编辑命令
--------------------------------------------------
$ cat script1.sed
s/brown/green/
s/fox/elephant/
s/dog/cat/
$
$ sed -f script1.sed data1.txt
#从文件读取编辑命令
--------------------------------------------- 换行符 sed -n ' /^//a##################################' /etc/fstab #astring 在当前行下面插入文本。为在以 开头的行下卖弄插入一行##.... sed -n ' /^//i##################################' /etc/fstab istring 在当前行上面插入文本。 sed ' /^//i################################## $$$$$$$$$$$$$$$$$$$$$$$' /etc/fstab r :file 从file中读行。将指定文件内容添加到符合条件的行处 w :将指定范围内的内容另存至指定文件中 sed '/oot/w out_oot.txt' /etc/fstab #在fstab 文件中过滤包含oot 的行,w 能够将滤出来的行保存至指定文件夹。 sed 's/oot/OOT/' /etc/fstab # s 替换每行中第一次被模式匹配到的字符串 修饰符: g: 全局替换
数字:指定的行 sed 's/oot/OOT/g' /etc/fstab i: 查找时忽略大小写 s 替换可以使用其他字符作为分隔符,如: s@@@ , s### & :引用模式匹配整个串 sed 's@l..e@&r@g' sed_test.txt #查找l..e 的字符串后面加r 字符 ".." 为模式匹配任意字符 sed 's@l(..e)@L1@g' sed_test.txt 反向引用替换某个单词中的某个字符 -i :-i 选项直接修改源文件 -e : 可以同时执行多个脚本 -f : 调用脚本文件处理 列如: sed /path/sed.script file sed '$d' file # 删除最后一行 sed -i '$d' /etc/fstab # 直接在源文件进行修改,删除最后一行 history |sed 's@^[[:space:]]@@g' |cut -d ' ' -f1 取出history 的序号,删除空格 sed 练习: 1. 删除 /etc/grub.conf 文件中行首的空白字符 sed -r 's@^[[:space:]]+@@g' /etc/grub.conf 2.替换 /etc/inittab 文件中的"id:3:initdefault" 中的数字为5 sed 's@(id:)[0-9](:initdefault:)@152@g' /etc/inittab sed -n 's@(id:)[0-9](:initdefault:)@152@g'p /etc/inittab #-n 和p 参数组合使用,只显示修改的参数 #-n选项和p命令一起使用表示只打印那些发生替换的行 3.删除 /etc/inittab 中的空白行 sed '/^$/d' /etc/inittab 4.删除 /etc/inittab 文件中开头的#号 sed 's@^#@@g' /etc/inittab 5.删除文件中以# 号开头的及后面的空白字符,且必须含有空白字符才执行。 sed -r 's@^#[[:space:]]+@@g' /etc/inittab #### -r 参数表示在脚本中使用扩展正则表达式 6.删除空白字符及# 号开头的部分 sed -r 's@^[[:space:]]+#@@g' /etc/inittab 7,取出一个文件中的目录名称 echo "/etc/rc.d" |sed -r 's@^(/.*/)[^/]+/?@1@g'
sed 进阶
多行命令
N:将数据流中的下一行加进来创建一个多行组(multiline group)来处理。
D:删除多行组中的一行。
P:打印多行组中的一行
单行 next 命令
[root@localhost gawk]# cat data1 test text 123 test the cost is $4.0 the cost isis $4.0 is cost $4.0 the is cast $4.0 the is cbst $4.0 [root@localhost gawk]# sed '/123/{n ;d}' data1 #n 命令相当于查询指定的匹配条件,然后跳到下一行执行命令,然后继续往后执行 test text 123 test the cost is $4.0 the cost isis $4.0 is cost $4.0 the is cast $4.0 the is cbst $4.0
合并文本行
[root@localhost gawk]# cat data1 test text 123 test the cost is $4.0 the cost isis $4.0 is cost $4.0 the is cast $4.0 the is cbst $4.0 [root@localhost gawk]# sed '/123/{N ; s/ / /}' data1 test text 123 test the cost is $4.0 the cost isis $4.0 is cost $4.0 the is cast $4.0 the is cbst $4.0 [root@localhost gawk]#
排除命令
#使用 ! 来排除匹配的范围
[root@localhost gawk]# cat data1 test text 123 test the cost is $4.0 the cost isis $4.0 is cost $4.0 the is cast $4.0 the is cbst $4.0 [root@localhost gawk]# sed -n '/123/!p' data1 test text the cost is $4.0 the cost isis $4.0 is cost $4.0 the is cast $4.0 the is cbst $4.0 [root@localhost gawk]#
在脚本中使用sed
反转文本数据
[root@localhost gawk]# cat data1 test text 123 test the cost is $4.0 the cost isis $4.0 is cost $4.0 the is cast $4.0 the is cbst $4.0 [root@localhost gawk]# ./test3.sh data1 the is cbst $4.0 the is cast $4.0 is cost $4.0 the cost isis $4.0 the cost is $4.0 123 test test text [root@localhost gawk]#