目录
sed简介
Stream EDitor, 行编辑器
sed是一种流编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时
缓冲区中,称为“模式空间”( pattern space),接着用sed命令处理缓冲区中的
内容,处理完成后,把缓冲区的内容送往屏幕。然后读入下行,执行下一个循环。
如果没有使诸如‘D’ 的特殊命令,那会在两个循环之间清空模式空间,但不会清
空保留空间。这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重
定向存储输出。
功能:主要用来自动编辑一个或多个文件,简化对文件的反复操作,编写转换程序等
参考: http://www.gnu.org/software/sed/manual/sed.html
sed工具
用法说明:
[root@Centos7 ~]#sed --help
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...
-n, --quiet, --silent
suppress automatic printing of pattern space
-e script, --expression=script
add the script to the commands to be executed
-f script-file, --file=script-file
add the contents of script-file to the commands to be executed
--follow-symlinks
follow symlinks when processing in place
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if SUFFIX supplied)
-c, --copy
use copy instead of rename when shuffling files in -i mode
-b, --binary
does nothing; for compatibility with WIN32/CYGWIN/MSDOS/EMX (
open files in binary mode (CR+LFs are not treated specially))
-l N, --line-length=N
specify the desired line-wrap length for the `l' command
--posix
disable all GNU extensions.
-r, --regexp-extended
use extended regular expressions in the script.
-s, --separate
consider files as separate rather than as a single continuous
long stream.
-u, --unbuffered
load minimal amounts of data from the input files and flush
the output buffers more often
-z, --null-data
separate lines by NUL characters
--help
display this help and exit
--version
output version information and exit
If no -e, --expression, -f, or --file option is given, then the first
non-option argument is taken as the sed script to interpret. All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.
GNU sed home page: <http://www.gnu.org/software/sed/>.
General help using GNU software: <http://www.gnu.org/gethelp/>.
E-mail bug reports to: <[email protected]>.
Be sure to include the word ``sed'' somewhere in the ``Subject:'' field.
常用选项:
- -n:不输出模式空间内容到屏幕,即不自动打印
- -e: 多点编辑
- -f: /PATH/SCRIPT_FILE: 从指定文件中读取编辑脚本
- -r: 支持使用扩展正则表达式
- -i.bak: 备份文件并原处编辑(会直接修改原文件,一般加.bak等后缀建立备份后操作)
script: '地址命令'
地址定界:
(1) 不给地址: 对全文进行处理
(2) 单地址: #: 指定的行, $: 最后一行
[root@Centos7 ~]#sed -n '2p' /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
[root@Centos7 ~]#sed -n '$p' /etc/passwd
/pattern/: 被此处模式所能够匹配到的每一行
(3) 地址范围:
#,#
#,+#
/pat1/,/pat2/
#,/pat1/
[root@Centos7 ~]#sed -n '1,3p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@Centos7 ~]#sed -n '1,+3p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
[root@Centos7 ~]#sed -n '1,/^d/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
(4) ~:步进
1~2 奇数行
2~2 偶数行
[root@Centos7 ~]#cat -n /etc/passwd | sed -n '1~2p'
1 root:x:0:0:root:/root:/bin/bash
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
[root@Centos7 ~]#cat -n /etc/passwd | sed -n '2~2p'
2 bin:x:1:1:bin:/bin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
6 sync:x:5:0:sync:/sbin:/bin/sync
8 halt:x:7:0:halt:/sbin:/sbin/halt
10 operator:x:11:0:operator:/root:/sbin/nologin
编辑命令
- d: 删除模式空间匹配的行,并立即启用下一轮循环
- p: 打印当前模式空间内容,追加到默认输出之后
- a [\]text:在指定行后面追加文本
- 支持使用\n实现多行追加
- i [\]text:在行前面插入文本
- c [\]text:替换行为单行或多行文本
- w /path/somefile: 保存模式匹配的行至指定文件
- r /path/somefile:读取指定文件的文本至模式空间中匹配到的行后
- =: 为模式空间中的行打印行号
- !:模式空间中匹配行取反处理
s///:查找替换,支持使用其它分隔符, s@@@, s###
替换标记:
- g: 行内全局替换
- p: 显示替换成功的行
w /PATH/TO/SOMEFILE:将替换成功的行保存至文
[root@Centos7 app]#vim f1
root b c root d e root
1 2 3 4 5
aa bb cc
[root@Centos7 app]#sed 's#root#ROOT#gp' f1
ROOT b c ROOT d e ROOT
ROOT b c ROOT d e ROOT
1 2 3 4 5
aa bb cc
高级编辑命令
P:打印模式空间开端至\n内容,并追加到默认输出之前
h: 把模式空间中的内容覆盖至保持空间中
H:把模式空间中的内容追加至保持空间中
g: 从保持空间取出数据覆盖至模式空间
G:从保持空间取出内容追加至模式空间
x: 把模式空间中的内容与保持空间中的内容进行互换
n: 读取匹配到的行的下一行覆盖至模式空间
N:读取匹配到的行的下一行追加至模式空间
d: 删除模式空间中的行
D:如果模式空间包含换行符,则删除直到第一个换行符的模式空间中的文本,并不会读取新的输入行,而使用合成的模式空间 重新启动循环。如果模式空间不包含换行符,则会像发出d命令那样启动正常的新循环