sed 使用

简介
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的行

2)使用正则表达式、扩展正则表达式(必须结合-r选项)

^

锚点行首的符合条件的内容,用法格式"^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/>.


参考资料

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值