In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What are the most commonly used Sed skills in Linux production environment, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, people who have this need can learn, I hope you can gain something.
The sed command is widely used, simple to use, and a tool for fast text processing. In fact, it does not have many skills, recitation, use is the most appropriate learning channel, belongs to hard skills. But it's complicated because there are so many advanced features. Xiaobian does not pay attention to the advanced functions of sed, but only explains some commonly used operations.
As you use it, you will find that some of the ideas of vim are similar, and the syntax of regular expressions is basically the same, and there is not much learning cost. From the perspective of personal vision and work efficiency, the sed command is an important tool that programmers must master.
A simple primer
As shown in the figure, a simple sed command has three main parts: parameters, scope, and action. The file to be manipulated 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 parameter, which is an advanced usage and will not be described too much.
Some of the example commands I will repeat many times, if you are smart, you will find patterns in them, sometimes without even explaining them.
parameters
-n This parameter means--quiet or--silent. Indicates ignoring the output of the execution process and simply outputting our results.
There is another parameter we often use: -i.
With this parameter, all changes will be performed on the original file. Your output will overwrite the original file. Very dangerous, be careful.
range
1, 4 means find the contents of lines 1, 2, 3, 4 in the file.
This range designation is very clever, see the following example (please replace the range part in the figure).
5 Select row 5.
2,5 Select 2 to 5 rows, 4 rows in total.
1 - 2 Select odd rows.
2 - 2 Select even rows.
2,+3 and 2, 5 have the same effect, 4 lines in total.
2,$from the second line to the end of the file.
Range selection can also use regular matching. See the following example.
/sys/,+3 Select the line where sys appears, and the next three lines.
/^sys/,/mem/Select the data between the line beginning with sys and the line where mem appears.
For the sake of intuition, the following commands correspond to the above description one by one, and there can be spaces between the scope and operation.
sed -n '5p' filesed -n '2,5 p' filesed -n '1~2 p' filesed -n '2~2 p' filesed -n '2,+3p' filesed -n '2,$ p' filesed -n '/sys/,+3 p' filesed -n '/^sys/,/mem/p' file
operation
The most common operation is p, which means printing. For example, the following two commands are equivalent:
cat filesed -n 'p' file
In addition to printing, there are the following operations, we are commonly used.
p Print matching content.
d Delete matching content. At this point, we have to remove the-n parameter. Think about why.
w Writes matches elsewhere.
A,I,C and other operations are basic but rarely used, so they are not introduced. We still have some orders to explain.
sed -n '2,5 p' filesed '2,5 d' filesed -n '2,5 w output.txt' file
Let's see what sed commands can do, and try them out.
Delete all lines beginning with #and blank lines.
sed -e 's/#.*// ' -e '/^$/ d' file
The most common, such as the following.
sed -n '2p' /etc/group
Represents printing the second line in a group file.
1. Parameters such as-n2. Mode such as '2p'3. Files such as/etc/group
So what if I want to execute multiple commands at once and don't want to write sed script files? Then you need to add the-e parameter.
The operation unit of sed is row.
replacement mode
These are common matching patterns for sed, but it also has a powerful substitution pattern, meaning that it finds and replaces certain values and outputs the result. The-n parameter is rarely used in substitution mode.
There are a lot of parameters in the replacement mode, but the first part and the fifth part can be omitted. The replacement will print out the entire text.
The first half is used to match some ranges, and the second half performs alternative actions.
range
This scope is syntactically similar to the scope above. Consider the following example.
/sys/,+3 Select the line where sys appears, and the next three lines.
/^sys/,/mem/Select the data between the line beginning with sys and the line where mem appears.
Specific orders are:
sed '/sys/,+3 s/a/b/g' filesed '/^sys/,/mem/s/a/b/g' file
command
The order here refers to s. That means substitute.
find matching
The lookup section finds the string to replace. This section accepts either pure strings or regular expressions. Consider the following example.
a Finds the string a in the range row.
[a,b,c] Find the string a or b or c in the range line.
Command is similar:
sed 's/a/b/g' filed's/[a,b,c]//g' file#This command is explained below
replaced
It's time to replace the string you found. The contents of this section replace those found in the Find Matches section.
Unfortunately, this part cannot be used regularly. The usual is exact replacement. Replace A with B.
But there are also advanced features. Similar to java or python's regular api, sed's replacement also has the meaning of Matched Pattern, and it can also get Group without further investigation. The most common replacement is &.
**&*, repeat. When used in a substitution string, it represents the original lookup match 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 each line of the file in quotes.
sed 's/.*/ "&"/' file
flag parameter
These parameters can be used singly or in multiple ways, but only the most common ones are described.
g by default matches only the first occurrence of the line, plus g, you can replace the full text. Often used.
p When the-n argument is used, p will output only the contents of the matching row.
W is similar to the w pattern above, but it outputs only lines with transitions.
i This parameter is more important, indicating that case is ignored.
e indicates that for each line to be output, execute one command. It is not recommended to use, you can use xargs to complete this function.
Look at the syntax of two commands:
sed -n 's/a/b/gipw output.txt' filesed 's/^/ls -la/e' file
fun
Due to regular relationships, many characters need to be escaped. You're going to do a lot of\\,\* stuff 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' filesed '/aaa/s@/etc@/usr@g' filesed '/aaa/s^/etc^/usr^g' filesed '/aaa/s|/etc|/usr|g' filesed '/aaa/s!/ etc!/ usr! g' file
Note: The first half of the scope is not available in this way. I'm used to using the symbol @.
Other regular expressions
As you can see, regular expressions are ubiquitous on the command line. The following is only a brief description.
^Head of line
$End of line
. single character
**** 0 or more matches
+ 1 or more matches
? 0 or 1 match
{m}Previous match repeated m times
{m,n} previous matches repeated m to n times
** Escape character
[0-9]Match any character in parentheses,or the role of
| or, or
\b Match a word. For example\lucky\b matches only the word lucky
parameter I
The parameter i has been briefly introduced above, and its purpose is to make the operation perform in the original file. No matter what you do, the original file will be overwritten. This is very dangerous.
By adding a parameter, you can make a backup of the original file.
sed -i.bak 's/a/b/' file
The above command takes effect on the original file and generates a file.bak file. It is strongly recommended that you specify the bak file with the i parameter.
show us
Let's take a look at the power of sed combined with other commands using two commands.
Output lines no less than 50 characters long
sed -n '/^. {50}/p'
Count how many times each word appears in the file
sed 's/ /\n/g' file | sort | uniq -c
Find py files in the directory and delete all line level comments
find ./ -name "*.py" | xargs sed -i.bak '/^[ ]*#/d'
Look at lines 5-7 and 10-13
sed -n -e '5,7p' -e '10,13p' file
Output IP address only
ip route show | sed -n '/src/p' | sed -e 's/ */ /g' |Linux is a free-to-use and freely distributed UNIX-like operating system, a POSIX-based multiuser, multitasking, multithreaded, and multiCPU operating system that runs major Unix tools, applications, and network protocols.
Did reading the above help you? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.
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.