On Linux production environment, the most commonly used set of "Sed" skills

sedCommand widely used, simple to use, is a tool for fast text processing. It is actually not much skill, recitation, learning to use the most appropriate channel, which belongs to the hard skills. But it is very complicated, because too many advanced features. Benpian not concerned about the advanced features of sed, only some common operations, explained.

With use, you will find it and vimsome ideas are figured in, the regular expression syntax is basically the same, and not much learning costs. From a personal perspective and efficiency point of view, sed command is an important tool for programmers must master.

Those who say you can use the google site, mostly used to copy the text to excel, slowly soldiering, encountered large quantities of documents is confused. Not a person not into a door, this article is not written for you.

A simple entry


As shown, a simple sed command comprises three main parts: 参数, 范围, 操作. File to operate, can be directly linked to the last command line. In addition to the command line, sed -f parameter can also be specified by a sed script, this is an advanced usage, do not do too much description.

Some of the examples I will repeat the command several times, smart as you are sure to find where the law, and sometimes even have no need to explain.

parameter

-n  This parameter is --quietor --silentmeans. Show ignore the implementation process output, output only to our results.

We used there is another argument: -i.

After using this parameter, all changes will be performed in the original file. Your output will overwrite the original file. Very dangerous , we must pay attention.

range

1,4  represents the contents of the file to find the 1,2,3,4 line.
This range is specified very spiritual, consider the following example (Alternatively range portion in the figure itself).

5  Select line 5.

2,5  Select 2-5 lines, 4 lines.

1-2  selects odd rows.

2 ~ 2  to select even-numbered rows.

2, + 3  , and 2,5the effect is the same, a total of four lines.

2, $  from the second line to the end of the file.

The range of options can also use regular match. Consider the following example.

/ sys /, + 3  row selection sys words appear, and the latter three lines.

/ ^ sys /, / mem /  selects names that begin sys lines, and data lines between the mem words appear.

For illustrative, the following commands correspond above description, the range of operation and that there may be a space.

sed -n '5p' file
sed -n '2,5 p' file
sed -n '1~2 p' file
sed -n '2~2 p' file
sed -n '2,+3p' file sed -n '2,$ p' file sed -n '/sys/,+3 p' file sed -n '/^sys/,/mem/p' file

operating

The most common operation is p, meaning that print. For example, the following two commands is the equivalent of:

cat file 
sed -n 'p' file

In addition to printing, as well as the following, common to us.

p  matching content to print.

d  to delete the contents match. This time we should get rid of -nparameters, and think about why.

w  The contents are written to match elsewhere.

a, i, cAnd other operations, although basic but with less, does not describe. We still get some commands to illustrate.

sed -n '2,5 p' file
sed    '2,5 d' file
sed -n '2,5 w output.txt' file

Let's look at what some are capable sed command, the command point experience.

Delete all lines starting with # and blank lines.

sed -e 's/#.*//' -e '/^$/ d' file

The most commonly used, such as the following.

sed -n '2p' /etc/group

The second line group represents a print file.

1、参数部分 比如 -n
2、模式部分 比如'2p'
3、文件,比如/etc/group

Well, I think once multiple commands, sed do not want to write a script file how to do? It would need to add -e parameter.

sed operation units are .

Replace mode

These are sedcommonly used commands matching pattern, but it also has a powerful replacement mode, meaning that the search and replace some of these values, and outputs the result. Use replacement mode rarely used -nparameters.


Parameter substitution pattern a little more, but is the first portion and the fifth portion may be omitted. It will replace the entire text output out.

To match the number of the first half of the range, and the second half of the alternative operation execution.

range

The range and scope of the above syntax similar. See the examples below.

/ sys /, + 3  row selection sys words appear, and the latter three lines.

/ ^ sys /, / mem /  selects names that begin sys lines, and data lines between the mem words appear.

Specific command is:

sed '/sys/,+3 s/a/b/g' file
sed '/^sys/,/mem/s/a/b/g' file 

command

Command here is the s. That is the meaning of substitute.

Find a match

Find will find string section to be replaced. This part can accept pure strings, can also accept regular expressions. See the examples below.

a  search string line range a .

[a, b, c]  search string a or b or c in the range of from the line.

Command like this:

sed 's/a/b/g' file
sed 's/[a,b,c]/<&>/g' file
#这个命令我们下面解释

replace

It's time to find out the string to replace. Of this section will replace the contents to find the matching portion of the finds.

Unfortunately, this part can not use regular. Commonly used is the exact replacement. For example to replace a b.

But there are also advanced features. Python and java or similar regular api, sed replacement also has Matched Patternmeaning, can also be obtained Group, not the bottom. Commonly used for placeholder is &.

&No, repeat. When it is used in the replacement string time, it represents the raw data to find a match.

[&]  Shows that the searched data using [] surrounded.

"&"  Indicates that the data lookups using "" surrounded.

The following command will put each line in the file, use quotes around them.

sed 's/.*/"&"/' file

flag parameters

These parameters can be used individually, a plurality may be used to introduce only the most commonly used.

g 默认只匹配行中第一次出现的内容,加上g,就可以全文替换了。常用。

p 当使用了-n参数,p将仅输出匹配行内容。

w 和上面的w模式类似,但是它仅仅输出有变换的行。

i 这个参数比较重要,表示忽略大小写。

e 表示将输出的每一行,执行一个命令。不建议使用,可以使用xargs配合完成这种功能。

看两个命令的语法:

sed -n 's/a/b/gipw output.txt' file
sed 's/^/ls -la/e' file

好玩

由于正则的关系,很多字符需要转义。你会在脚本里做些很多\\\*之类的处理。你可以使用|^@!四个字符来替换\
比如,下面五个命令是一样的。

sed '/aaa/s/\/etc/\/usr/g' file
sed '/aaa/s@/etc@/usr@g' file
sed '/aaa/s^/etc^/usr^g' file
sed '/aaa/s|/etc|/usr|g' file
sed '/aaa/s!/etc!/usr!g' file

注意:前半部分的范围是不能使用这种方式的。我习惯使用符号@

其他

正则表达式

可以看到,正则表达式在命令行中无处不在。以下,紧做简要说明。

^ 行首

$ 行尾

. 单个字符

* 0个或者多个匹配

+ 1个或者多个匹配

? 0个或者1个匹配

{m} 前面的匹配重复m次

{m,n} 前面的匹配重复m到n次

** 转义字符

[0-9] 匹配括号中的任何一个字符,or的作用
| or,或者

b 匹配一个单词。比如\blucky\b 只匹配单词lucky

参数i

上面已经简单介绍了参数i,它的作用是让操作在原文件执行。无论你执行了啥,原始文件都将会被覆盖。这是非常危险的。
通过加入一个参数,可以将原文件做个备份。

sed -i.bak 's/a/b/' file

以上命令会对原file文件生效,并生成一个file.bak文件。强烈建议使用i参数同时指定bak文件。

表演一下

我们通过两个命令,来稍微看下sed和其他命令组合起来的威力。

输出长度不小于50个字符的行

sed -n '/^.{50}/p'

统计文件中有每个单词出现了多少次

sed 's/ /\n/g' file | sort | uniq -c

查找目录中的py文件,删掉所有行级注释

find ./ -name "*.py" | xargs sed -i.bak '/^[ ]*#/d'

查看第5-7行和10-13行

sed -n -e '5,7p' -e '10,13p' file

仅输出ip地址

ip route show | sed -n '/src/p' | sed -e 's/ */ /g' | cut -d' ' -f9

End

Guess you like

Origin www.cnblogs.com/sunsky303/p/10979424.html