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

Regular expression two of the three Musketeers sed tool

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

The second of the three Musketeers sed tool

Sed (Stream EDitor) is a powerful and simple text parsing conversion tool, which can read the text and edit the text content (delete, replace, add, move, etc.) according to the specified conditions, and finally output all lines or only some of the lines processed. Sed can also implement quite complex text processing operations without interaction, and it is widely used in Shell scripts to complete a variety of automatic processing tasks.

The workflow of sed mainly includes three processes: read, execute and display.

 read: sed reads a line from the input stream (file, pipe, standard input) and stores it to a temporary buffer

Punch zone (also known as pattern space, pattern space).

 execution: by default, all sed commands are executed sequentially in the mode space, and sed commands will be executed on all lines in turn unless the address of the line is specified.

 display: send the modified content to the output stream. After the data is sent again, the schema space will be emptied.

Before all the contents of the file are processed, the above process will be repeated until all the contents have been processed.

Note: by default, all sed commands are executed in the schema space, so the input file will not change unless you use redirection to store the output.

Common usage of sed command

There are two formats for invoking the sed command. Among them, "parameter" refers to the target file of the operation, which is used when there are multiple operating objects, and the files are separated by commas, while scriptfile represents the script file, which needs to be specified with the "- f" option. When the script file appears in front of the target file, it means that the input target file is processed through the specified script file.

Common sed command options include the following:

(1)  e or-expression=: means to process the input text file with a specified command or script.

(2)-f or-file=: means to process the input text file with the specified script file.

(3)-h or-help: show help.

(4)-n,-quiet, or silent: indicates that only the processed results are displayed.

(5)-I: edit the text file directly.

"Action" is used to specify the action behavior of the operation on the file, that is, the command of sed. The format of the "[N1 [, N2]]" operation parameter is usually used. N1 and N2 are optional and do not necessarily exist, which represent the number of rows selected for operation. If the operation needs to be carried out between 5 and 20 lines, it is expressed as "5jin20 action behavior". Common operations include the following.

(1) a: increase by adding a specified line below the current line.

(2) c: replace, replacing the selected line with the specified content.

(3) d: delete, delete the selected row.

(4) I: insert, inserting a specified line above the selected row.

(5) p: printing, if you specify a line at the same time, it means printing the specified line; if you do not specify a line, it means printing everything; if there are non-printing characters, it is output in ASCII code. It is usually used with the "- n" option.

(6) s: replace, replace the specified character.

(7) y: character conversion.

First of all, we prepare a test file [root@localhost ~] # vim chen.txtUse CDROM installation mediacdrom.thethethe.THETHEASDHAS Use graphical install.graphical.besttestASSDJFXYxyzCAxyzxyzxyzCkeyboard-- vckeymap=cn-- xlayouts='cn' System languagelang zh_CN.UTF-8 Network informationnetwork-- bootproto=dhcp-- device=ens33-- onboot=off-- ipv6=auto-- no-activatenetwork-- hostname=localhost.localdomain Root passwordrootpw-- iscrypted $6$ L.egxzosoP/0k9Nj$wna7vPXZjeH0jFcNZUymYKF8ySXq5HxQuvxTFxIpEAAxuDj7MQJtXBds5E0LxAftI1H5JbJuYpN44d5n6t1AZ. System servicesservices-- enabled= "chronyd" # System timezonetast (1) output qualified text (p means normal output) # version=DEVEL#System authorization informationauth-- enableshadow-- passalgo=sha512 Use CDROM installation mediacdrom.thethethe.THETHEASDHAS Use graphical install.graphical.besttestASSDJFXYxyzCAxyzxyzxyzCkeyboard-- vckeymap=cn-- xlayouts='cn' System languagelang zh_CN.UTF-8 Network informationnetwork-- bootproto=dhcp-- device=ens33-- onboot=off-- ipv6=auto-- no-activatenetwork-- hostname=localhost.localdomain Root passwordrootpw-- iscrypted $6$ L.egxzosoP/0k9Nj$wna7vPXZjeH0jFcNZUymYKF8ySXq5HxQuvxTFxIpEAAxuDj7MQJtXBds5E0LxAftI1H5JbJuYpN44d5n6t1AZ. System servicesservices-- enabled= "chronyd" System timezonetastbet Run the Setup Agent on first boot.firstboot-- enableignoredisk-- only-use=sdawoodwdwodwoooooooooddfsjdjoooooofFoofddd1241533234342222222faasd112ZASASDNAshortshirt (2) output line 3 [root@localhost ~] # sed-n '3p' chen.txtauth-- enableshadow-- passalgo=sha512 (3) output 3 ^ 5 lines [root@localhost ~] # sed-n' 3Lines 5p' chen.txtauth-- enableshadow-- passalgo=sha512#Use CDROM installation mediacdrom. (4) output all odd lines, n means read the next line of data P shows [root@localhost ~] # sed-n'p on the first line. N 'chen.txt#version=DEVELauth-- enableshadow-- passalgo=sha512cdrom.THE Use graphical install.bestASSDJFXYxyzCkeyboard-- vckeymap=cn-- xlayouts='cn'lang zh_CN.UTF-8 Network informationnetwork-- hostname=localhost.localdomain Root password System services System timezonebetfirstboot-- enablewoodwoddfsjdjoooooof1241533422222222short (5) outputs all even lines. N is not displayed on the first line, and starts directly from the second line [root@localhost ~] # sed-n. P 'chen.txt System authorization information Use CDROM installation mediathethethe.THEASDHASgraphical.testAxyzxyzxyzC#System languagenetwork-- bootproto=dhcp-- device=ens33-- onboot=off-- ipv6=auto-- no-activaterootpw-- iscrypted $6$ L.egxzosoP/0k9Nj$wna7vPXZjeH0jFcNZUymYKF8ySXq5HxQuvxTFxIpEAAxuDj7MQJtXBds5E0LxAftI1H5JbJuYpN44d5n6t1AZ.services-- enabled= "chronyd" tast Run the Setup Agent on first boot.ignoredisk-- only-use=sdawdwooooooooodFoofddd3234faasd11ZASASDNAshirt (6) outputs the even line from line 10 to the end of the file [root@localhost ~] # sed-n '10jue ${n P} 'chen.txtbestASSDJFXYxyzCkeyboard-vckeymap=cn-xlayouts='cn'lang zh_CN.UTF-8 Network informationnetwork-hostname=localhost.localdomain Root password# System services#System timezonebetfirstboot-enablewoodwoddfsjdjoooooof1241533422222222short

The first line read is the 10th line of the file when executing the "sed-nexp 10pm ${ntertp} 'test.txt" command

Line 2 read is line 11 of the file, and so on, so the even-numbered lines of the output are lines 11 and 13 of the file up to the end of the file, including blank lines.

These are the basic uses of the sed command. When the sed command is combined with a regular expression, the format is slightly different, which is surrounded by "/". For example, the following is an example of the sed command used in conjunction with a regular expression.

(7) output the line [root@localhost ~] # sed-n'/ the/p' chen.txtthethethe that contains the. Run the Setup Agent on first boot. (8) output from line 4 to the first line containing the [root@localhost ~] # sed-n'4 chen.txt Use CDROM installation mediacdrom.thethethe. (9) output the line number of the line containing the The equal sign (=) is used to output the line number [root@localhost ~] # sed-n'/ the/=' chen.txt630 (10) output the line beginning with fi [root@localhost ~] # sed-n'/ ^ fi/p' chen.txtfirstboot-- enable (11) output the line ending with a number [root@localhost ~] # sed-n'/ [0-9] $/ p 'chen.txtauth-- enableshadow-- passalgo=sha512lang zh_CN.UTF-81241533234342222222faasd112 (12) output the line containing the word wood Represents the word boundary [root@localhost ~] # sed-n'/\ / P 'chen.txtwood (II) Delete eligible text (d)

Because the later example also needs to use the test file test.txt, you need to back up the test file before performing the delete operation. The following examples demonstrate several common deletion uses of the sed command.

The nl command in the following command is used to calculate the number of lines in the file, and combined with this command, you can see the result of the command execution more intuitively.

(1) Delete line 3 [root@localhost ~] # nl chen.txt | sed '3d' 1 # version=DEVEL 2 # System authorization information 4 # Use CDROM installation media 5 cdrom. 6 thethethe. (2) Delete line 3-5 [root@localhost ~] # nl chen.txt | sed '3Magne5d' 1 # version=DEVEL 2 # System authorization information 6 thethethe. (3) / Delete the line containing cross, and the original line 47 is deleted / / delete the line that does not contain cross, use! The symbol denotes an inverse operation, such as'/ crossmap! D'[root@localhost ~] # nl chen.txt | sed '3authoring 5d' 45 short 46 shirt (4) Delete lines that begin with lowercase letters

[root@localhost ~] # sed'/ [^ amurz] / d'chen.txt

Besttestcrptastcrossbetwoodwdwodwoooooooooddfsjdjoooooofshortshirt (5) delete to "." The line at the end

[root@localhost ~] # sed'/\. $/ d 'chen.txt

(6) Delete all blank lines

[root@localhost ~] # sed'/ ^ $/ d 'chen.txt

Note: if you delete duplicate blank lines, that is, only one consecutive blank line is left, you can execute the command "sed-e'/ ^ KaTeX parse error: Expected group after'^'at position 6: / {nexter / ^ blank / d} 'test.txt". The effect is the same as "cat-s blank", where n means to read the next line of data.

(3) replace qualified text (1) use s (string substitution), c (whole line / block replacement), when using the sed command to replace. The y (character conversion) command option replaces the first the in each line with THE [root@localhost ~] # sed's passalgo=sha512 Use CDROM installation mediacdrom.THEthethe.THE 'chen.txt#version=DEVEL System authorization informationauth-- enableshadow-- passalgo=sha512 Use CDROM installation mediacdrom.THEthethe.THE (2) / replace the third l in each line with L [root@localhost ~] # sed's Universe Lhand' chen.txt#version=DEVEL System authorization informationauLth-- enableshadow-- passalgo=sha512 Use CDROM instaLlation mediacdLrom. (3) replace the file Replace all the in with THE [root@localhost ~] # sed's Universe the chen.txt#version=DEVEL System authorization informationaulth g 'chen.txt#version=DEVEL System authorization informationaulth-- enableshadow-- passalgo=sha512 Use CDROM installation mediacdlrom.THETHETHE.THETHEASDHAS (4) Delete all o in the file (replace with empty string)

[root@localhost ~] # sed's Universe GHG 'chen.txt

(5) insert the number # [root@localhost] # sed's / ^ / # / g 'chen.txt##version=DEVEL##System authorization information#aulth-- enableshadow-- passalgo=sha512##Use CDROM installation media#cdlrom.#thethethe.#THE#THEASDHAS##Use graphical install at the beginning of each line. (6) insert the string EOF [root@localhost ~] # sed's passalgo=sha512EOFse CDROM installation mediaEOFcdlrom.EOFthethethe.EOFTHEEOFTHEASDHASEOFUse graphical at the end of each line. Install.EOF (4) Migration of qualified texts

H, copy to the clipboard

G, G, overwrite / append the data from the clipboard to the specified row

W, save as a file

R, read the specified file

A, append the specified content.

(1) move the line containing the to the end of the file, {;} for multiple operations [root@localhost ~] # sed'/ the/ {Hintd}; $G' chen.txt#version=DEVEL System authorization informationaulth-- enableshadow-- passalgo=sha512 Use CDROM installation mediacdlrom.THETHEASDHAS (2) after moving the contents of line 1 to line 5 to line 17, [root@localhost ~] # sed'1, 5 {Hintd} 17G 'chen.txtthethethe.THETHEASDHAS Use graphical install.graphical.best (3) Save the line containing the as a file / opt/abc.txt [root@localhost ~] # sed' / the/w / opt/abc.txt' chen.txt [root@localhost ~] # cd / opt [root@localhost opt] # lsabc.txt rh [root@localhost opt] # cat abc.txtthethethe.#Run the Setup Agent on first boot. (4) add the contents of the file / etc/hostname to each containing the After line [root@localhost ~] # sed'/ the/r / etc/hostname' chen.txt Run the Setup Agent on first boot.localhost.localdomain (5) insert a new line after line 3 The content is New [root@localhost ~] # sed '3aNEW' chen.txt#version=DEVEL System authorization informationaulth-- enableshadow-- passalgo=sha512NEW (6) insert a new line after each line containing the, and the content is New [root@localhost ~] # sed'/ the/aNEW' chen.txt Run the Setup Agent on first boot.NEW (7) insert multiple lines after line 3 The middle\ n indicates a new line [root@localhost ~] # sed '3aNEW\ nNEW2\ nNEW3' chen.txtversion=DEVELSystem authorization informationaulth-- enableshadow-- passalgo=sha512NEWNEW2NEW3 (5) script edits the file root@localhost ~] # vim wq.composer Bingray Bash # specifies the sample file path and configuration file path SAMPLE= "/ usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE/vsftpd.conf" CONFIG= "/ etc/vsftpd/vsftpd.conf" # back up the original configuration file Check whether the file name / etc/vsftpd/vsftpd.conf.bak backup file exists, and if not, use the cp command to backup the file [!-e "$CONFIG.bak"] & & cp $CONFIG $CONFIG.bak# to adjust based on the sample configuration. Overwrite the existing file sed-eBay / ^ anonymous_enable/s/YES/NO/g' $SAMPLE > $CONFIGsed-I-eBay / ^ local_enable/s/NO/YES/g'-eBay / ^ write_enable/s/NO/YES/g' $CONFIG grep "listen" $CONFIG | | sed-I'$alisten=YES' $CONFIG# start the vsftpd service and set it to run systemctl restart vsftpdsystemctl enable vsftpd automatically after boot

[root@localhost ~] # chmod + x wq.sh

[root@localhost ~] #. / wq.sh

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

Servers

Wechat

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

12
Report