当前位置:网站首页>"Forget to learn again" shell Basics - 30. Use of SED command

"Forget to learn again" shell Basics - 30. Use of SED command

2022-06-13 11:38:00 Flourishing

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 .

原网站

版权声明
本文为[Flourishing]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206131121515590.html