文章目錄
- sed beginner
- reference
- sed語句基本組成和要點
- s command flags标志清單
- 常用内容
- 指定分界符
- sed script
- 确定學習目标
- sed使用案例
- sed n command(optional/old-fashioned)
- 參考手冊閱讀指南
- sed 指令( commands of sed)清單
- index of sed references(manual)
sed beginner
reference
- sed, a stream editor (gnu.org)
sed語句基本組成和要點
sed 中有三個基本概念:
option
,
command
,
flag
- sed
(option of sed)option
- sed
(command of sed)command
- sed
command withs
(flag of sed command)flag
核心内容
- sed 最常用的command是替換(s指令)
- 替換指令可以處理
- 修改
- 删除(将内容替換為空)
s command flags标志清單
The
s
command can be followed by zero or more of the following flags:
-
g
Apply the replacement to all matches to the regexp, not just the first.
-
number
Only replace the number th match of the regexp.
interaction in
s
command Note: the POSIX standard does not specify what should happen when you mix the
g
and number modifiers, and currently there is no widely agreed upon meaning across
sed
implementations.
For GNU
sed
, the interaction is defined to be: ignore matches before the number th, and then match and replace all matches from the number th on.
-
p
If the substitution was made, then print the new pattern space.
Note: when both the
p
and
e
options are specified, the relative ordering of the two produces very different results. In general,
ep
(eval(0, 0, 0, 0.06); padding: 0px 2px; border-radius: 6px; line-height: inherit; overflow-wrap: break-word; text-indent: 0px;">sed interprets specially the presence of
p
options both before and after
e
, printing the pattern space before and after eval(0, 0, 0, 0.06); padding: 0px 2px; border-radius: 6px; line-height: inherit; overflow-wrap: break-word; text-indent: 0px;">s command show their effect just once. This behavior, although documented, might change in future versions.
-
w filename
If the substitution was made, then write out the result to the named file. As a GNU
sed
extension, two special values of filename are supported: /dev/stderr, which writes the result to the standard error, and /dev/stdout, which writes to the standard output.3
-
e
This command allows one to pipe input from a shell command into pattern space. If a substitution was made, the command that is found in pattern space is executed and pattern space is replaced with its output. A trailing newline is suppressed; results are undefined if the command to be executed contains a NUL character. This is a GNU
sed
extension.
-
I i
The
I
modifier to regular-expression matching is a GNU extension which makes
sed
match regexp in a case-insensitive manner.
-
M m
The
M
modifier to regular-expression matching is a GNU
sed
extension which directs GNU
sed
to match the regular expression in multi-line mode. The modifier causes
^
and
$
to match respectively (in addition to the normal behavior) the empty string after a newline, and the empty string before a newline. There are special character sequences (
\`
and
\'
) which always match the beginning or the end of the buffer. In addition, the period character does not match a new-line character in multi-line mode.
常用内容
指定分界符
\%regexp%
(The
%
may be replaced by any other single character.)
- This also matches the regular expression regexp, but allows one to use a different delimiter than
. This is particularly useful if the regexp itself contains a lot of slashes, since it avoids the tedious escaping of every/
./
- If regexp itself includes any delimiter characters, each must be escaped by a backslash (
).\
- The following commands are equivalent.
- They print lines which start with ‘/home/alice/documents/’:
sed -n '/^\/home\/alice\/documents\//p'
sed -n '\%^/home/alice/documents/%p'
sed -n '\;^/home/alice/documents/;p'
sed script
- A
program consists of one or more sed
commands, passed in by one or more of the -e, -f, --expression, and --file options,sed
- or the first non-option argument if zero of these options are used.
-
commands follow this syntax:sed
[addr]commandX[options(of commandX)]
is always required in every complete and valid sed command.
commandX
- commandX
- add commandX
- commandX options
- add commandX options
-
is a single-lettercommandX
command.sed
-
is an optional line address.[addr]
- If
is specified, the command X will be executed only on the matched lines.[addr]
-
can be a single line number, a regular expression, or a range of lines (seesed addresses).[addr]
- Additional
are used for some[options]
commands.sed
- The following examples are all equivalent.
- They perform two
operations:sed
- deleting any lines matching the regular expression
, and replacing all occurrences of the string ‘hello’ with ‘world’:/^foo/
#method1:
sed '/^foo/d ; s/hello/world/' input.txt > output.txt
#method2:
sed -e '/^foo/d' -e 's/hello/world/' input.txt > output.txt
#method3:
echo '/^foo/d' > script.sed
echo 's/hello/world/' >> script.sed
sed -f script.sed input.txt > output.txt
#method4:
echo 's/hello/world/' > script2.sed
sed -e '/^foo/d' -f script2.sed input.txt > output.txt
Commands ,
a
,
c
, due to their syntax, cannot be followed by semicolons working as command separators and thus should be terminated with newlines or be placed at the end of a script or script-file.
i
- Commands can also be preceded with optional non-significant whitespace characters. SeeMultiple commands syntax.
确定學習目标
- 完全掌握sed需要付出一些努力和時間
- 初次學習sed的我的目标是
- 學會注釋指定行
- 再指定行下方插入若幹文本(文本行)
- 要做到這兩點,需要掌握行定位(基礎)
- 可能還需要了解如何比對特殊字元文本
如果有具體的需求,可以把需求細化,然後分别搜尋解決方案,這有望使用最少的時間來解決需求
sed使用案例
下面這段代碼,能夠為你執行安裝oh my zsh shell架構
涉及的sed操作如下
- 注釋比對到的特定行(比對和替換)
- 在指定行下執行插入操作(插入)
- 将處理的結果就地儲存,其在儲存修改之前做一個備份(檔案名為
結尾)
E
# 工作目錄設定為使用者家目錄
cd ~
sudo apt update
sudo apt install zsh curl git man wget -y
wget https://gitee.com/mirrors/oh-my-zsh/raw/master/tools/install.sh
# 由于國内網絡問題,可能需要多嘗試幾次一下source 指令才可以安裝成功.(我将其注釋掉,采用換源後再執行clone
#source install.sh
#本段代碼将修改install.sh中的拉取源,以便您能夠沖gitee上成功将需要的檔案clone下來.
# 本段代碼會再修改前做備份(備份檔案名為install.shE)
sed '/(^remote)|(^repo)/I s/^#*/#/ ;
/^#*remote/I a\
REPO=${REPO:-mirrors/oh-my-zsh}\
REMOTE=${REMOTE:-https://gitee.com/${REPO}.git} ' -r -iE ~/install.sh
# 執行安裝
source install.sh
#傳回到腳本所在目錄(以便執行新的腳本)
cd -
sed n command(optional/old-fashioned)
- 該sed中附屬指令n(next),表示跳過上一行,直接處理下一行
- sed處理文筆是一行行讀入(input);預設情況下,是每讀取一行就處理(執行sed 處理腳本)一行
- 但是被跳過的行仍然預設原樣列印出來
- 但是如果使用了
,就可以跳行處理,每使用一個
n;
,效果相當于執行跳躍步長+1;預設步長為1
n;
- 即,(冒号
是sed script中表示一條指令的結束)
;
- 當使用了
每間隔0行處理一次(即,每行都處理(預設行為));
n;
- 當使用了
每間隔1行處理一次;
n;
- 當使用了
每間隔2行處理一次;
n;n;
- If auto-print is not disabled, print the pattern space, then, regardless, replace the pattern space with the next line of input. If there is no more input then
exits without processing any more commands.sed
This command is useful to skip lines (e.g. process every Nth line).
$ seq 6 | sed 'n;n;s/./x/'
1
2
x
4
5
x
$ seq 6 | sed '0~3s/./x/'
1
2
x
4
5
x
參考手冊閱讀指南
- sed 的一個重點是文本比對(通過指定位址address(位址區間address range))來篩選sed的操作範圍/對象
- 文本所在行的比對
- 較為簡單
- 隻有被比對到的行才會被執行相應的sed操作
- (要和s指令中的被替換内容差別開,位址區間所指定的範圍是sed command 要作用的範圍,而s指令中的被替換内容的指定需要另外通過正則指定)
- 這有點而想css選擇器選中的元素才會被表現出指定的樣式那麼般
- 行級别的比對可以是單行,也可以是指定範圍區間内的若幹行
- 我們可以用正規表達式比對文本中的精确位置的字元,但是在列印結果的時候,預設是整行的列印
- 即使我們的表達式指定了開頭(
)和結尾^
)$
- 但是這不影響我們精确替換文本行中的字元
- 文本精确位置比對
- 需要用sed規範下的正規表達式來比對
- sed 最常用的command是替換(s指令)
- 手冊 3.4部分給出常用指令;
- 第7部分給出了許多使用經典的sed使用案例
sed 指令( commands of sed)清單
sed commands summary
The following commands are supported in GNU sed. Some are standard POSIX commands, while other are GNU extensions. Details and examples for each command are in the following sections. (Mnemonics) are shown in parentheses.
a\
text
Append text after a line.
a text
Append text after a line (alternative syntax).
b label
Branch unconditionally to label. The label may be omitted, in which case the next cycle is started.
c\
text
Replace (change) lines with text.
c text
Replace (change) lines with text (alternative syntax).
d
Delete the pattern space; immediately start next cycle.
D
If pattern space contains newlines, delete text in the pattern space up to the first newline, and restart cycle with the resultant pattern space, without reading a new line of input.
If pattern space contains no newline, start a normal new cycle as if the d command was issued.
e
Executes the command that is found in pattern space and replaces the pattern space with the output; a trailing newline is suppressed.
e command
Executes command and sends its output to the output stream. The command can run across multiple lines, all but the last ending with a back-slash.
F
(filename) Print the file name of the current input file (with a trailing newline).
g
Replace the contents of the pattern space with the contents of the hold space.
G
Append a newline to the contents of the pattern space, and then append the contents of the hold space to that of the pattern space.
h
(hold) Replace the contents of the hold space with the contents of the pattern space.
H
Append a newline to the contents of the hold space, and then append the contents of the pattern space to that of the hold space.
i\
text
insert text before a line.
i text
insert text before a line (alternative syntax).
l
Print the pattern space in an unambiguous form.
n
(next) If auto-print is not disabled, print the pattern space, then, regardless, replace the pattern space with the next line of input. If there is no more input then sed exits without processing any more commands.
N
Add a newline to the pattern space, then append the next line of input to the pattern space. If there is no more input then sed exits without processing any more commands.
p
Print the pattern space.
P
Print the pattern space, up to the first <newline>.
q[exit-code]
(quit) Exit sed without processing any more commands or input.
Q[exit-code]
(quit) This command is the same as q, but will not print the contents of pattern space. Like q, it provides the ability to return an exit code to the caller.
r filename
Reads file filename.
R filename
Queue a line of filename to be read and inserted into the output stream at the end of the current cycle, or when the next input line is read.
s/regexp/replacement/[flags]
(substitute) Match the regular-expression against the content of the pattern space. If found, replace matched string with replacement.
t label
(test) Branch to label only if there has been a successful substitution since the last input line was read or conditional branch was taken. The label may be omitted, in which case the next cycle is started.
T label
(test) Branch to label only if there have been no successful substitutions since the last input line was read or conditional branch was taken. The label may be omitted, in which case the next cycle is started.
v [version]
(version) This command does nothing, but makes sed fail if GNU sed extensions are not supported, or if the requested version is not available.
w filename
Write the pattern space to filename.
W filename
Write to the given filename the portion of the pattern space up to the first newline
x
Exchange the contents of the hold and pattern spaces.
y/src/dst/
Transliterate any characters in the pattern space which match any of the source-chars with the corresponding character in dest-chars.
z
(zap) This command empties the content of pattern space.
#
A comment, until the next newline.
{ cmd ; cmd ... }
Group several commands together.
=
Print the current input line number (with a trailing newline).
: label
Specify the location of label for branch commands (b, t, T).
index of sed references(manual)
- 1 Introduction
- 2 Running sed
- 2.1 Overview
- 2.2 Command-Line Options
- 2.3 Exit status
- 3
scriptssed
- 3.1
script overviewsed
- 3.2
commands summarysed
- 3.3 The
Commands
- 3.4 Often-Used Commands
- 3.5 Less Frequently-Used Commands
- 3.6 Commands for
gurussed
- 3.7 Commands Specific to GNU
sed
- 3.8 Multiple commands syntax
- 3.8.1 Commands Requiring a newline
- 4 Addresses: selecting lines
- 4.1 Addresses overview
- 4.2 Selecting lines by numbers
- 4.3 selecting lines by text matching
- 4.4 Range Addresses
- 5 Regular Expressions: selecting text
- 5.1 Overview of regular expression in
sed
- 5.2 Basic (BRE) and extended (ERE) regular expression
- 5.3 Overview of basic regular expression syntax
- 5.4 Overview of extended regular expression syntax
- 5.5 Character Classes and Bracket Expressions
- 5.6 regular expression extensions
- 5.7 Back-references and Subexpressions
- 5.8 Escape Sequences - specifying special characters
- 5.8.1 Escaping Precedence
- 5.9 Multibyte characters and Locale Considerations
- 5.9.1 Invalid multibyte characters
- 5.9.2 Upper/Lower case conversion
- 5.9.3 Multibyte regexp character classes
- 6 Advanced
: cycles and bufferssed
- 6.1 How
Workssed
- 6.2 Hold and Pattern Buffers
- 6.3 Multiline techniques - using D,G,H,N,P to process multiple lines
- 6.4 Branching and Flow Control
- 6.4.1 Branching and Cycles
- 6.4.2 Branching example: joining lines
- 7 Some Sample Scripts
- 7.1 Joining lines
- 7.2 Centering Lines
- 7.3 Increment a Number
- 7.4 Rename Files to Lower Case
- 7.5 Print
Environmentbash
- 7.6 Reverse Characters of Lines
- 7.7 Text search across multiple lines
- 7.8 Line length adjustment
- 7.9 Reverse Lines of Files
- 7.10 Numbering Lines
- 7.11 Numbering Non-blank Lines
- 7.12 Counting Characters
- 7.13 Counting Words
- 7.14 Counting Lines
- 7.15 Printing the First Lines
- 7.16 Printing the Last Lines
- 7.17 Make Duplicate Lines Unique
- 7.18 Print Duplicated Lines of Input
- 7.19 Remove All Duplicated Lines
- 7.20 Squeezing Blank Lines
- 8 GNU
’s Limitations and Non-limitationssed
- 9 Other Resources for Learning About
sed
- 10 Reporting Bugs
- Appendix A GNU Free Documentation License
- Concept Index
- Command and Option Index