TOC
Sed
sed is a steam editor. sed treats multiple input files as one long stream.
The full format for invoking sed is:
sed OPTIONS... [SCRIPT] [INPUTFILE...]
Some common OPTIONS:
-n
to suppress output, sed -n '45p' file.txt
this command prints only line 45 of input file;
-e
options are used to specify a script expression, such as sed -e 's/hello/world/' input.txt > output.txt
;
-f
specify a script file, such as sed -f myscript.sed input.txt > output.txt
-r
is to enable extended regular expressions
How sed Works
sed
maintains two data buffers: the active pattern space
, and the auxiliary hold space
. Both are initially empty.
sed
operates by performing the following cycle on each line of input: first, sed reads one line from the input stream, removes any trailing newline, and places it in the pattern space. Then commands are executed; each command can have an address associated to it: addresses are a kind of condition code, and a command is only executed if the condition is verified before the command is to be executed.
sed exit status
0
: Successful completion.
1
: Invalid command, invalid syntax, invalid regular expression
2
: one or more input file specified could not be open
4
: an I\O error
$ echo | sed 'Q42' ; echo $?
sed: 1: "Q42": invalid command code Q
1
sed script
sed script syntax: [addr]X[options]
For example:
$ echo 123| sed "/1/s/1/4/", Output: 423
In this example, /1/
is addr, s
is command, /1/4/
is the options of s
[addr]
is an optional line address. If [addr] is specified, the command X will be executed only on the matched lines. It can be a single line number, a regular expression, or a range of lines (see sed addresses);
X
is a single-letter command;
More examples:
sed '30,35d' input.txt > output.txt
deletes lines 30 to 35 in the input.
sed '/^foo/q42' input.txt > output.txt
, if the line starting with “/^foo/” is found, sed will terminate with exit status 42. “/^foo/” is a regular-expression address.
Multiple scripts
script or script-file can be seperated by semicolon(;) or newlines(ASCII 10).
Follow examples are equivalent.
sed '/^foo/d ; s/hello/world/' input.txt > output.txt
sed -e '/^foo/d' -e 's/hello/world/' input.txt > output.txt
echo '/^foo/d' > script.sed
echo 's/hello/world/' >> script.sed
sed -f script.sed input.txt > output.txt
echo 's/hello/world/' > script2.sed
sed -e '/^foo/d' -f script2.sed input.txt > output.txt
sed commands summary
b label
branch unconditionally to label.
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.
g
Replace the contents of the pattern space with the contents of the hold space.
{cmd;cmd;cmd}
group several commands together.
;
is the delimiter between commands. for example /regexp/{ X ; X ; X }
:x
, Labels are defined with a colon followed by one or more letters (e.g. ‘:x’)
sed s
command example
s script syntax: s/regexp/replacement/flags
for example:
echo "aaa" | s/aaa/bbb/
substitute aaa
with bbb
;
echo "aaa" | sed "s_aaa_bbb_"
this command also substitute aaa
with bbb
;
Note that: the character after s
is a delimiter.
echo "aaa" | sed -r "s/[a-z]+/bb/"
: here -r
is to enable extended regular expressions and +
means match one or more times.
Mac OS X and FreeBSD uses -E instead of -r.
sed -E "/\:\/\//n;s|\/\/.*||"
subtitute comments starting with //
, but not subtitute the line starting with “://” and “https://";
- replacement can contain
\n
, n is number from 1 to 9;
flags
g
Apply the replacement to all matches to the regexp, not just the first.
For example, (following commands are executed on mac)
echo "a-b-" |sed -E "s/(b-|-)/pp/"
Output: appb-
Sript with g flags
echo "a-b-" |sed -E "s/(b-|-)/pp/g"
output: apppp
number
Only replace the numberth match of the regexp.
using &
in replacement reference the matched string
echo "(abc) 123" |sed -r "s/[a-z]+/& is a string/"
output: (abc is a string) 123
&
characters reference the whole matched portion of the pattern space.
Addresses overview
The following command replaces the word “hello” with “world” on line 144;
sed '144s/hello/world/' input.txt > output.txt
The following command replaces the word “hello” with “world” in lines containing the word “apple”
sed '/apple/s/hello/world/' input.txt > output.txt
Appending the ! character to the end of an address specification (before the command letter) negates the sense of the match. That is, if the ! character follows an address or an address range, then only lines which do not match the addresses will be selected. The following command replaces the word ‘hello’ with ‘world’ only in lines not containing the word ‘apple’:
sed '/apple/!s/hello/world/' input.txt > output.txt
Other examples
grep -r "test_1" ./*|awk --field-separator=":" '{print $1}'|xargs -n 1 sed -i "s/test_1/subtitute_1/g"
Following command will replace dl-cdn.alpinelinux.org
with mirrors.tencent.com
in /etc/apk/repositories
file:
$ sed -i 's/dl-cdn.alpinelinux.org/mirrors.tencent.com/g' /etc/apk/repositories
REFERENCE:
1.Sed
2.sed
「点个赞」
点个赞
使用微信扫描二维码完成支付
