sed (GNU)

這篇也不錯: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'


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章