1. 程式人生 > >sed詳解---用法及解釋

sed詳解---用法及解釋

nts parameter 同一行 開頭 types consul win 命令 one

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 \ 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
(http://www.wollery.demon.co.uk/sedtut10.txt,
http://www.ptug.org/sed/sedfaq.htm).
BUGS
E-mail bug reports to [email protected]. Be sure to
include the word ``sed‘‘ somewhere in the ``Subject:‘‘
field.
Sed學習筆記
作者:Jims of
肥 肥世家

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結尾的單詞的行。
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被追加到以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對於腳本中輸入的命令非常挑剔,在命令的末尾不能有任何空白或文本,如 果在一行中有多個命令,要用分號分隔。以#開頭的行為註釋行,且不能跨行。

sed詳解---用法及解釋