1.sed -n '2'p filename
打印文件的第二行。
2.sed -n '1,3'p filename
打印文件的1到3行
3. sed -n '/Neave/'p filename
打印匹配Neave的行(模糊匹配)
4. sed -n '4,/The/'p filename
在第4行查詢模式The
5. sed -n '1,$'p filename
打印整個文件,$表示最後一行。
6. sed -n '/.*ing/'p filename
匹配任意字母,並以ing結尾的單詞(點號不能少)
7 sed -n / -e '/music/'= filename
打印匹配行的行號,-e 會打印文件的內容,同時在匹配行的前面標誌行號。-n只打印出實際的行號。
8.sed -n -e '/music/'p -e '/music/'= filename
打印匹配的行和行號,行號在內容的下面
9.sed '/company/' a\ "Then suddenly it happend" filename
選擇含有company的行,將後面的內容"Then suddenly it happend"加入下一行。注意:它並不改變文件,全部操做在緩衝區,若是要保存輸出,重定向到一個文件。
10. sed '/company/' i\ "Then suddenly it happend" filename
同9,只是在匹配的行前插入
11.sed '/company/' c\ "Then suddenly it happend" filename
用"Then suddenly it happend"替換匹配company的行的內容。
12.sed '1'd ( '1,3'd '$'d '/Neave/'d) filename
刪除第一行(1到3行,最後一行,匹配Neave的行)
13.[ address [,address]] s/ pattern-to-find /replacement-pattern/[g p w n]
s選項通知s e d這是一個替換操做,並查詢pattern-to-find,成功後用replacement-pattern替換它。
替換選項以下:
g 缺省狀況下只替換第一次出現模式,使用g選項替換全局全部出現模式。
p 缺省s e d將全部被替換行寫入標準輸出,加p選項將使- n選項無效。- n選項不打印輸出結果。
w 文件名使用此選項將輸出定向到一個文件。(注意只將匹配替換的行寫入文件,而不是整個內容)
14.sed s'/nurse/"hello "&/' filename
將'hello '增長到'nurse' 的前面。
15. sed '/company/r append.txt' filename
在匹配company的行的下一行開始加入文件append.txt的內容。
16. sed '/company/'q filename
首次匹配company後就退出sed程序
只因此看sed命令,是由於我遇到了這個一個問題。
網上有不少教程,他們發表了不少程序代碼,可是做者爲了解釋方便,都對程序做了行號編碼,就像下面這樣:
代碼::
1:#!/bin/bash
2:#rename file extesions
3:#
4:# rfe old_extensions new_extension
假設這個文件名是tmp,那麼咱們可使用下面的命令來去掉這個行號和冒號(:)
代碼::
sed -e s'/^[0-9]\{1,\}://g' tmp
不過上面的命令的命令有一個缺點,那就是若是這個行號不是數字開頭,而是有空格的話,那就須要修改匹配規則,規則應該修改成匹配第一個非空白字符是數字開始,後面接一個冒號的配對。命令以下:
代碼::
sed -e s'/^[^0-9a-zA-Z]*[0-9]\{1,\}://g' tmp
這令我很興奮,因而想看看sed到底有多厲害,看了之後,明白的是否是sed有多厲害,就像awk同樣,他們只是把正規表達式用到了極致。
以 Redhat6.0 爲測試環境
事實上在solaris下的sed命令要比linux強,但由於沒有測試
環境,我這裏只給在linux下通過測試的用法。
★ 命令行參數簡介
★ 首先假設咱們有這樣一個文本文件 sedtest.txt
★ 輸出指定範圍的行 p
★ 在每一行前面增長一個製表符(^I)
★ 在每一行後面增長--end
★ 顯示指定模式匹配行的行號 [/pattern/]=
★ 在匹配行後面增長文本 [/pattern/]a\ 或者 [address]a\
★ 刪除匹配行 [/pattern/]d 或者 [address1][,address2]d
★ 替換匹配行 [/pattern/]c\ 或者 [address1][,address2]c\
★ 在匹配行前面插入文本 [/pattern/]i\ 或者 [address]i\
★ 替換匹配串(注意再也不是匹配行) [addr1][,addr2]s/old/new/g
★ 限定範圍後的模式匹配
★ 指定替換每一行中匹配的第幾回出現
★ &表明最後匹配
★ 利用sed修改PATH環境變量
★ 測試並提升sed命令運行效率
★ 指定輸出文件 [address1][,address2]w outputfile
★ 指定輸入文件 [address]r inputfile
★ 替換相應字符 [address1][,address2]y/old/new/
★ !號的使用
★ \c正則表達式c 的使用
★ sed命令中正則表達式的複雜性
★ 轉換man手冊成普通文本格式(新)
★ sed的man手冊(用的就是上面的方法)
★ 命令行參數簡介
sed
-e script 指定sed編輯命令
-f scriptfile 指定的文件中是sed編輯命令
-n 寂靜模式,抑制來自sed命令執行過程當中的冗餘輸出信息,好比只
顯示那些被改變的行。
不明白?沒關係,把這些骯髒丟到一邊,跟我往下走,不過下面的介紹裏
不包括正則表達式的解釋,若是你不明白,可能有點麻煩。
★ 首先假設咱們有這樣一個文本文件 sedtest.txt
cat > sedtest.txt
Sed is a stream editor
----------------------
A stream editor is used to perform basic text transformations on an input stream
--------------------------------------------------------------------------------
While in some ways similar to an editor which permits scripted edits (such as ed
)
,
--------------------------------------------------------------------------------
-
-
sed works by making only one pass over the input(s), and is consequently more
-----------------------------------------------------------------------------
efficient. But it is sed's ability to filter text in a pipeline which particular
l
y
--------------------------------------------------------------------------------
-
★ 輸出指定範圍的行 p other types of editors.
sed -e "1,4p" -n sedtest.txt
sed -e "/from/p" -n sedtest.txt
sed -e "1,/from/p" -n sedtest.txt
★ 在每一行前面增長一個製表符(^I)
sed "s/^/^I/g" sedtest.txt
注意^I的輸入方法是ctrl-v ctrl-i
單個^表示行首
★ 在每一行後面增長--end
sed "s/$/--end/g" sedtest.txt
單個$表示行尾
★ 顯示指定模式匹配行的行號 [/pattern/]=
sed -e '/is/=' sedtest.txt
1
Sed is a stream editor
----------------------
3
A stream editor is used to perform basic text transformations on an input stream
--------------------------------------------------------------------------------
While in some ways similar to an editor which permits scripted edits (such as ed
)
,
--------------------------------------------------------------------------------
-
-
7
sed works by making only one pass over the input(s), and is consequently more
-----------------------------------------------------------------------------
9
efficient. But it is sed's ability to filter text in a pipeline which particular
l
y
--------------------------------------------------------------------------------
-
-
意思是分析sedtest.txt,顯示那些包含is串的匹配行的行號,注意 11行中出現了is字符串
這個輸出是面向stdout的,若是不作重定向處理,則不影響原來的sedtest.txt
★ 在匹配行後面增長文本 [/pattern/]a\ 或者 [address]a\
^D
sed -f sedadd.script sedtest.txt
Sed is a stream editor
A stream editor is used to perform basic text transformations on an input stream
While in some ways similar to an editor which permits scripted edits (such as ed
)
,
--------------------------------------------------------------------------------
-
-
sed works by making only one pass over the input(s), and is consequently more
-----------------------------------------------------------------------------
efficient. But it is sed's ability to filter text in a pipeline which particular
l
y
--------------------------------------------------------------------------------
-
-
[scz@ /home/scz/src]> sed -e "a\\
+++++++++
---------------------------------------------
找到包含from字符串的行,在該行的下一行增長+++++++++。
這個輸出是面向stdout的,若是不作重定向處理,則不影響原來的sedtest.txt
不少人想在命令行上直接完成這個操做而不是多一個sedadd.script,不幸的是,這須要用?nbsp;
?nbsp;
續行符\,
[scz@ /home/scz/src]> sed -e "/from/a\\
> +++++++++" sedtest.txt
[scz@ /home/scz/src]> sed -e "a\\
> +++++++++" sedtest.txt
上面這條命令將在全部行後增長一個新行+++++++++
[scz@ /home/scz/src]> sed -e "1 a\\
> +++++++++" sedtest.txt
把下面這兩行copy/paste到一個shell命令行上,效果同樣
+++++++++" sedtest.txt
[address]a\ 只接受一個地址指定
對於a命令,不支持單引號,只能用雙引號,而對於d命令等其餘命令,同時
★ 刪除匹配行 [/pattern/]d 或者 [address1][,address2]d
sed -e '/---------------------------------------------/d' sedtest.txt
Sed is a stream editor
A stream editor is used to perform basic text transformations on an input stream
While in some ways similar to an editor which permits scripted edits (such as ed
)
,
sed works by making only one pass over the input(s), and is consequently more
efficient. But it is sed's ability to filter text in a pipeline which particular
l
y
sed -e '6,10d' sedtest.txt
刪除6-10行的內容,包括6和10
sed -e "2d" sedtest.txt
刪除第2行的內容
sed "1,/^$/d" sedtest.txt
刪除從第一行到第一個空行之間的全部內容
注意這個命令很容易帶來意外的結果,當sedtest.txt中從第一行開始並無空行,則sed刪
?nbsp;
?nbsp;
sed "1,/from/d" sedtest.txt
刪除從第一行到第一個包含from字符串的行之間的全部內容,包括第一個包含
from字符串的行。
★ 替換匹配行 [/pattern/]c\ 或者 [address1][,address2]c\
sed -e "/is/c\\
**********" sedtest.txt
尋找全部包含is字符串的匹配行,替換成**********
**********
----------------------
**********
--------------------------------------------------------------------------------
While in some ways similar to an editor which permits scripted edits (such as ed
)
,
--------------------------------------------------------------------------------
-
-
**********
-----------------------------------------------------------------------------
**********
--------------------------------------------------------------------------------
-
sed -e "1,11c\\
**********" sedtest.txt----------------------
在1-12行內搜索全部from字符串,分別替換成****字符串
★ 限定範圍後的模式匹配
sed "/But/s/is/are/g" sedtest.txt
對那些包含But字符串的行,把is替換成are
sed "/is/s/t/T/" sedtest.txt
對那些包含is字符串的行,把每行第一個出現的t替換成T
sed "/While/,/from/p" sedtest.txt -n
輸出在這兩個模式匹配行之間的全部內容
★ 指定替換每一行中匹配的第幾回出現
sed "s/is/are/5" sedtest.txt
把每行的is字符串的第5次出現替換成are
★ &表明最後匹配
sed "s/^$/(&)/" sedtest.txt
給全部空行增長一對()
sed "s/is/(&)/g" sedtest.txt
給全部is字符串外增長()
sed "s/.*/(&)/" sedtest.txt
給全部行增長一對()
sed "/is/s/.*/(&)/" sedtest.txt
給全部包含is字符串的行增長一對()
★ 利用sed修改PATH環境變量
先查看PATH環境變量
[scz@ /home/scz/src]> echo $PATH
/usr/bin:/usr/bin:/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/X11R6/bin:.
去掉尾部的{ :/usr/X11R6/bin:. }
[scz@ /home/scz/src]> echo $PATH | sed "s/^\(.*\):\/usr[/]X11R6\/bin:[.]$/\1/"
/usr/bin:/usr/bin:/bin:/usr/local/bin:/sbin:/usr/sbin
去掉中間的{ :/bin: }
[scz@ /home/scz/src]> echo $PATH | sed "s/^\(.*\):\/bin:\(.*\)$/\1\2/"
/usr/bin:/usr/bin/usr/local/bin:/sbin:/usr/sbin:/usr/X11R6/bin:.
[/]表示/失去特殊意義
\/一樣表示/失去意義
\1表示子匹配的第一次出現
\2表示子匹配的第二次出現
\(.*\)表示子匹配
去掉尾部的:,而後增長新的路徑
PATH=`echo $PATH | sed 's/\(.*\):$/\1/'`:$HOME/src
注意反引號`和單引號'的區別。
★ 測試並提升sed命令運行效率
time sed -n "1,12p" webkeeper.db > /dev/null
time sed 12q webkeeper.db > /dev/null
能夠看出後者比前者效率高。
[address]q 當碰上指定行時退出sed執行
★ 指定輸出文件 [address1][,address2]w outputfile
sed "1,10w sed.out" sedtest.txt -n
將sedtest.txt中1-10行的內容寫到sed.out文件中。
★ 指定輸入文件 [address]r inputfile
sed "1r sedappend.txt" sedtest.txt
將sedappend.txt中的內容附加到sedtest.txt文件的第一行以後
★ 替換相應字符 [address1][,address2]y/old/new/
sed "y/abcdef/ABCDEF/" sedtest.txt
將sedtest.txt中全部的abcdef小寫字母替換成ABCDEF大寫字母。
★ !號的使用
sed -e '3,7!d' sedtest.txt
刪除3-7行以外的全部行
sed -e '1,/from/!d' sedtest.txt
找到包含from字符串的行,刪除其後的全部行
★ \c正則表達式c 的使用
sed -e "\:from:d" sedtest.txt
等價於 sed -e "/from/d" sedtest.txt
★ sed命令中正則表達式的複雜性
cat > sedtest.txt
^\/[}]{.*}[\(]$\)
^D
如何才能把該行替換成
\(]$\)\/[}]{.*}^[
★ 轉換man手冊成普通文本格式(新)
man sed | col -b > sed.txt
sed -e "s/^H//g" -e "/^$/d" -e "s/^^I/ /g" -e "s/^I/ /g" sed.txt > sedman
txt
刪除全部退格鍵、空行,把行首的製表符替換成8個空格,其他製表符替換成一個空格。
★ sed的man手冊(用的就是上面的方法)
NAME
sed - a Stream EDitor
SYNOPSIS
sed [-n] [-V] [--quiet] [--silent] [--version] [--help]
[-e script] [--expression=script]
[-f script-file] [--file=script-file]
[script-if-no-other-script]
[file...]
DEscriptION
Sed is a stream editor. A stream editor is used to per-
form basic text transformations on an input stream (a file
or input from a pipeline). While in some ways similar to
an editor which permits scripted edits (such as ed), sed
works by making only one pass over the input(s), and is
consequently more efficient. But it is sed's ability to
filter text in a pipeline which particularly distinguishes
it from other types of editors.
OPTIONS
Sed may be invoked with the following command-line
options:
-V
--version
Print out the version of sed that is being run and
a copyright notice, then exit.
-h
--help Print a usage message briefly summarizing these
command-line options and the bug-reporting address,
then exit.
-n
--quiet
--silent
By default, sed will print out the pattern space at
the end of each cycle through the script. These
options disable this automatic printing, and sed
will only produce output when explicitly told to
via the p command.
-e script
--expression=script
Add the commands in script to the set of commands
to be run while processing the input.
-f script-file
--file=script-file
Add the commands contained in the file script-file
to the set of commands to be run while processing
the input.
If no -e,-f,--expression, or --file options are given on
the command-line, then the first non-option argument on
the command line is taken to be the script to be executed.
If any command-line parameters remain after processing the
above, these parameters are interpreted as the names of
input files to be processed. A file name of - refers to
the standard input stream. The standard input will pro-
cessed if no file names are specified.
Command Synopsis
This is just a brief synopsis of sed commands to serve as
a reminder to those who already know sed; other documenta-
tion (such as the texinfo document) must be consulted for
fuller descriptions.
Zero-address ``commands''
: label
Label for b and t commands.
#comment
The comment extends until the next newline (or the
end of a -e script fragment).
} The closing bracket of a { } block.
Zero- or One- address commands
= Print the current line number.
a \
text Append text, which has each embedded newline pre-
ceeded by a backslash.
i \
text Insert text, which has each embedded newline pre-
ceeded by a backslash.
q Immediately quit the sed script without processing
any more input, except that if auto-print is not
diabled the current pattern space will be printed.
r filename
Append text read from filename.
Commands which accept address ranges
{ Begin a block of commands (end with a }).
b label
Branch to label; if label is omitted, branch to end
of script.
t label
If a s/// has done a successful substitution since
the last input line was read and since the last t
command, then branch to label; if label is omitted,
branch to end of script.
c \
text Replace the selected lines with text, which has
each embedded newline preceeded by a backslash.
d Delete pattern space. Start next cycle.
D Delete up to the first embedded newline in the pat-
tern space. Start next cycle, but skip reading
from the input if there is still data in the pat-
tern space.
h H Copy/append pattern space to hold space.
g G Copy/append hold space to pattern space.
x Exchange the contents of the hold and pattern
spaces.
l List out the current line in a ``visually unambigu-
ous'' form.
n N Read/append the next line of input into the pattern
space.
p Print the current pattern space.
P Print up to the first embedded newline of the cur-
rent pattern space.
s/regexp/replacement/
Attempt to match regexp against the pattern space.
If successful, replace that portion matched with
replacement. The replacement may contain the spe-
cial character & to refer to that portion of the
pattern space which matched, and the special
escapes \1 through \9 to refer to the corresponding
matching sub-expressions in the regexp.
w filename Write the current pattern space to file-
name.
y/source/dest/
Transliterate the characters in the pattern space
which appear in source to the corresponding charac-
ter in dest.
Addresses
Sed commands can be given with no addresses, in which case
the command will be executed for all input lines; with one
address, in which case the command will only be executed
for input lines which match that address; or with two
addresses, in which case the command will be executed for
all input lines which match the inclusive range of lines
starting from the first address and continuing to the sec-
ond address. Three things to note about address ranges:
the syntax is addr1,addr2 (i.e., the addresses are sepa-
rated by a comma); the line which addr1 matched will
always be accepted, even if addr2 selects an earlier line;
and if addr2 is a regexp, it will not be tested against
the line that addr1 matched.
After the address (or address-range), and before the com-
mand, a ! may be inserted, which specifies that the com-
mand shall only be executed if the address (or address-
range) does not match.
The following address types are supported:
number Match only the specified line number.
first~step
Match every step'th line starting with line first.
For example, ``sed -n 1~2p'' will print all the
odd-numbered lines in the input stream, and the
address 2~5 will match every fifth line, starting
with the second. (This is a GNU extension.)
$ Match the last line.
/regexp/
Match lines matching the regular expression regexp.
\cregexpc
Match lines matching the regular expression regexp.
The c may be any character.
Regular expressions
POSIX.2 BREs should be supported, but they aren't com-
pletely yet. The \n sequence in a regular expression
matches the newline character. There are also some GNU
extensions. [XXX FIXME: more needs to be said. At the
very least, a reference to another document which
describes what is supported should be given.]
Miscellaneous notes
This version of sed supports a \<newline> sequence in all
regular expressions, the replacement part of a substitute
(s) command, and in the source and dest parts of a
transliterate (y) command. The \ is stripped, and the
newline is kept.
SEE ALSO
awk(1), ed(1), expr(1), emacs(1), perl(1), tr(1), vi(1),
regex(5) [well, one ought to be written... XXX], sed.info,
any of various books on sed, the sed FAQ
([url]http://www.wollery.demon.co.uk/sedtut10.txt[/url],
[url]http://www.ptug.org/sed/sedfaq.htm[/url]).
BUGS
E-mail bug reports to bug-gnu-utils@gnu.org. Be sure to
include the word ``sed'' somewhere in the ``Subject:''
field.
Sed學習筆記
--------------------------------------------------------------------------------
Table of Contents
1. Sed簡介
2. 定址
3. Sed命令
4. 選項
5. 元字符集
6. 實例
7. 腳本
1. Sed簡介
sed是一種在線編輯器,它一次處理一行內容。處理時,把當前處理的行存儲在臨時緩衝區中,稱爲「模式空間」(pattern space),接着用sed命令處理緩衝區中的內容,處理完成後,把緩衝區的內容送往屏幕。接着處理下一行,這樣不斷重複,直到文件末尾。文件內容並無改變,除非你使用重定向存儲輸出。Sed主要用來自動編輯一個或多個文件;簡化對文件的反覆操做;編寫轉換程序等。如下介紹的是Gnu版本的Sed 3.02。
2. 定址
能夠經過定址來定位你所但願編輯的行,該地址用數字構成,用逗號分隔的兩個行數表示以這兩行爲起止的行的範圍(包括行數表示的那兩行)。如1,3表示1,2,3行,美圓符號($)表示最後一行。範圍能夠經過數據,正則表達式或者兩者結合的方式肯定 。
3. Sed命令
調用sed命令有兩種形式:
sed [options] 'command' file(s)
sed [options] -f scriptfile file(s)
a\
在當前行後面加入一行文本。
b lable
分支到腳本中帶有標記的地方,若是分支不存在則分支到腳本的末尾。
c\
用新的文本改變本行的文本。
d
從模板塊(Pattern space)位置刪除行。
D
刪除模板塊的第一行。
i\
在當前行上面插入文本。
h
拷貝模板塊的內容到內存中的緩衝區。
H
追加模板塊的內容到內存中的緩衝區
g
得到內存緩衝區的內容,並替代當前模板塊中的文本。
G
得到內存緩衝區的內容,並追加到當前模板塊文本的後面。
l
列表不能打印字符的清單。
n
讀取下一個輸入行,用下一個命令處理新的行而不是用第一個命令。
N
追加下一個輸入行到模板塊後面並在兩者間嵌入一個新行,改變當前行號碼。
p
打印模板塊的行。
P(大寫)
打印模板塊的第一行。
q
退出Sed。
r file
從file中讀行。
t label
if分支,從最後一行開始,條件一旦知足或者T,t命令,將致使分支到帶有標號的命令處,或者到腳本的末尾。
T label
錯誤分支,從最後一行開始,一旦發生錯誤或者T,t命令,將致使分支到帶有標號的命令處,或者到腳本的末尾。
w file
寫並追加模板塊到file末尾。
W file
寫並追加模板塊的第一行到file末尾。
!
表示後面的命令對全部沒有被選定的行發生做用。
s/re/string
用string替換正則表達式re。
=
打印當前行號碼。
#
把註釋擴展到下一個換行符之前。
如下的是替換標記
g表示行內全面替換。
p表示打印行。
w表示把行寫入一個文件。
x表示互換模板塊中的文本和緩衝區中的文本。
y表示把一個字符翻譯爲另外的字符(可是不用於正則表達式)
4. 選項
-e command, --expression=command
容許多臺編輯。
-h, --help
打印幫助,並顯示bug列表的地址。
-n, --quiet, --silent
取消默認輸出。
-f, --filer=script-file
引導sed腳本文件名。
-V, --version
打印版本和版權信息。
5. 元字符集
^ 錨定行的開始 如:/^sed/匹配全部以sed開頭的行。
$ 錨定行的結束 如:/sed$/匹配全部以sed結尾的行。
. 匹配一個非換行符的字符 如:/s.d/匹配s後接一個任意字符,而後是d。
* 匹配零或多個字符 如:/*sed/匹配全部模板是一個或多個空格後緊跟sed的行。
[] 匹配一個指定範圍內的字符,如/[Ss]ed/匹配sed和Sed。
[^] 匹配一個不在指定範圍內的字符,如:/[^A-RT-Z]ed/匹配不包含A-R和T-Z的一個字母開頭,緊跟ed的行。
\(..\) 保存匹配的字符,如s/\(love\)able/\1rs,loveable被替換成lovers。
& 保存搜索字符用來替換其餘字符,如s/love/**&**/,love這成**love**。
\< 錨定單詞的開始,如:/\<love/匹配包含以love開頭的單詞的行。
\> 錨定單詞的結束,如/love\>/匹配包含以love結尾的單詞的行。
x\{m\}重複字符x,m次,如:/0\{5\}/匹配包含5個o的行。
x\{m,\} 重複字符x,至少m次,如:/o\{5,\}/匹配至少有5個o的行。
x\{m,n\}重複字符x,至少m次,很少於n次,如:/o\{5,10\}/匹配5--10個o的行。
6. 實例
刪除:d命令
$ sed '2d' example-----刪除example文件的第二行。
$ sed '2,$d' example-----刪除example文件的第二行到末尾全部行。
$ sed '$d' example-----刪除example文件的最後一行。
$ sed '/test/'d example-----刪除example文件全部包含test的行。
替換:s命令
$ sed 's/test/mytest/g' example-----在整行範圍內把test替換爲mytest。若是沒有g標記,則只有每行第一個匹配的test被替換成mytest。
$ sed -n 's/^test/mytest/p' example-----(-n)選項和p標誌一塊兒使用表示只打印那些發生替換的行。也就是說,若是某一行開頭的test被替換成mytest,就打印它。
$ sed 's/^192.168.0.1/&localhost/' example-----&符號表示替換換字符串中被找到的部份。全部以192.168.0.1開頭的行都會被替換成它自已加localhost,變成192.168.0.1localhost。
$ sed -n 's/\(love\)able/\1rs/p' example-----love被標記爲1,全部loveable會被替換成lovers,並且替換的行會被打印出來。
$ sed 's#10#100#g' example-----不論什麼字符,緊跟着s命令的都被認爲是新的分隔符,因此,「#」在這裏是分隔符,代替了默認的「/」分隔符。表示把全部10替換成100。
選定行的範圍:逗號
$ sed -n '/test/,/check/p' example-----全部在模板test和check所肯定的範圍內的行都被打印。
$ sed -n '5,/^test/p' example-----打印從第五行開始到第一個包含以test開始的行之間的全部行。
$ sed '/test/,/check/s/$/sed test/' example-----對於模板test和west之間的行,每行的末尾用字符串sed test替換。
多點編輯:e命令
$ sed -e '1,5d' -e 's/test/check/' example-----(-e)選項容許在同一行裏執行多條命令。如例子所示,第一條命令刪除1至5行,第二條命令用check替換test。命令的執行順序對結果有影響。若是兩個命令都是替換命令,那麼第一個替換命令將影響第二個替換命令的結果。
$ sed --expression='s/test/check/' --expression='/love/d' example-----一個比-e更好的命令是--expression。它能給sed表達式賦值。
從文件讀入:r命令
$ sed '/test/r file' example-----file裏的內容被讀進來,顯示在與test匹配的行後面,若是匹配多行,則file的內容將顯示在全部匹配行的下面。
寫入文件:w命令
$ sed -n '/test/w file' example-----在example中全部包含test的行都被寫入file裏。
追加命令:a命令
$ sed '/^test/a\\--->this is a example' example<-----'this is a example'被追加到以test開頭的行後面,sed要求命令a後面有一個反斜槓。
插入:i命令
$ sed '/test/i\\
new line
-------------------------' example
若是test被匹配,則把反斜槓後面的文本插入到匹配行的前面。
下一個:n命令
$ sed '/test/{ n; s/aa/bb/; }' example-----若是test被匹配,則移動到匹配行的下一行,替換這一行的aa,變爲bb,並打印該行,而後繼續。
變形:y命令
$ sed '1,10y/abcde/ABCDE/' example-----把1--10行內全部abcde轉變爲大寫,注意,正則表達式元字符不能使用這個命令。
退出:q命令
$ sed '10q' example-----打印完第10行後,退出sed。
保持和獲取:h命令和G命令
$ sed -e '/test/h' -e '$G example-----在sed處理文件的時候,每一行都被保存在一個叫模式空間的臨時緩衝區中,除非行被刪除或者輸出被取消,不然全部被處理的行都將打印在屏幕上。接着模式空間被清空,並存入新的一行等待處理。在這個例子裏,匹配test的行被找到後,將存入模式空間,h命令將其複製並存入一個稱爲保持緩存區的特殊緩衝區內。第二條語句的意思是,當到達最後一行後,G命令取出保持緩衝區的行,而後把它放回模式空間中,且追加到如今已經存在於模式空間中的行的末尾。在這個例子中就是追加到最後一行。簡單來講,任何包含test的行都被複制並追加到該文件的末尾。
保持和互換:h命令和x命令
$ sed -e '/test/h' -e '/check/x' example -----互換模式空間和保持緩衝區的內容。也就是把包含test與check的行互換。
7. 腳本
Sed腳本是一個sed的命令清單,啓動Sed時以-f選項引導腳本文件名。Sed對於腳本中輸入的命令很是挑剔,在命令的末尾不能有任何空白或文本,若是在一行中有多個命令,要用分號分隔。以#開頭的行爲註釋行,且不能跨行。php