In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Sed tool
Sed is a powerful and simple text parsing conversion tool, which can read the text and edit the text content according to the specified conditions (delete, replace, add, move, etc.), 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.
Sed workflow
1. Read: sed reads a line from the input stream (file, pipe, standard input) and stores it in a temporary buffer.
two。 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.
3. Display: sends the modified content to the output stream. After the data is sent, the schema space will be emptied.
Sed command usage sed [option] 'Operation' parameter sed [option]-f scriptfile parameter common sed command option-e or-- expression=: indicates that the input text file is processed with a specified command or script. -f or-- file=: means to process the input text file with the specified script file. -h or-- help: displays help. -n,-- quiet, or silent: indicates that only the processed results are displayed. -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, representing the number of rows selected for operation
The common operation option a: increase, adding a specified line below the current line. C: replace, replacing the selected line with the specified content. D: delete, delete the selected row. I: insert, inserting a specified line above the selected row. P: print, if you specify a line at the same time, it prints the specified line; if you don't specify a line, it prints everything; if there are non-printed characters, it is output in ASCII code. It is usually used with the "- n" option. S: replace, replace the specified character. Y: character conversion. Usage demonstration
1. Output qualified text (p for normal output)
[root@localhost opt] # sed-n 'p' / etc/passwd / / output everything Equivalent to the use of cat-n' 5p'/ etc/passwd / / output the contents of line 5 sed-n'3 sed-n'p 5p / etc/passwd / / output 3 to 5 lines N'/ etc/passwd / / output the contents of all odd lines, n means to read the contents of the next line of data sed-n'nscape p' / etc/passwd / / output the contents of all even lines sed-n' 1 sed-n' 22 between the odd lines between lines 1 and 5 sed-n' 22 / output the even lines from line 22 to line bits
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 "/".
The sed command is used with regular expressions
[root@localhost opt] # sed-n'/ sbin/p' / etc/passwd / / output the line bin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin containing sbin. [root@localhost opt] # sed-n'2 / sbin/p' / etc/passwd / / output from line 2 to the first line containing sbin bin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologin [root@localhost opt] # sed-n'/ sbin/=' / etc/passwd / / output contains the line number where sbin is located The equal sign (=) is used to output the line number 2345678. [root@localhost opt] # sed-n'/ ^ ssh/p' / etc/passwd / / output the line beginning with ssh sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin [root@localhost opt] # sed-n'/ [0-9] $/ p 'test.txt / / output the line PI=3.141592653589793238462643383249901429 [root@] ending with a number Localhost opt] # sed-n'/\ / p 'test.txt / / output the line containing the word wood \ stands for word boundary a wood cross!
two。 Delete eligible text (d)
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.
Nl test.txt | sed '3d' / / Delete Line 3 nl test.txt | sed' 3Magazine 5d' / / Delete Line 3 / 5 nl test.txt | sed'/ cross/d' / / Delete the line containing cross sed'/ ^ [a test.txt] / / Delete the line beginning with a lowercase letter sed'/\. $/ d' test.txt / / Delete with "." The line at the end of the line sed'/ ^ $/ d 'test.txt / / deletes all blank lines [root@localhost opt] # nl test.txt | sed' 3 5 days 1 he was short and fat.2 He was wearing a blue polo shirt with black pants.6 The year ahead will test our political establishment to the limit. 7 PI=3.141592653589793238462643383249901429 Note: if you delete duplicate blank lines, that is, only one consecutive blank line is left, it can be achieved by executing the command "sed-e'/ ^ $/ {n X / ^ $/ d} 'test.txt". The effect is the same as "cat-s test.txt", where n means to read the next row of data.
3. Replace eligible text
When replacing with the sed command, you need to use the s (string substitution), c (whole line / block substitution), y (character conversion) command options
Sed's pedigree test.txt 'test.txt / / replace the first the in each line with THEsed's Placement LAccord 3' test.txt / / replace the third l in each line with Lsed's Placement the test.txt / / replace all the the in the file with THEsed's test.txt / / the file. Delete (replace with an empty string) sed's / ^ / # / 'test.txt / / insert the number # sed' / the/s/ ^ / # / 'test.txt / / at the beginning of each line containing the THE/g' test.txt / / replace all the in line 3-5 with THEsed'/ the/s/o/O/g' test.txt / / replace o with O in all lines containing the
4. Migrate eligible text
H copies to the clipboard; g G overwrites / appends the data from the clipboard to the specified line; w saves as a file; r reads the specified file; a, appends the specified content. Sed'/ the/ {Htterd}; $G'test.txt / / move the line containing the to the end of the file, {;} for multiple operations sed '1jue 5 {Hutterd} 17G 'test.txt / / after transferring line 1' 5 to line 17, sed'/ the/w out.file' test.txt / / Save the line containing the as a file out.filesed'/ the/r / etc/hostname' test.txt / / add the contents of the file / etc/hostname to each line containing the sed '3aNew' test.txt / / insert a new line after line 3 The content is New sed'/ the/aNew' test.txt / / insert a new line after each line containing the, the content is New sed '3aNew1\ nNew2' test.txt / / insert multiple lines after line 3, and the middle\ n indicates a line break
5. Edit a file using a script
When using the sed script, you can store multiple editing instructions in a file (one editing instruction per line) and call it through the "- f" option.
Sed'1 vim try.list1,6H1,6d18G 6 {Hutterd}; 18G 'test.txt / / after transferring line 1 / 6 to line 18 [root@localhost opt] # vim try.list1,6H1,6d18G [root@localhost opt] # sed-f try.list test.txt
6.sed directly manipulates files
Write a script to adjust the vsftpd service configuration: anonymous users are prohibited, but local users are allowed (and writes are also allowed).
[root@localhost opt] # vim aaa.shangxambinbash # specify 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 and check whether the backup file named / etc/vsftpd/vsftpd.conf.bak exists If it does not exist, use the cp command to backup the file [!-e "$CONFIG.bak"] & & cp $CONFIG $CONFIG.bak # 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 [root@localhost opt] # chmod + x aaa.sh [root@localhost opt] #. / aaa.sh automatically after boot.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.