sed (GNU)

来源:互联网 发布:国培计划网络研修日志 编辑:程序博客网 时间:2024/06/05 18:51

这篇也不错:sed的一篇强例子集锦的翻译(转)

operations sed does

  1. reads a line from the input stream
  2. removes any trailing newline
  3. puts it in the pattern space
  4. executes the commands
  5. outputs the pattern space, adding trailing newline

unless special commands, like D, are used, the pattern space is deleted between two cycles. Thehold space (buffer) is on the contrary.


options

-n no pattern space is output unless explicitly requested via the p command

-i input file is changed in place

-r use extended regular expressions


addresses

数字 指定的行数的那行

开始~步长 从开始行起,每步长行

$ 最后一行

/表达式/ 按内容找到匹配行;无表达式时使用之前的。如果后面再加I,则不区分大小写;后面再加M则^和$分别是新行后、前的空串。`和'总是内容的起始和结束。

\符号表达式符号 以符号而非斜线为匹配内容的分界,从而方便表达式中使用斜线

逗号可以表示两个地址之间的行(含起始行,不含结尾行)。如果结尾地址是进行匹配,则匹配从起始处下一行开始。如果起始地址是0,则匹配从第一行开始。结尾地址可以带加号表示相对行数。结尾地址可以带~表示直到行数是结尾地址的倍数。

在地址后面加!表示取反。

地址可以用搜索形式,之后用替换或添加命令,如0,/^env/s/...


commands

you can use space between addresses and commands.

use semicolon between commands

q 退出

{ 命令集 }

s/表达式/替换内容/标志 搜索。可以用其它符号替代斜线。替换内容可以包含\数字,数字为1至9,对应匹配内容中指定次数的\(和\)之间的内容。替换内容中可以包含&(不转义)对应所有匹配内容。替换内容中还可以包含\L或\U将\E之前的内容转换为小或大写,\l或\u将下一字符转换为小或大写。标志可以是:g,不止匹配一次;数字,找到指定次数的匹配;p,显示完成后的pattern space;e,执行完成后的pattern space;I或i,忽略大小写;M或m,使^和$表示新行后、前的空串。

y/前集/后集 可以用其它符号替代斜线。将pattern space中前集中每个字符对应转换为后集中每个字符。

a字符串 append text as a new line

a\

字符串 queue the lines of text after this command to the output at the end of the current cycle.

i字符串 prepend text as a new line

i\

字符串 immediately output the lines of text after this command.

c\

字符串 delete the lines according to the addresses, and output the lines of text after this command.

= 显示当前行号。

l 数字 显示不可见字符;长行(根据指定的长度)折行;显示行尾。

r或w 文件名 ……

d 清空pattern space并立刻开始下一循环。

D 清空pattern space中的第一行,如果还有内容则继续处理,否则进入下一循环。

n 显示pattern space并用输入替换pattern space

N Add a new line to the pattern space, then append the next line of input to the pattern space.

P 显示pattern space中的第一行

p 显示pattern space

h 用pattern space内容替换hold space

H Append a newline to the hold space, then append the pattern space to the hold space.

g 用hold space内容替换pattern space

G Append a newline to the pattern space, then append the hold space to the pattern space.

x Exchange the two spaces.

: 标签

b 标签 转到标签

t 标签 只在成功替换后,转到标签

e 命令 ……

L 数字 ……

Q ……

R ……

T ……

v ……

W ……

z 清空pattern space


examples

to reverse all lines, similar to tac

#!/usr/bin/sed -nf # reverse all lines of input, i.e. first line became last, ... # from the second line, the buffer (which contains all previous lines) # is *appended* to current line, so, the order will be reversed 1! G # on the last line we're done -- print everything $ p # store everything on the buffer again h

to print first 10 lines

10q

to print last 10 lines

#!/usr/bin/sed -nf     1!{H;g}     1,10!s/[^\n]*\n//     $p     h

to replace the last match, we have to take advantage of regular expressions

sed -n '1!{H;g};$s/\(.*target[^\n]*\)\(.*\)/\1\nnew\2/;$p;h'

从第二行开始,将读入内容添加到缓存(H,g,h)。到最后一行时找到最后一个目标,保留其前、其后到行尾,添加新内容和换行,再补上后面所有行。到最后显示出来。

如果target、new是变量,用'$target'和'$new'替换上文的target和new。


去掉前、后一行

#或没有井号ifndef这行会去掉

TT任意字符

#或没有井号endif这行也会去掉

sed -n '1!{H;g};$s/\(.*\)#*ifndef\n\(TT[^\n]*\)\n#*endif\n\(.*\)/\1\2\3/;$p;h'


原创粉丝点击