In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
1. Brief introduction to Sed
Sed is an online editor that processes one line of content at a time. When processing, the currently processed line is stored in a temporary buffer, called "pattern space" (pattern space), and then the contents of the buffer are processed with the sed command, and when the processing is completed, the contents of the buffer are sent to the screen. Then process the next line, which is repeated until the end of the file. The contents of the file have not changed unless you use redirect to store the output. Sed is mainly used to automatically edit one or more files, simplify the repeated operation of files, write conversion programs, and so on. The following is the Gnu version of Sed 3.02.
two。 Address
You can locate the lines you want to edit by addressing them. The address is made up of numbers, and the number of two lines separated by commas indicates the range of lines starting and ending with these two behaviors (including the two lines represented by the number of lines). For example, 1pr 3 means 1pr 2jue 3, and the dollar sign ($) represents the last line. The range can be determined by data, regular expressions, or a combination of both.
3. Sed command
There are two forms of invoking the sed command:
*
Sed [options] 'command' file (s)
*
Sed [options]-f scriptfile file (s)
A\
Add a line of text after the current line.
B lable
Branch to the marked place in the script, or to the end of the script if the branch does not exist.
C\
Change the text of this line with the new text.
D
Delete rows from the Pattern space location.
D
Delete the first line of the template.
I\
Inserts text above the current line.
H
Copy the contents of the mold plate to the buffer in memory.
H
Append the contents of the template block to the buffer in memory
G
Gets the contents of the memory buffer and replaces the text in the current module plate.
G
Gets the contents of the memory buffer and appends it to the text of the current module block.
L
A list cannot print a list of characters.
N
Read the next input line and process the new line with the next command instead of the first command.
N
Append the next input line to the template block and embed a new line between the two to change the current line number.
P
Print the lines of the template plate.
P (uppercase)
Print the first line of the template plate.
Q
Exit Sed.
R file
Read lines from file.
T label
The if branch, starting on the last line, will result in a branch to the labeled command, or to the end of the script, once the condition is met or the TMaget command is met.
T label
The error branch, starting at the last line, will result in a branch to the labeled command or to the end of the script in the event of an error or the TMaget command.
W file
Write and append the template to the end of the file.
W file
Write and append the first line of the template block to the end of file.
!
Indicates that subsequent commands have an effect on all rows that have not been selected.
S/re/string
Replace the regular expression re with string.
=
Print the current line number.
#
Extends the comment before the next newline character.
The following is the replacement tag
*
G means full in-line replacement.
*
P represents a print line.
*
W means to write a line to a file.
*
X represents the text in the interchange mode plate and the text in the buffer.
*
Y means to translate one character into another (but not for regular expressions)
4. Option
-e command,-- expression=command
Multiple editors are allowed.
-h,-- help
Print the help and display the address of the bug list.
-n,-- quiet,-- silent
Cancel the default output.
-f,-- filer=script-file
Boot sed script file name.
-V-- version
Print version and copyright information.
5. Metacharacter set ^
Anchoring the beginning of a line such as: / ^ sed/ matches all lines that begin with sed.
$
The end of an anchor line such as: / sed$/ matches all lines that end in sed.
.
Matches a non-newline character such as: / s.d/ matches s followed by any character followed by d.
*
Match zero or more characters such as: / * sed/ matches all lines where the template is one or more spaces followed by sed.
[]
Matches a specified range of characters, such as / [Ss] ed/ matches sed and Sed.
[^]
Matches a character that is not in the specified range, such as: / [^ A-RT-Z] ed/ matches the beginning of a letter that does not contain Amurr and Tmurz, followed by a line of ed.
\ (..\)
Save matching characters, such as s /\ (love\) able/\ 1rsrecoverable is replaced with lovers.
&
Save search characters to replace other characters, such as * * love**, save search characters, save search characters and replace other characters, such as * * love.
\
< 锚定单词的开始,如:/\ 锚定单词的结束,如/love\>/ matches lines that contain words ending in love.
X\ {m\}
Repeat the character xQuery m times, for example: / o\ {5\} / match a line containing 5 os.
X\ {m,\}
Repeat the character x at least m times, such as: / o\ {5,\} / matches a line with at least 5 o.
X\ {mdirection n\}
Repeat the character x at least m times and no more than n times, for example: / o\ {5pm 10\} / lines that match 5Murray 10 o.
6. Example
Delete: d command
*
$sed '2d' example- deletes the second line of the example file.
*
$sed'2 example- deletes all lines from the second line to the end of the example file.
*
$sed'$d' example- deletes the last line of the example file.
*
$sed'/ test/'d example- deletes all lines in the example file that contain test.
Replace: s command
*
$sed's test TestUniGO example- replaces test with mytest across the entire line. If there is no g tag, only the first matching test in each line is replaced with mytest.
*
The $sed-n's / ^ test / mytest/p' example- (- n) option, used with the p flag, means that only those lines that are replaced are printed. That is, if the test at the beginning of a line is replaced with mytest, print it.
*
The $sed's / ^ 192.168.0.1 / & localhost/' example-& symbol indicates the part of the replacement string that was found. All lines starting with 192.168.0.1 are replaced with their own localhost and become 192.168.0.1localhost.
*
$sed-n's /\ (love\) able/\ 1rs example-love is marked as 1, all loveable will be replaced with lovers, and the replacement line will be printed.
*
No matter what character, the s command followed by the s command is considered to be the new delimiter, so "#" is the delimiter here instead of the default "/" separator. $sed's delimiter 10 characters 100% g 'character is considered the new delimiter after the s command. Means to replace all 10 with 100.
Range of selected lines: comma
*
$sed-n'/ test/,/check/p' example- all lines within the range determined by the templates test and check are printed.
*
$sed-n'5J / ^ test/p' example- prints all lines from the fifth line to the first line that contains the line starting with test.
*
$sed'/ test/,/check/s/$/sed test/' example- for the lines between template test and west, the end of each line is replaced with the string sed test.
Multipoint editing: e command
*
The $sed-e '1cr 5d'-e's example- option allows multiple commands to be executed on the same line. As shown in the example, the first command deletes lines 1 to 5, and the second command replaces test with check. The order in which the command is executed affects the result. If both commands are replacement commands, the first replacement command affects the result of the second replacement command.
*
$sed-- expression='s/test/check/'-- expression='/love/d' example- A better command than-e is-- expression. It assigns values to sed expressions.
Read from file: r command
*
The contents of $sed'/ test/r file' example-file are read in and displayed after the lines that match the test, and if there are multiple lines, the contents of the file are displayed below all the matching lines.
Write to file: W command
*
$sed-n'/ test/w file' example- all lines in example that contain test are written to file.
Append command: a command
*
$sed'/ ^ test/a\\-- > this is an example' example'-> this is an example' is appended to the line that begins with test, and sed requires a backslash after the command a.
Insert: I command
$sed'/ test/i\\
New line
-'example
If the test is matched, the text after the backslash is inserted in front of the matching line.
Next: n command
*
$sed'/ test/ {n; s aa BB example- if the aa is matched, move to the next line of the matching line, replace the aa of this line, change to bb, print the line, and then continue.
Morph: y command
*
$sed '1Magne10yCharachaABCDE example- converts all abcde in 1Murray 10 lines to uppercase. Note that regular expression metacharacters cannot use this command.
Exit: Q command
*
After $sed '10q' example- prints line 10, exit sed.
Keep and get: h command and G command
*
$sed-e'/ test/h'-e'$G example- when sed processes a file, each line is saved in a temporary buffer called pattern space, and unless the line is deleted or the output is cancelled, all processed lines will be printed on the screen. The pattern space is then emptied and stored on a new line to be processed. In this example, when a row that matches test is found, it is stored in the pattern space, and the h command copies it and stores it in a special buffer called the hold cache. The second statement means that when the last line is reached, the G command takes out the line that holds the buffer, puts it back in the pattern space, and appends it to the end of the line that already exists in the pattern space. In this case, it is appended to the last line. Simply put, any line that contains test is copied and appended to the end of the file.
Keep and swap: h command and x command
*
$sed-e'/ test/h'-e'/ check/x' example-swaps the mode space and holds the contents of the buffer. That is, swap lines that contain test and check.
7. Script
The Sed script is a list of sed commands that boot the script file name with the-f option when Sed is started. Sed is very picky about the commands entered in the script. There can be no white space or text at the end of the command, and if there are multiple commands on one line, separate them with semicolons. Behaviors that begin with # comment lines and cannot span lines.
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.