sed command

1. sed command

sed is a non-interactive text editor that can edit text files as well as standard output.

The sed command will select a line of data from the file or standard output, copy it into the buffer, then read the edited subcommand, and edit the line, repeating this process until all the text lines are processed.

The file edited by the sed command is a copy of the original file, so the editing operation does not affect the original file, and only outputs to the screen. If you want to save it, you can redirect it to a disk file.



2. How to use the sed command

grammar:

sed [options] [script] [input file]


Options:

-n: suppress default output. Only specially treated lines are displayed.

-e: run executes multiple scripts

-f: followed by the script name, read commands from the script

-i: Modify the original file directly.

-I: uppercase i, case insensitive.

-l: Specifies the length of the line.

-r: Use extended regular expressions in scripts.

-s: By default, sed treats multiple filenames specified on the command line as a long continuous input stream. GNU sed allows them to be treated as separate files, so that regular expressions don't match across files.

-u: minimally cache input and output.


script:

Users can write a series of sed commands in a script file, and sed will read from the file and execute each command in sequence.


Output file:

Indicates the input file. If no input file is specified, it will be read from the standard input.


Three ways of working:

1. Execute the sed command directly on the command line. sed [options] [command] [file providing data]

2. Write the operation of sed into the script, and specify the script through -f. sed [options] -f [script] [file providing data]

3. Write sed to the script, and the interpreter is #!/bin/sed. ./scrpt [file providing data]


Positioning lines of text:

Representation

significance

example

x

x is an integer indicating the number of rows.

If the example locates to the 10th row, use 10

x,y

Navigate to consecutive lines, starting with line x and ending with line y.

The example locates lines 2-5. then 2,5

x~s

Start line and step size, x line start, s is the step size

For example, to locate even-numbered lines, then 0~2

$

Indicates to locate the last line.

For example, the last line $, the first line is 1

x,+n

Indicates starting from line x, followed by n lines

For example, from the 2nd line and the next 5 lines, then 2,+5


Regular expressions locate lines of text: the syntax is /regexp/

Regular

significance

/general characters/

The matching character itself, e.g. /abc/ matches the line containing abc

*

Indicates that the preceding character is repeated 0 or more times. For example /a*/ means that a appears 0 or more times

\+

Indicates one or more occurrences of the preceding character. Extended Regular Expression

\?

Indicates that the preceding character occurs 0 or 1 times

\{x\}

Indicates that the previous character appears x times, for example /a\{3\}/ matches aaa

\{x,y\}

Indicates that the preceding character appears x to y times. Example /a\{1,2\}/ matches a or aa

\{x,\}

Indicates that the preceding character appears at least x times.

.

matches any character

^

Match the first character of the line, for example ^aa matches the line starting with aa

$

Match end-of-line characters, for example aa$ matches lines ending with aa

[]

matches any character within brackets

[^]

Matches characters that do not appear in brackets

\n

match newline



3. Common operations and usage of sed

3.1 Basic syntax of sed editing commands - general syntax

grammar:

[position parameter1[,position parameter2]] subcommand [subcommand parameter]


introduce:

Positional parameters: can be represented by line numbers or regular expressions. With positional arguments specified, operate on matching lines. Operates on the entire content without specifying positional arguments.

Subcommands: Subcommands provided by sed to implement editing operations.

Subcommand Arguments: Arguments for the subcommand.


Subcommand:

p: Output the text line in the buffer area.

s: Substitute command. The replacement mode can be determined by the flag, g: global. Decimal digits: the first number. p: first and output. w: first and output to disk. Empty: replace the first one.

d: Delete text.

a: Append content on a new line below the matched line.

i: Append content on a new line above the matched line.


3.2 Select text p

Subcommand p can display the parameters filtered by the positional parameters. . The syntax is as follows:

[position parameter 1[, position parameter 2]] p


Example:

1. The method of line positioning, display the text line.

Display lines 1-3 of the file without the -n option.

# sed 1,3p 1.txt

eeee

eeee

asdada

asdada

adadas

adadas

adsASQWE

SGDFGE


To display only lines 1 to 3 of the file, use the -n option.

# sed -n 1,3p 1.txt

eeee

asdada

adadas


2. The method of regular positioning to display the text line.

show lines containing dada

# sed -n '/dada/p'  1.txt

asdada

adadas


3.3 替换文本 s

s子命令可以对文本进行替换。

[位置参数1[,位置参数2]]  s/匹配字符/替换字符/[标志]


标志:

g:全局匹配,会替换文本行中的所有符合规则的字符串。

十进制数字:数字为n,则替换第n个符合规则的字符串。

p:替换第一个,并输出到标准输出。

w:替换第一个,并输出到磁盘文件中。

空:不加标志,则替换第一个。


实例:

1.替换文件的内容。

把文件的a替换成大写A。

# sed  's/a/A/g' 1.txt  

eeee

AsdAdA

AdAdAs

AdsASQWE


2.对指定的行进行替换。

把文件1~3行的a替换成A。

# sed '1,3  s/a/A/g' 1.txt      

eeee

AsdAdA

AdAdAs

adsASQWE


3.4 删除文本 d

子命令d可以实现文本行删除。语法如下:

[位置参数1[,位置参数2]]  d


实例:

1.删除文件的行

删除文件的第一行

# sed '1d' 1.txt

asdada

adadas

adsASQWE


删除文件的最后一行

# sed '$d' 1.txt


删除空白行

# sed '/^$/d' 1.txt  


3.5 追加文本 a

在匹配到的行的下面插入文本。

[位置参数1]  a  文本


实例:

在passwd文件 root 开头的行的后面插入一行aaaaaaaaa

# sed '/^root/ a aaaaaaaaaaaaaaaaaaa' passwd

root:x:0:0:root:/root:/bin/bash

aaaaaaaaaaaaaaaaaaa

bin:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin


3.6 插入文本 i

在匹配到的行的上面插入文本。

[位置参数1]  i  文本


实例:

在passwd文件 root 开头的行的上面插入一行aaaaaaaaa

# sed '/^root/ i aaaaaaaaaaaaaaaaaaa' passwd

aaaaaaaaaaaaaaaaaaa

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin


3.7 调换位置 ()

例如调换passwd文件第一列和最后一列的位置。可以使用sed,语法如下:

sed -r 's/(表达式1):(表达式2):(表达式3)/\3\2\1/' [文件/数据流]


实例1:

调换passwd文件第一列和最后一列的位置

# sed -r 's/([^:]+):(.*):([^:]+)/\3:\2:\1/'  passwd

/bin/bash:x:0:0:root:/root:root

/sbin/nologin:x:1:1:bin:/bin:bin

/sbin/nologin:x:2:2:daemon:/sbin:daemon


解释:

([^:]+):(.*):([^:]+):根据顺序,每一个小括号都在以后变换位置的时候,分别对应 123。

([^:]+)::匹配不是冒号的字符一个或多个然后分隔符是冒号。匹配到的是第一段。

(.*)::匹配任意个任意字符,到冒号结束。也就是匹配第一个冒号和最后一个冒号之间的部分。

([^:]+):匹配的不是冒号的字符一个或多个。也就是匹配了最后一段。

\3:\2:\1:123分别表示三个括号的内容,使用\3\2\1表示重新排列。:即分割符。


实例2:

在passwd文件的最前面插入一段。

# sed -r 's/(.*)/aaa:&/' passwd

aaa:root:x:0:0:root:/root:/bin/bash

aaa:bin:x:1:1:bin:/bin:/sbin/nologin

aaa:daemon:x:2:2:daemon:/sbin:/sbin/nologin


解释:

(.*):可以匹配整个行,用小括号括起来是为了用数字调用。

aaa:&:aaa:是插入的内容。&表示括号的内容。也可以用 转义1(\1)表示。


实例3:

把文件中的4段倒叙排列。

# cat 1.txt

111:222:333:444

# sed -r 's/(.*):(.*):(.*):(.*)/\4:\3:\2:\1/' 1.txt  

# sed -r 's/(.*):(.*):(.*):([^:]+)/\4:\3:\2:\1/' 1.txt

444:333:222:111



4. 组合命令

sed支持多个子命令组合一起用。对文本进行多个不同的操作,使用-e选项可以在一条sed命令中完成这些操作。


4.1 使用-e执行多个子命令

可以将sed后面的多个子命令使用-e链接起来。

# sed -e '/^1111$/ a 2222' -e '/^1111$/ i 0000' 2.txt

0000

1111

2222

3333


4.2 使用;分号执行多个子命令

# sed -n -e 's/a/A/g ; 1,3p' 1.txt

eeee

AsdAdA

AdAdAs


4.3对一个地址使用多个子命令

如果对一个文件指定的行进行很多次操作, sed命令提供了对同一个地址使用多次子命令的语法。如下:

位置参数 {

子命令1

子命令2

子命令3

...

}

也可以写在一行

位置参数 {子命令1;子命令2;子命令3;...}


实例:

对 1.txt 的1~5行,进行2次替换,和一次插入。

# sed -n '1,5 {

>s/a/A/g

> s/b/B/g

> 2 i 20180425

> p

> }' 1.txt


4.4 sed脚本

还可以把sed脚本写在脚本里。脚本里的特殊字符需要转义。格式如下:

sed -f 脚本 [文件/数据流]


实例:

就是把子命令一行一行的写在脚本里,然后通过-f指定脚本文件即可。

# vim 1.sed

s/a/A/g

s/b/B/g

2 i 20180425


# sed -f 1.sed 1.txt

eeee

20180425

AsdAdA

AdAdAs

AdsASQWE

SGDFGE

XVCZS

dsgfsd

dfsd


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324915312&siteId=291194637