In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail the example analysis of sed multi-line in linux, Xiaobian thinks it is quite practical, so share it with you as a reference, I hope you can gain something after reading this article.
Under normal circumstances, sed reads the line to be processed into the pattern space, and the commands in the script process the line one by one until the script is executed, and then the line is output, and the pattern space is empty; then repeat the previous action, and a new line in the file is read until the file is processed completely. However, for various reasons, such as the user wanting a command in a script to be executed under certain conditions, or wanting pattern space to be reserved for next processing, it is possible that sed will not follow the normal flow when processing files. At this point, sed sets up some high-level commands to satisfy the user's requirements. If you want to learn the advanced commands of sed, you should first understand the following two buffers:
Definition of pattern space: Pattern space is a buffer that holds what sed has just read from the input.
2. Definition of temporary storage space: temporary storage space is used to temporarily cache data when processing pattern space data.
There are also several command parameters:
g: Copy the contents of hold space to pattern space, and clear the contents of original pattern space
G: Append the contents of hold space to pattern space\n
h: Copy the contents of pattern space to hold space, and the contents of the original hold space are cleared.
H: Append the contents of pattern space to hold space\n
x: Swap the contents of pattern space and hold space
For example, if we want to reverse the contents of a file, the file is as follows:
[qiu.li@l-tdata1.tkt.cn6 ~]$ cat tmp 1-line 2-line 3-line
Execute the following command:
[qiu.li@l-tdata1.tkt.cn6 ~]$ sed '2,$G;h;$! d' tmp 3-line 2-line 1-line
Below we gradually understand the above implementation process
Let us analyze the following three commands:
2,$G: Execute G from the second line to the last line
h: execute h command
$! d: Delete all rows except the last row
II. Specific operations
Scan to the first line
Put 1-line into pattern space; pattern space is still 1-line;
Directly execute the h command, at this time the temporary storage space is 1-line;
Execute d command to delete only one line of data in pattern space. After deletion, pattern space is empty.
Scan to the second line
Put 2-line into pattern space
Execute the G command to add the 1-line of the scratch space to the back of the 2-line of the pattern space, where the pattern space is 2-line\n1-line;
Execute the h command, and the contents of the temporary storage space are 2-line\n1-line;
Execute d command, pattern space is cleared
Scan to line three.
Put 3-line into pattern space,
Execute the G command to add the 2-line\n1-line of the scratch space to the back of the 3-line of the pattern space, where the pattern space is 3-line\n2-line\n1-line;
Execute the h command. At this time, the contents of the temporary storage space are 3-line\n2-line\n1-line;
Do not execute $! d;
Direct output 3-line\n2-line\n1-line
Of course, command: sed '1! G;h;$! D' tmp can also have this effect.
About "sed multi-line example analysis in linux" this article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.
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.