sed 使用
2017-11-30 16:43
148 查看
简介
sed是非交互式的编辑器。它不会修改文件,除非使用shell重定向来保存结果。默认情况下,所有的输出行都被打印到屏幕上。
sed编辑器逐行处理文件(或输入),并将结果发送到屏幕。具体过程如下:首先sed把当前正在处理的行保存在一个临时缓存区中(也称为模式空间),然后处理临时缓冲区中的行,完成后把该行发送到屏幕上。sed每处理完一行就将其从临时缓冲区删除,然后将下一行读入,进行处理和显示。处理完输入文件的最后一行后,sed便结束运行。sed把每一行都存在临时缓冲区中,对这个副本进行编辑,所以不会修改原文件。
使用方法
sed [options] 'command' file(s)
sed [options] -f scriptfile file(s)
命令的选项
-n :只打印模式匹配的行
-e :直接在命令行模式上进行sed动作编辑,此为默认选项
-f :将sed的动作写在一个文件内,用–f filename 执行filename内的sed动作
-r :支持扩展表达式
-i :直接修改文件内容
查询文本
1)使用行号,可以是一个简单数字,或是一个行号范围
2)使用正则表达式、扩展正则表达式(必须结合-r选项)
编辑命令
帮助信息
$ sed -h
sed: invalid option -- 'h'
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)
-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/>.
参考资料
http://www.gnu.org/software/sed/manual/sed.html
https://www.cnblogs.com/ctaixw/p/5860221.html
sed是非交互式的编辑器。它不会修改文件,除非使用shell重定向来保存结果。默认情况下,所有的输出行都被打印到屏幕上。
sed编辑器逐行处理文件(或输入),并将结果发送到屏幕。具体过程如下:首先sed把当前正在处理的行保存在一个临时缓存区中(也称为模式空间),然后处理临时缓冲区中的行,完成后把该行发送到屏幕上。sed每处理完一行就将其从临时缓冲区删除,然后将下一行读入,进行处理和显示。处理完输入文件的最后一行后,sed便结束运行。sed把每一行都存在临时缓冲区中,对这个副本进行编辑,所以不会修改原文件。
使用方法
sed [options] 'command' file(s)
sed [options] -f scriptfile file(s)
命令的选项
-n :只打印模式匹配的行
-e :直接在命令行模式上进行sed动作编辑,此为默认选项
-f :将sed的动作写在一个文件内,用–f filename 执行filename内的sed动作
-r :支持扩展表达式
-i :直接修改文件内容
查询文本
1)使用行号,可以是一个简单数字,或是一个行号范围
x | x为行号 |
x,y | 表示行号从x到y |
/pattern | 查询包含模式的行 |
/pattern /pattern | 查询包含两个模式的行 |
pattern/,x | 在给定行号上查询包含模式的行 |
x,/pattern/ | 通过行号和模式查询匹配的行 |
x,y! | 查询不包含指定行号x和y的行 |
^ | 锚点行首的符合条件的内容,用法格式"^pattern" |
$ | 锚点行首的符合条件的内容,用法格式"pattern$" |
^$ | 空白行 |
. | 匹配任意单个字符 |
* | 匹配紧挨在前面的字符任意次(0,1,多次) |
.* | 匹配任意长度的任意字符 |
\? | 匹配紧挨在前面的字符0次或1次 |
\{m,n\} | 匹配其前面的字符至少m次,至多n次 |
\{m,\} | 匹配其前面的字符至少m次 |
\{m\} | 精确匹配前面的m次\{0,n\}:0到n次 |
\< | 锚点词首----相当于 \b,用法格式:\<pattern |
\> | 锚点词尾,用法格式:\>pattern |
\<pattern\> | 单词锚点 |
分组,用法格式:pattern,引用\1,\2 | |
[] | 匹配指定范围内的任意单个字符 |
[^] | 匹配指定范围外的任意单个字符 |
[:digit:] | 所有数字, 相当于0-9, [0-9]---> [[:digit:]] |
[:lower:] | 所有的小写字母 |
[:upper:] | 所有的大写字母 |
[:alpha:] | 所有的字母 |
[:alnum:] | 相当于0-9a-zA-Z |
[:space:] | 空白字符 |
[:punct:] | 所有标点符号 |
p | 打印匹配行(和-n选项一起合用) |
= | 显示文件行号 |
a\ | 在定位行号后附加新文本信息 |
i\ | 在定位行号后插入新文本信息 |
d | 删除定位行 |
c\ | 用新文本替换定位文本 |
w filename | 写文本到一个文件,类似输出重定向 > |
r filename | 从另一个文件中读文本,类似输入重定向 < |
s | 使用替换模式替换相应模式 |
q | 第一个模式匹配完成后退出或立即退出 |
l | 显示与八进制ACSII代码等价的控制符 |
{} | 在定位行执行的命令组,用分号隔开 |
n | 从另一个文件中读文本下一行,并从下一条命令而不是第一条命令开始对其的处理 |
N | 在数据流中添加下一行以创建用于处理的多行组 |
g | 将模式2粘贴到/pattern n/ |
y | 传送字符,替换单个字符 |
$ sed -h
sed: invalid option -- 'h'
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)
-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/>.
参考资料
http://www.gnu.org/software/sed/manual/sed.html
https://www.cnblogs.com/ctaixw/p/5860221.html
相关文章推荐
- Linux使用sed对文档进行增、删、改、查
- sed 中使用shell变量
- sed使用方法
- linux下sed的使用
- sed的使用1-替换
- sed 命令 使用 总结
- grep、sed、awk命令使用参数说明
- 使用sed替换一行内多个括号内的值
- Sed使用简介
- sed使用详解
- 每天学点Linux之sed命令的使用
- sed 的使用(一) -- s参数/替换
- 使用sed和cut将进程的pid过滤出来
- 第7章 sed、gawk介绍与正则表达式-----------(sed编辑器简单使用)
- sed命令常见使用杂记
- shell中sed的使用
- sed使用笔记
- SED使用
- 浅谈linux中sed命令和awk命令的使用
- Linux 之Sed使用