sed 是一种流编辑器,它是文本处理中非常中的工具,能够完美的配合正则表达式使用,功能不同凡响。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有 改变,除非你使用重定向存储输出。Sed主要用来自动编辑一个或多个文件;简化对文件的反复操作;编写转换程序等。
说明:功能强大的流式文本编辑器 用法: sed [选项]... {脚本(如果没有其他脚本)} [输入文件]...
-n, --quiet, --silent 取消自动打印模式空间 -e 脚本, --expression=脚本 添加“脚本”到程序的运行列表 -f 脚本文件, --file=脚本文件 添加“脚本文件”到程序的运行列表 --follow-symlinks 直接修改文件时跟随软链接 -i[SUFFIX], --in-place[=SUFFIX] edit files in place (makes backup if SUFFIX supplied) -l N, --line-length=N 指定“l”命令的换行期望长度 --posix 关闭所有 GNU 扩展 -E, -r, --regexp-extended use extended regular expressions in the script (for portability use POSIX -E). -s, --separate consider files as separate rather than as a single, continuous long stream. --sandbox operate in sandbox mode. -u, --unbuffered 从输入文件读取最少的数据,更频繁的刷新输出 -z, --null-data separate lines by NUL characters --help 打印帮助并退出 --version 输出版本信息并退出
如果没有 -e, --expression, -f 或 --file 选项,那么第一个非选项参数被视为 sed脚本。其他非选项参数被视为输入文件,如果没有输入文件,那么程序将从标准 输入读取数据。 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: <bug-sed@gnu.org>.
sed示例
sed工作方式
sed 通过对输入数据执行任意数量用户指定的编辑操作(命令)。sed 是基于行的,因此按顺序对每一行执行命令。然后将其结果写入标准输出。
$ cat /etc/rc.local # 注释1 #!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing.
service php-fpm start service mysql start service nginx start
exit 0 $ sed -e '/^#/d' /etc/rc.local # 注释2
service php-fpm start service mysql start /usr/local/nginx/sbin/nginx
service php-fpm start service mysql start /usr/local/nginx/sbin/nginx
exit 0 $ sed -n -e '/^#/p' /etc/rc.local # 显示缓冲区所有注释行 #!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. #/etc/init.d/nginx start $ sed -e '/^[^#]/d' /etc/rc.local # 删除不是以 # 号开头的行(空行没有被删除) #!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing.
$ sed -e '1,10s/power/aurora/g' /tmp/demo.c # 替换1到10行中的所有power # include <stdio.h> # include <math.h>
long int aurora(int, int); # 此处被替换
int main() { int base, n; scanf("%d, %d\n", &base, &n); printf("The aurora is: %d\n", aurora(base, n)); # 此处被替换 }
long int power(int base, int n) # 此处没被替换 { return base^n; } $ sed -e '/main[[:space:]]*(/,/^)/s/power/aurora/g' /tmp/demo.c # 替换 main 函数中所有 power(没起作用,后面的 power都被替换了) # include <stdio.h> # include <math.h>
long int power(int, int);
int main() { int base, n; scanf("%d, %d\n", &base, &n); printf("The aurora is: %d\n", aurora(base, n)); # 此处被替换 }
long int aurora(int base, int n) # 此处被替换 { return base^n; }