In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
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: sends 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 1.sed command
There are usually two formats for invoking the sed command, as shown below. 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.
Sed [option] 'Operation' parameter
Sed [option]-f scriptfile parameter
Common sed command options include the following.
-e or-- expression=: means to process the input text file 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, 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. A: increase by 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.
Examples of usage:
Output qualified text (p means normal output) [root@localhost ~] # sed-n 'p' test.txt# outputs everything, which is equivalent to cat test.txt
[root@localhost ~] # sed-n '3p' test.txt # output line 3
[root@localhost ~] # sed-n '3jue 5p' test.txt # output 3 minutes 5 lines
[root@localhost ~] # sed-n'pleft n 'test.txt# outputs all odd lines, n means to read the next line of data
[root@localhost ~] # sed-n'nscape p 'test.txt# outputs all even lines, and n means to read the next line of data
[root@localhost ~] # sed-n'1 test.txt# output odd lines between lines 1-5 (lines 1, 3, 5)
[root@localhost ~] # sed-n'10 test.txt# output an even number of lines from line 10 to the end of the file
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.
[root@localhost ~] # sed-n'/ the/p' test.txt # output the line containing the
[root@localhost ~] # sed-n'4 the test.txt# output from line 4 to the first line containing the
[root@localhost ~] # sed-n'/ the/=' test.txt# outputs the line number of the line containing the, and the equal sign (=) is used to output the line number
[root@localhost ~] # sed-n'/ ^ PI/p' test.txt / / output lines that start with PI
[root@localhost ~] # sed-n'/ [0-9] $/ p 'test.txt # outputs lines ending with numbers
[root@localhost ~] # sed-n'/\ / p 'test.txt# output contains the line of the word wood, and\ represents the word boundary
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.
[root@localhost ~] # nl test.txt | sed '3d' # Delete line 3
[root@localhost ~] # nl test.txt | sed '3jie 5d' # Delete Line 3 and 5
[root@localhost ~] # nl test.txt | sed'/ the/d'// deletes the line containing the, and the original the line is deleted
[root@localhost ~] # sed'/ ^ [amurz] / d'test.txt # Delete lines that begin with lowercase letters
[root@localhost ~] # sed'/\. $/ d 'test.txt # delete to "." The line at the end
[root@localhost ~] # sed'/ ^ $/ d 'test.txt # Delete all blank lines
Note: if you delete duplicate blank lines, that is, only one consecutive blank line is left, you can do so by executing the command "sed-e'/ ^ $/ {n X / ^ $/ d} 'test.txt". The effect is the same as that of "cat-s test.txt". N means to read the next line of data.
Replace eligible text
When using the sed command to replace, you need to use the s (string substitution), c (whole line / block substitution), y (character conversion) command options, as shown below.
Sed's pedigree test.txt / / replace the first the in each line with THEsed's test.txt 2' test.txt / / replace the second l in each line with Lsed's the / / replace all the the in the file with THEsed's charger / / delete all o in the file (replace with empty string) sed's / ^ / # / 'test .txt / / insert the number # sed'/ the/s/ ^ / # / 'test.txt / / at the beginning of each line containing the and insert the number # sed's test.txt at the beginning of each line containing the / / insert the string EOFsed' 3rec 5s at the end of each line to replace all the in line 3 with THEsed'/ the/s/o/O/g' test.txt / / replace o with O in all lines containing the
Migrate eligible text
Among them, H, copy to the clipboard; g, G, overwrite / append the data in the clipboard to the specified line; w, save as a file; r, read the specified file; a, append the specified content.
Sed'/ the/ {Htterd}; $G'test.txt / / move the line containing the to the end of the file, {;} for multiple operations sed '1head 5 {Hintd}; 17G' test.txt / / sed'/ the/w out.file' test.txt / / save the line containing the as file out.filesed'/ the/r / etc/hostname' test.txt / / add the contents of the file / etc/hostname after transferring line 1 / 5 to line 17
/ / sed '3aNew' test.txt / / insert a new line after line 3 with the content of Newsed' / the/aNew' test.txt / / insert a new line after each line containing the, and insert multiple lines after line 3 with the content of Newsed '3aNew1\ nNew2' test.txt / / insert multiple lines after line 3, with\ n in the middle indicating line break
Edit a file using a script
Using the sed script, multiple editing instructions are stored in a file (one for each line) and called through the "- f" option. For example:
Sed'1 test.txt 5 {Hutchd}; 17G'G` / / after transferring the contents of line 1 to line 5 to line 17
The above operations can be changed to script files: [root@localhost ~] # vi opt.list 1 vi opt.list 5H1Magne5d17G [root@localhost ~] # sed-f opt.list test.txtThe year ahead will test our political establishment to the limit.PI=3.141592653589793238462643383249901429a wood crossroads actions speak louder than words#woood # # woooooood # AxyzxyzxyzxyzCI bet this place is really spooky late at night! Misfortunes never come alone/single.I shouldn't have lett so tast.he was short and fat.He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online.the tongue is boneless but it breaks bones.12! Google is the best tools for search keyword.
6) sed direct manipulation file example
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 ~] # vim local_only_ftp.sh #! / bin/bash# 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-e'/ ^ anonymous_enable/s/YES/NO/g' $SAMPLE > $CONFIGsed-I-e'/ ^ local_enable/s/NO/YES/g'-e'/ ^ 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 ~] # chmod + x local_only_ftp.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: 256
*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.