sed命令简介

Sed 简介
sed 是一种新型的,非交互式的编辑器。它能执行与编辑器 vi 和 ex 相同的编辑任务。sed 编辑器没有提供交互式使用方式,使用者只能在命令行输入编辑命令、指定文件名,然后在屏幕上查看输出。

sed 工作过程
sed 编辑器逐行处理文件(或输入),并将输出结果发送到屏幕。 sed 的命令是从行编辑器ed发展而来,但是它只能非交互式执行。 sed 把当前正在处理的行保存在一个临时缓存区中,这个缓存区称为模式空间或临时缓冲。sed 处理完模式空间中的行后(即在该行上执行 sed 命令,命令由模式和动作组成,模式用于匹配是否在该行上执行动作,动作主要由单字符的命令组成,可以有多个命令,每个命令都由模式和动作,读出的一行执行完所有命令后,该行处理结束,sed选项用于影响所有的命令),就把改行发送到屏幕上(除非之前有命令删除这一行或取消打印操作)。 sed 每处理完输入文件的最后一行后, sed 便结束运行。 sed 把每一行都存在临时缓存区中,对这个副本进行编辑,所以不会修改或破坏源文件。


Sed 命令格式
sed OPTIONS... [SCRIPT] [INPUTFILE...]

sed   options overview
sed   may be invoked with the following command-line options:
--version
Print out the version of   sed   that is being run and a copyright notice, then exit.
--help
Print a usage message briefly summarizing these command-line options and the bug-reporting address, then exit.
-n --quiet --silent
By default,   sed   prints out the pattern space at the end of each cycle through the script (see   How   sed   works ). These options disable this automatic printing, and   sed   only produces output when explicitly told to via the   p   command.
仅仅显示的有p命令的行才显示
-e  script
Without -e or -f options, sed uses the first non-option parameter as the script, and the following non-option parameters as input files. If -e or -f options are used to specify a script, all non-option parameters are taken as input files. Options -e and -f can be combined, and can appear multiple times 

sed script overview

sed   commands follow this syntax:
[addr] X [options]
X   is a single-letter   sed   command.   [addr]   is an optional line address. If   [addr]   is specified, the command   X   will be executed only on the matched lines.   [addr]   can be a single line number, a regular expression, or a range of lines (see   sed addresses ). Additional   [options]   are used for some   sed   commands.

Commands within a   script   or   script-file   can be separated by semicolons ( ; ) or newlines (ASCII 10). Multiple scripts can be specified with   -e   or   -f   options.

The following examples are all equivalent. They perform two   sed   operations: deleting any lines matching the regular expression   /^foo/ , and replacing all occurrences of the string ‘ hello ’ with ‘ world ’:
sed '/^foo/d ; s/hello/world/' input.txt > output.txt

sed -e '/^foo/d' -e 's/hello/world/' input.txt > output.txt

echo '/^foo/d' > script.sed
echo 's/hello/world/' >> script.sed
sed -f script.sed input.txt > output.txt

echo 's/hello/world/' > script2.sed
sed -e '/^foo/d' -f script2.sed input.txt > output.txt
Commands   a ,   c ,   i , due to their syntax, cannot be followed by semicolons working as command separators and thus should be terminated with newlines or be placed at the end of a   script   or   script-file .  

sed commands summary


注意在使用sed时,一般都是通过shell去执行的,此时要特别注意shell对特殊字符的解释和拓展,解释和拓展完之后才传递给sed,如果不想特殊字符被shell处理,则最后用单引号,另外对于命令中的特殊字符,可以放入文件中,防止shell解释带来的影响,不同的shell对特殊字符的解释不同,使用时应注意使用的是哪种shell。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值