In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article focuses on "what are the Sed skills commonly used in Linux production environment". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what are the Sed skills commonly used in Linux production environment"?
A simple introduction
A simple sed command consists of three main parts: parameters, ranges, and operations. The file you want to manipulate can be hung directly at the end of the command line. In addition to the command line, sed can also specify a sed script with the-f argument, which is an advanced usage without too much description.
Some example commands I will repeat many times, smart as you must be able to find the rules, and sometimes do not even need to explain.
Parameters.
The parameter-n means-- quiet or-- silent. Indicates that the output of the execution process is ignored and only our results are output.
There is another parameter that we commonly use:-I.
After using this parameter, all changes will be performed on the original file. Your output will overwrite the original file. It's very dangerous. We must pay attention to it.
Range
1Jing 4 means to find the contents of the 4lines of 1pm 2pm 3pm in the file.
The designation of this range is very intelligent, please see the following example (please replace the scope part of the figure yourself).
5 Select line 5.
2. 5 Select 2 to 5 lines, a total of 4 lines.
1x2 Select odd rows.
2x2 Select even rows.
The effect of 2 and 5 is the same, a total of 4 lines.
2dint $from the second line to the end of the file.
Regular matching can also be used for range selection. See the following example.
/ sys/,+3 Select the line where the word sys appears, and the next three lines.
/\ ^ sys/,/mem/ selects the data between the line that starts with sys and the line that appears with the word mem.
For the sake of intuition, the following commands correspond to the above introduction one by one, and there can be spaces between the scope and the operation.
Sed-n'5p 'file sed-n' 2 sys/,+3 5p 'file sed-n' 1' 2p 'file sed-n' 2p 'file sed-n' 2p 'file sed-n' 2 file sed $p 'file sed-n' / sys/,+3 p 'file sed-n' / ^ sys/,/mem/p' file
Operation
The most common operation is p, which means printing. For example, the following two commands are equivalent:
Cat file sed-n 'p' file
In addition to printing, there are the following operations that we often use.
P prints the matching content.
D delete the matching content. At this point, the-n parameter is about to be removed, and think about why.
W writes the match somewhere else.
Although the operations such as arecom iPol c are basic, they are seldom used and are not introduced. We still come up with some orders to illustrate.
Sed-n'2 5 p 'file sed' 2 5 d 'file sed-n' 2 5 w output.txt' file
Let's take a look at what sed commands can do and try it with some commands.
Delete all lines starting with # and blank lines.
Sed-e's file. Hammer file'- e'/ ^ $
The most commonly used ones, such as the one below.
Sed-n '2p' / etc/group
Represents the second line in the printed group file.
1. Parameters such as-n
2. The mode part such as'2p'
3. Files, such as / etc/group
So what if I want to execute more than one command at a time and don't want to write a sed script file? Then you need to add the-e parameter.
The operation unit of sed is the line.
Replacement mode
The above is a common matching pattern for the sed command, but it also has a powerful replacement pattern, which means to find and replace some of the values and output the results. The-n parameter is rarely used in replacement mode.
The parameters of the replacement mode are a little too many, but the first and fifth parts can be omitted. The entire text will be output after replacement.
The first half is used to match some ranges, while the second half performs the action of replacement.
Range
This scope is similar to the scope syntax above. Look at the following example.
/ sys/,+3 Select the line where the word sys appears, and the next three lines.
/\ ^ sys/,/mem/ selects the data between the line that starts with sys and the line that appears with the word mem.
The specific commands are:
Sed'/ sys/,+3 sAccord a file sed bAccord g 'file sed' / ^ sys/,/mem/s/a/b/g' file
Command
The command here refers to s. That's what substitute means.
Find a match
The search section will find the string to be replaced. This part can accept either pure strings or regular expressions. Look at the following example.
A look for the string an in the line of scope.
Look for the string an or b or c from the scope line.
The command is similar to:
Sed's file sed a file#, we will explain the command below.
Replace
It's time to replace the string found. The content of this section replaces what is found in the find matching section.
Unfortunately, regularities cannot be used in this part. What is commonly used is precise replacement. For example, replace a with b.
But it also has advanced features. Similar to the regular api of java or python, the replacement of sed also has the meaning of Matched Pattern, and you can also get Group. The commonly used substitute is &.
& sign, repeat it again. When it is used in a replacement string, it represents the original search matching data.
[&] indicates that the found data is surrounded by [].
"&" indicates that the data you are looking for is surrounded by "".
The following command will enclose every line in the file in quotation marks.
Sed's Compact. File / "&" /'
Flag parameter
These parameters can be used individually or multiple, and only the most commonly used ones are described.
G by default only matches the content that appears for the first time in the line, plus g, you can replace it in full. Commonly used.
P when the-n argument is used, p will output only the contents of the matching line.
W is similar to the w pattern above, but it only outputs lines with transformations.
I this parameter is more important, which means that case is ignored.
E represents each line that will be output and executes a command. It is not recommended, you can use xargs to complete this function.
Look at the syntax of the two commands:
Sed-n 's/a/b/gipw output.txt' file sed's / ^ / ls-la/e' file
Interesting
Because of the regularity, many characters need to be escaped. You will do a lot of things like\\,\ * in the script. You can use | ^ @! Four characters to replace\.
For example, the following five commands are the same.
Sed'/ aaa/s/\ / etc/\ / usr/g' file sed'/ aaa/s@/etc@/usr@g' file sed'/ aaa/ s^ / ETC ^ / usr ^ g 'file sed' / aaa/s | / etc | / usr | g 'file sed' / AAA file
Note: the scope of the first half cannot be used in this way. I am used to using the symbol @.
Other
Regular expression
As you can see, regular expressions are everywhere on the command line. The following is a brief explanation.
^ the beginning of the line
$end of line
. Single character
* 0 or more matches
+ 1 or more matches
? 0 or 1 match
{m} previous matches are repeated m times
The previous match is repeated m to n times.
\ escape character
[0-9] matches any character in parentheses, the role of or
| | or, or |
\ b matches a word. For example,\ blucky\ b only matches the word lucky
Parameter I
The parameter I has been briefly introduced above, and its function is to let the operation be performed in the original file. No matter what you execute, the original file will be overwritten. This is very dangerous.
By adding a parameter, the original file can be backed up.
Sed-i.bak 's Universe a Universe b Universe 'file
The above command takes effect on the original file file and generates a file.bak file. It is strongly recommended to use the I parameter to specify the bak file at the same time.
Give a show.
Let's take a look at the combined power of sed and other commands with two commands.
Output lines with a length of not less than 50 characters
Sed-n'/ ^. {50} / p'
Count how many times each word appears in the file
Sed's / /\ nCompact g' file | sort | uniq-c
Find the py file in the directory and delete all line-level comments
Find. /-name "* .py" | xargs sed-i.bak'/ ^ [] * # / d'
View lines 5-7 and 10-13
Sed-n-e'5 7p'- e'10 13 p 'file
Output only ip addresses
Ip route show | sed-n'/ src/p' | sed-e's / * / / g' | cut-d'-f9 so far, I believe you have a better understanding of "what Sed techniques are commonly used in Linux production environment". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.