1、sed Command specification

sed It is mainly used to select data 、 Replace 、 Delete 、 New orders ,grep,awk,sed It is called the three swordsmen of text operation .

Let's see. sed The syntax of the command :

[[email protected] ~]# sed [ Options ] '[ action ]'  file name 

Options :

  • -n: commonly sed The command will output all the data to the screen , If you add this option , It will only pass through sed Command processing lines output to the screen .
  • -e: Allows you to apply more than one... To input data sed Command to edit .
  • -f Script name : from sed Read in the script sed operation . and awk Ordered -f Very similar .
  • -r: stay sed Extended regular expressions are supported in .
  • -i: use sed The result of the modification directly modifies the file that reads the data , Not from the screen .

action :

  • a \: Additional , Add one or more rows after the current row . When adding multiple lines , Except for the last line , You need to use... At the end of each line \ It means the data is not finished .( If you do not add multiple rows , Just ignore it \
  • c \: Row substitution , use c The following string replaces the original data line , When replacing multiple lines , Except for the last line , At the end of each line \ It means the data is not finished .( If you do not add multiple rows , Just ignore it \
  • i \: Insert , Insert one or more lines before the current line . When inserting multiple lines , Except for the last line , You need to use... At the end of each line \ It means the data is not finished .( If you do not add multiple rows , Just ignore it \
  • d: Delete , Delete the specified row .
  • p: Print , Output the specified line .
  • s: String replacement , Replace one string with another . The format is Line scope s/ Old string / New string /g( and vim The substitution format in is similar to ).

Tips :

about sed Command everyone to pay attention to ,sed The changes do not directly change the contents of the file ( If it's the output of a command received with a pipeline character , In this case, there are no documents ), Instead, the modification results are only displayed on the screen , Unless used -i Option will directly modify the file .

Small instructions :

vim You can modify the contents of the file directly , We also use a lot , Why do you have to learn sed Orders ?

because vim We need to interact with the computer manually , Some editing instructions need to be entered manually , Save instructions , Switching mode command, etc , To complete vim The editing operation of .

stay Shell It is difficult to achieve human-computer interaction in , therefore vim There is no way to operate in Shell Program usage , So pass sed Order to complete , This is a Shell The most important way to modify the contents of a file .

2、 Row data operation

Use the following text student.txt

ID      Name    Python  Linux   MySQL   Java
1 Tangs 88 87 86 85.55
2 Sunwk 99 98 97 96,66
3 Zhubj 77 76 75 74.44
4 Shahs 66 65 64 63.33

(1) Look at the data in the file

Let's give a few examples sed What are the orders for .

Look at the student.txt The second line of the document , Then we can use p action , Execute the following command :

[[email protected] tmp]# sed '2p' student.txt
ID Name Python Linux MySQL Java
1 Tangs 88 87 86 85.55
1 Tangs 88 87 86 85.55
2 Sunwk 99 98 97 96.66
3 Zhubj 77 76 75 74.44
4 Shahs 66 65 64 63.33

As can be seen from the above results ,p The command does output the second line of data , however sed The command will also output all the data once , Then you will see this strange result , The data in the second row is output twice .

If you want to specify a row of data to be output , Need -n Help with options .

[[email protected] tmp]# sed -n '2p' student.txt
1 Tangs 88 87 86 85.55

(2) Delete the data in the file

Delete student.txt The second to fourth lines of data in the text , The order is as follows :

[[email protected] tmp]# sed  '2,4d' student.txt
ID Name Python Linux MySQL Java
4 Shahs 66 65 64 63.33

2,4 Said to delete 2 to 4 Row data .

But here's the thing , My operation above , Not written to file , We can see student.txt What's in the text :

[[email protected] tmp]# cat student.txt
ID Name Python Linux MySQL Java
1 Tangs 88 87 86 85.55
2 Sunwk 99 98 97 96.66
3 Zhubj 77 76 75 74.44
4 Shahs 66 65 64 63.33

You can see student.txt The contents of the text are still ,sed Commands are only output , Filter out the data .

If you need to delete the content at the same time , Write the file again , Need -i Options. .

Carry out orders :

[[email protected] tmp]# sed -i '2,4d' student.txt That's all right. .

(3) Append data to file

demand : stay student.txt Add... After the third line in the text 66666666666666.

Execute the order as follows :

[[email protected] tmp]# sed "3a 66666666666666" student.txt
ID Name Python Linux MySQL Java
1 Tangs 88 87 86 85.55
2 Sunwk 99 98 97 96.66
66666666666666
3 Zhubj 77 76 75 74.44
4 Shahs 66 65 64 63.33

Empathy ,student.txt The contents of the text have not been changed , If changes are needed student.txt What's in the text , add to -i Options .

Be careful :sed Command options should be written in single quotation marks ( Double quotes are OK ) outside , Such as :

[[email protected] tmp]# sed -i "3a 66666666666666" student.txt

(4) Insert data into a file

demand : stay student.txt Add... Before the third line in the text 88888888888.

Execute the order as follows :

[[email protected] tmp]# sed "3i 88888888888888" student.txt
ID Name Python Linux MySQL Java
1 Tangs 88 87 86 85.55
88888888888888
2 Sunwk 99 98 97 96.66
66666666666666
3 Zhubj 77 76 75 74.44
4 Shahs 66 65 64 63.33

Empathy , You need to write the changes to the file , Need to add -i Options .

(5) Modify multiple lines of data in the file ( Delete , Additional , Insert )

If you want to append or insert multiple rows of data , Except for the last line , Add... At the end of each line \ It means the data is not finished .

demand : towards student.txt Add... After the second line in the text hello world.

Execute the following command :

[[email protected] tmp]# sed '2a hello \
> world' student.txt
ID Name Python Linux MySQL Java
1 Tangs 88 87 86 85.55
hello
world
2 Sunwk 99 98 97 96.66
3 Zhubj 77 76 75 74.44
4 Shahs 66 65 64 63.33

( Quotation marks are not completely executed )

Tips : I found that after the line break tab The key prompt function is not available .( I don't know what's going on )

Here's an explanation -n Options ,-n Options will only pass through sed Command processing lines output to the screen .

Execute the following command :

[[email protected] tmp]# sed -n '2a hello \
world' student.txt
hello
world

(6) Replace the entire line of text in the file

demand : Replace student.txt The second line of data in the text is 999999999999999.

Execute the following command :

[[email protected] tmp]# sed '2c 999999999999999' student.txt
ID Name Python Linux MySQL Java
999999999999999
2 Sunwk 99 98 97 96.66
3 Zhubj 77 76 75 74.44
4 Shahs 66 65 64 63.33

Be careful :sed The command does not modify the contents of the file by default , If it is determined that sed The command directly handles the contents of the file , have access to -i Options . But be careful , This is very easy to misoperate , Be careful when operating system files .

(7) String substitution

stay sed in c The action is to replace the whole line , If you just want to replace some data in the line , Then use s Action .

demand : modify Zhubj Of Java The result is 100.

Execute the following command :

#  Command format 
[[email protected] tmp]# sed 's/ Old string / New string /g' file name # Carry out orders
[[email protected] tmp]# sed 's/74.44/100/g' student.txt
ID Name Python Linux MySQL Java
1 Tangs 88 87 86 85.55
2 Sunwk 99 98 97 96.66
3 Zhubj 77 76 75 100
4 Shahs 66 65 64 63.33 # Or it is more accurate to the line range
[[email protected] tmp]# sed '4s/74.44/100/g' student.txt
ID Name Python Linux MySQL Java
1 Tangs 88 87 86 85.55
2 Sunwk 99 98 97 96.66
3 Zhubj 77 76 75 100
4 Shahs 66 65 64 63.33

Be careful :

4s/74.44/100/g Expression s and / There must be no space between them .

If 4s/74.44/100/g No line number , That is, the first way to write it , It means to replace the matching string in the whole document .

(8) Perform multiple actions at the same time

stay sed In command ,-e Option can execute multiple at the same time sed action , Of course, if you just perform an action, you can also use -e Options , But at this moment -e Options are meaningless .

Also note the use of... Between multiple actions ; No. or enter split .

practice 1:

demand : hold Shahs Your grades are commented out , And the Zhubj Of Python Change the grade to 100.

Execute the order as follows :

#  Use ; Semicolons isolate multiple actions 
[[email protected] tmp]# sed -e '5s/^/#/g ; 4s/77/100/g' student.txt
ID Name Python Linux MySQL Java
1 Tangs 88 87 86 85.55
2 Sunwk 99 98 97 96.66
3 Zhubj 100 76 75 74.44
#4 Shahs 66 65 64 63.33

Be careful :

^ At the head of the line , It's a regular expression , Don't have to add -r Options can be .

In fact, the above command does not say -e Options , Commands can also be executed , It should be recognized by default .

But we try to standardize the writing .

#  Use carriage return to isolate multiple actions 
[[email protected] tmp]# sed -e '5s/^/#/g
> 4s/77/100/g' student.txt
ID Name Python Linux MySQL Java
1 Tangs 88 87 86 85.55
2 Sunwk 99 98 97 96.66
3 Zhubj 100 76 75 74.44
#4 Shahs 66 65 64 63.33

Be careful : Use carriage return to separate actions , You can't write semicolons ; 了 .

Particular attention :

stay sed When there are multiple actions in the command , When there are multiple options ,-e The option must be next to the action expression , Otherwise, an error will be reported .

for example :

-i -e '5s/^/#/g' correct ,

-e -i '5s/^/#/g' Report errors .

practice 2:

Delete string using sed Ordered d Operation cannot be realized , because d The operation is to delete the whole line . String replacement is required , That is to say sed Ordered s Act to complete .

demand : Delete Zhubj Of Python achievement .

Execute the following command :

#  Replace with empty 
[[email protected] tmp]# sed '4s/77//g' student.txt
ID Name Python Linux MySQL Java
1 Tangs 88 87 86 85.55
2 Sunwk 99 98 97 96.66
3 Zhubj 76 75 74.44
4 Shahs 66 65 64 63.33

3、 summary

When you need to use sed command ?

If you need it in a script program , Modify the data in the file , In this case, we need to use sed command .

『 Forget to learn again 』Shell Basics — 30、sed More about the use of commands

  1. 『 Forget to learn again 』Shell Basics — 10、Bash Special symbols in ( Two )

    Tips : This article is followed by an article , Mainly about () Parentheses and {} The difference and use of braces . 8.() parentheses (): Used when a series of commands are executed ,() The command in will be in the sub Shell Run in .( Together with the braces below ) 9.{} Curly braces {}: ...

  2. 『 Forget to learn again 』Shell Basics — 1、Shell Introduction to

    Catalog 1.Shell The origin of 2.Shell Two ways of executing instructions 3. What is? Shell Script 4.Shell Is a scripting language 1.Shell The origin of We are familiar with Windows Graphical interface of the system , For graphical interfaces ...

  3. 『 Forget to learn again 』Shell Basics — 4、Bash Basic function (history command )

    Catalog 1.history History commands 2. Set the number of command history records 3. Clear history command 4. Call of history command 5. Completion of commands and files stay Linux Default... In the system Shell Namely Bourne-AgainShell( abbreviation ...

  4. 『 Forget to learn again 』Shell Basics — 2、Shell Function and classification of

    Catalog 1.Shell The role of 2.Shell The classification of 1.Shell The role of Shell In addition to being able to interpret user input commands , Pass it to the kernel , just so so : Call other programs , Passing data or parameters to other programs , And get the processing result of the program . stay ...

  5. 『 Forget to learn again 』Shell Basics — 3、echo Introduction and use of command

    Catalog 1.echo Role of command 2.echo Basic usage of commands 3.echo Ordered -e Option usage 4.echo Some special uses of commands (1) The output character has a font color (2) The output character has a background color Talking about Shell Before the script , ...

  6. 『 Forget to learn again 』Shell Basics — 9、Bash Special symbols in ( One )

    Catalog 1. Double single quotation mark 2. Double quotes 3.$ Symbol 4. The quotation marks 5.$() Symbol 6.# Symbol 7.\ Symbol 1. Double single quotation mark '': Single quotation marks . All special symbols in single quotation marks , Such as $ and "`"( The quotation marks ) Nothing special ...

  7. 『 Forget to learn again 』Shell Basics — 11、 Rules and classification of variable definition

    Catalog 1. Rules for defining variables 2. Classification of variables 1. Rules for defining variables When you define variables , There are some rules to follow Variable names can be alphabetized . Numbers and underscores , But it can't start with a number . If the variable name is 2name It's wrong . stay Bash ...

  8. 『 Forget to learn again 』Shell Basics — 5、Bash Basic function ( Command aliases and common shortcuts )

    Catalog 1. Alias the command (1) Format the alias command (2) The command alias takes effect permanently (3) Alias priority 2.Bash Common shortcut key 1. Alias the command Linux The command alias of the system, as we have said before , Go over here . ...

  9. 『 Forget to learn again 』Shell Basics — 6、Bash Basic function ( I / O redirection )

    Catalog 1.Bash Standard input and output of 2. Output redirection (1) Standard output redirection (2) Standard error output redirection (3) Correct output and error output are saved at the same time 3. Input redirection 1.Bash Standard input and output of We've been talking about , stay Li ...

  10. 『 Forget to learn again 』Shell Basics — 8、 Introduction to pipeline symbols

    We've already written about pipe symbols before , Today, let's briefly summarize the usage . 1. Line extraction command grep grep Role of command , Is in the specified file , Search for qualified strings . Command format : [[email protected] ~ ] ...

Random recommendation

  1. sql in limit Usage method

    Here to mysql For example , But I believe that things are flexible in oracle It must also apply to Here are a few limit Methods : The principle should be understood by looking at the following examples It is used in many places in the database , For example, when you query tens of thousands of records in the database . Hundreds of thousands of hours l ...

  2. sdk Add new C File compilation error

    make: *** There are no rules to create "test.elf" The goal we need "mike.c.o". stop it . resolvent : Rebuild the project and compile the file

  3. be based on MVC4+EasyUI Of Web Experience summary of development framework (14)-- Automatically generate icon style file and icon selection operation

    In many Web In the system , In general, it is possible to provide some choices of icons , Convenient configuration button , Menu and other interface elements , From then on Web The system interface looks more beautiful and harmonious . But in the system, the built-in icon styles are relatively limited , And it's hard coded into the stylesheet , this ...

  4. Get random n Result lines

    * FROM [Menu] order by NEWID() * FROM [Menu]

  5. java Chinese sorting problem ( turn )

    stay Java in , For an array or list ( In this paper, they are collectively referred to as sets ) Element ordering in , It's a very common thing . Fortunately Sun The company in Java Most of the functions are implemented in the library . If the elements in the collection implement Comparable Interface , Call the following static (st ...

  6. c Language alignment problems

    introduction Consider the following structure definition : typedef struct{ char c1; short s; char c2; int i; }T_FOO; Suppose the members of this structure are compactly arranged in memory , And c1 The beginning of ...

  7. 【MySQL Reading notes 】 What are we doing when we execute the query statement

    I have seen a lot MySQL Related books and articles , I have never seen such an excellent column . So in the future, I will sort out what I have learned after reading this column . What are we doing when we execute the query statement mysql> select * fr ...

  8. ubuntu Next smokeping Installation configuration

    0. References http://wenku.baidu.com/view/950fbb0a79563c1ec5da71b1 http://aaaxiang000.blog.163.com/blog/sta ...

  9. Struts2 Relearn the custom interceptor ( Determine whether the user is logged in )

    Interceptor One :1: Concept :Interceptor Interceptors are similar to the filters we've learned , Yes, we can action Code executed before and after execution . yes web When developing , Common technology . such as , Access control , logging . 2: Multiple interceptors Interce ...

  10. Vue.js Image preview plugin

    vue-picture-preview-extend vue-picture-preview Extended version of , The plug-ins in this article are developed by other great gods , I made some extensions , Link to the original text :https://segmentfault. ...