How to use sed command in Linux

  

Linux sed command is mainly used for text processing, can handle the lines and columns of text, the following small series will give you a detailed introduction to the usage of the sed command in Linux, interested friends may wish Let's understand it.

sed command format is:

sed [-nefri] & lsquo; command & rsquo; input text

common options:

-n: Use the silent mode. In the general sed usage, all data from STDIN will generally be listed on the screen. However, if the -n parameter is added, only the line (or action) that has been specially processed by sed will be listed.

-e: Perform sed action editing directly in command line mode;

-f: directly write sed action in a file, -f filename can execute inside filename Sed action;

The action of -r:sed supports the syntax of extended regular notation. (The default is the basic regular notation syntax)

-i: Directly modify the contents of the read file instead of being output by the screen.

Commonly used commands:

a : New, a can be followed by a string, and these strings will appear on a new line (current next line) ~

c : Substitute, c can be followed by a string, these strings can replace the line between n1, n2!

d: delete, because it is deleted, so d usually does not pick up any 咚咚;

i: insert, i can be followed by a string, and these strings will be new The line appears (currently the previous line);

p: prints, and also prints a selected item. Usually p will work with the parameter sed -n~

s : Replace, you can directly replace the work! Usually this s action can be combined with regular notation! For example, 1,20s/old/new/g is it!

Example: (assuming we have a file named ab)

Delete a line

[root@localhost ruby] # sed ‘1d’ ab # Delete the first line

[root@localhost ruby] # sed ‘$d’ ab # Delete the last line

[root@localhost ruby] # sed ‘1,2d’ ab # Delete the first line Go to the second line

[root@localhost ruby] # sed ‘2,$d’ ab # Delete the second line to the last line

Display a line

. [root@localhost ruby] # sed -n ‘1p’ ab #Show the first line

[root@localhost ruby] # sed -n ‘$p’ ab #Show the last line

[root@localhost ruby] # sed -n ‘1,2p’ ab #Show the first line to the second line

[root@localhost ruby] # sed -n ‘2,$p&rsquo ; ab #Show the second line to the last line

Use the mode to query

[root@localhost ruby] # sed -n ‘/ruby/p’ ab #Query including the keyword ruby All lines

[root@localhost ruby] # sed -n ‘/\\$/p’ ab #Query all lines including the keyword $, use backslashes\\mask special meaning

Add one or more lines of string

[root@localhost ruby]# cat ab

Hello!

ruby ​​is me,welcome to my blog.

end

[root@localhost ruby] # sed ‘1a drink tea’ ab #Add characters after the first line String “drink tea”

Hello!

drink tea

ruby ​​is me,welcome to my blog.

end

[root@localhost ruby] # sed ‘1,3a drink Tea’ ab #Add the string after the first line to the third line“drink tea”

Hello!

drink tea

ruby ​​is me,welcome to my blog.

drink tea

end

drink tea

[root@localhost ruby] # sed ‘1a drink tea\ or coffee’ ab #Add multiple lines after the first line, use line breaks \

Hello!

drink tea

or coffee

ruby ​​is me,welcome to my blog.

end

instead of one or more lines< Br>

[root@localhost ruby] # sed ‘1c Hi’ ab #The first line is replaced by Hi

Hi

ruby ​​is me,welcome to my blog.

end

[root@localhost ruby] # sed ‘1,2c Hi’ ab #The first line to the second line instead of Hi

Hi

end

Replace a part of a line

Format: sed & lsquo;s/string to be replaced /new string /g’ (The string to be replaced can be expressed by regular expression式)

[root@localhost ruby] # sed -n ‘/ruby/p’ ab |  Sed ‘s/ruby/bird/g’ #replace ruby ​​as bird

[root@localhost ruby] # sed -n ‘/ruby/p’ ab |  Sed ‘s/ruby//g’ #去除ruby

Insert

[root@localhost ruby] # sed -i ‘$a bye’ ab #last line in file ab Directly enter “bye”

[root@localhost ruby]# cat ab

Hello!

ruby ​​is me,welcome to my blog.

end

bye

The above is the usage of the sed command under Linux, the usage of the sed command Similar to awk, but the sed command is more powerful for column processing, and you can update it according to the actual situation.

Copyright © Windows knowledge All Rights Reserved