Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use sed command in linux system

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to use the sed command in the linux system. I hope you will get something after reading this article. Let's discuss it together.

Ed is a "non-interactive" character stream-oriented editor. Can handle the contents of multiple files and multiple lines at the same time, you can input the whole file to the screen without changing the original file, and you can input the content that only matches the pattern to the screen. You can also make changes to the original file, but the results are not returned on the screen.

The sed syntax sed command handles the contents of the schema space, not the contents of the file directly. If you add the parameter I, you can modify the file content directly. Example: sed-I's / original string / new string /'/ home/test.txtsed [- nefr parameter] [function] [filePath] option and parameter description-n use silent mode. In the normal use of sed, the input data is output to the screen. However, if you add the-n parameter, it will not show that if there is a flag followed by p, the line specially handled by sed will be listed-e directly edit the action of sed on the command line interface, execute multiple subcommands-f to write the action of sed in a file,-f filename execute the sed action of the script file-rsed action supports the syntax of extended regular expressions-I directly modify the contents of the read file

Option-n, which is set to quiet mode after adding the-n option, that is, the default print information will not be output, unless the print p option is specifically specified in the subcommand, only the matching modified lines will be printed.

-both lines are printed-server11:~/test # echo-e'hello\ nworld' | sed's HelloGetWorldMurray-not a single line is printed-server11:~/test # echo-e'hello\ nWhen world' | sed-n's Universe hellodebcscaccpany-printed a matching line-server11:~/test # echo-e'hello\ n world' | Sed-n 's/hello/csc/p'csc

Option-e, multiple subcommands operate continuously

Echo-e 'hello world' | sed-e's Universe HelloUniverse cscUnip'-e's Universe Universe LWL LWL'Result: csc lwl

Option-I to directly modify the contents of the read file

Server11:~/test # cat file.txthello worldserver11:~/test # sed's HelloGamp LWL server11:~/test 'file.txtlwl worldserver11:~/test # cat file.txthello world---- plus parameter I can directly modify the contents of the file-server11:~/test # sed-I' HELLOGUP 'LWL sed' file.txtlwl worldserver11:~/test # cat file.txtlwl world

Option-f, execute the file script

Sed.script script content: s/hello/csc/s/world/lwl/-echo "hello world" | sed-f sed.script result: csc lwl

Option-r, which only supports basic regular expressions by default. If you need to support extended regular expressions, you need to add-r.

Echo "hello world" | sed-r's / (hello) | (world) / csc/g'csc cscfunction expression: [N1 [, N2]] function or / {pattern} / function

N1, N2: optional, which generally stands for "number of rows selected for function processing". For example, if "function" needs to be done between 10 and 20 lines, it is expressed as 10 function 20 [function] if you need to match a string with a regular expression, you can use / {pattern} / match

Test.txt content 111222333444-Delete all lines other than line 2 and line 3-server11:~ # sed-I'2 cat test.txt222333- test.txtserver11:~ # cat test.txt222333- regular expression matching-server11:~ # echo 'clswcl.txt' | sed-nr' /. * / p'clswcl.txt / {pattern} / = /. * / function has the following options [/ yiji] function description an is added: a can be followed by a string And these strings will appear on the new line (the current next line) I insert: I can be connected after the string, and these strings will appear on the new line (the current previous line) c can be replaced by c after the string, these strings can replace the line d deletion between N1 and N2: because it is deleted, there is usually nothing p to print after d: also print some selected data. Usually p runs s substitution with the parameter sed-n: work that can be replaced directly! Usually the action of this s can be matched with a regular expression! For example: 1Jing 20 s/old/new/g

Function:-a, insert a new row after the row

Sed-I'/ specific string / a new line string 'fileName

Function:-i, insert a new row before the row

Sed-I'/ specific string / I new line string 'fileName

Function:-c, modify the specified content line

Sed-I'/ specific string / c csc lwl' fileName

Function:-d, delete a specific string

The sed-I'/ specific string / d'fileNamesed subcommand: s / {pattern} / {replacement} / {flags} {pattern} is a regular expression if there is a grouping in {pattern}, the "\ n" in {replacement} represents the nth grouping and the "&" represents the entire matching string. For details, see the parameters of the example flags as follows: flags description n can be 1-512, indicating that if the nth occurrence occurs, replace g global change p print mode space content w file is written to a file file

Example

Server11:~ # echo-e'hello 1112 world' | sed-r's / ([Amurz] +) ([0-9] +) ([Amurz] +) / & / 'hello 1112 worldserver11:~ # echo-e' hello 1112 world' | sed-r's / ([Amurz] +) ([0-9] +) ([aMuz] +) /\ 3\ 2\ 1/'world 1112 hello has finished reading this article I believe you have a certain understanding of "how to use sed commands in linux system". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report