In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "Linux system sed command how to use", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's train of thought slowly in depth, together to study and learn "Linux system sed command how to use" it!
We know that Vim uses interactive text editing mode, and you can use keyboard commands to interactively insert, delete, or replace text in data. But the sed command to be discussed in this section is different. It uses stream editing mode. The most obvious feature is that before sed processes the data, it needs to provide a set of rules in advance, and sed will edit the data according to this rule.
Rules for using the sed command
The format of the command is as follows:
Sed [option] 'command' input_file
Option is optional, and the commonly used option is as follows:
-n uses silent mode, listing only the line (or action) that has been specially processed by sed;-e edits the action of sed directly on the instruction line mode;-f writes the action of sed directly in a file, and-f filename can execute the sed command in filename;-r lets the sed command support extended regular expressions (the default is the basic regular expression) -I directly modify the contents of the read file instead of being output by the screen
The commonly used commands are as follows:
A\: that is, the append appends the string, and the following characters can be added after the selection c\: replace / replace the string, and the subsequent content can be replaced with the selected content d: that is, delete delete, the command will delete the currently selected line I\: that is, insert insert the string, and the subsequent content can be inserted in front of the selection p: print will print This command prints the currently selected line to the screen s: replace, usually the use of the s command is like this: 1 the old string is replaced by the new string
Command example
Suppose you have a local file, test.txt, with the following contents:
[root@linuxprobe ~] $cat test.txtthis is first linethis is second linethis is third linethis is fourth linethis fifth linehappy everydayend
This section will use this file to demonstrate the use of each command in detail.
A command
[root@linuxprobe ~] $sed'1a\ add one' test.txtthis is first lineadd onethis is secondlinethis is third linethis is fourth linethis is fifth linehappy everydayend
The 1 in the command section of this example represents the first line, the same second line is written as 2, the first to the third line is written as 1Magol 3, and the last line is used to represent the last line, such as all the lines between the second line and the last line (including the second line and the last line).
The purpose of this example is to add the string "add one" after the first line, and you can see the effect from the output.
[root@linuxprobe ~] $sed'1 dint a\ add one' test.txtthis is first lineadd onethis is second lineadd onethis is third lineadd onethis is fourth lineadd onethis is fifth lineadd onehappy everydayadd oneendadd one
This example shows adding a "add one" string after all the lines on the first and last lines, and you can see the effect from the output.
[root@linuxprobe ~] $sed'/ first/a\ add one' test.txtthis is first lineadd onethis is secondlinethis is third linethis is fourth linethis is fifth linehappy everydayend
This example shows that the string "add one" is added after the line containing the "first" string. From the output, you can see that the first line contains first, so "add one" is added after the first line.
[root@linuxprobe ~] $sed'/ ^ ha.*day$/a\ add one' test.txtthis is first linethis is secondlinethis is third linethis is fourth linethis is fifth linehappy everydayadd oneend
This example uses regular expressions to match lines, and ^ ha.*day$ represents lines that start with ha and end with day, then match to the "happy everyday" of the file, so the "add one" string is added after the line.
I command
The I command uses the same method as the a command, except that the string is inserted before the matching line, so just replace a with I in the example of the a command above, and don't talk about it here.
C command
[root@linuxprobe ~] $sed'$c\ add one' test.txtthis is first linethis is secondlinethis is third linethis is fourth linethis is fifth linehappy everydayadd one
This example replaces the last line with the string "add one", and you can see the effect from the output.
[root@linuxprobe ~] $sed'4J / c\ add one' test.txtthis is first linethis is secondlinethis is third lineadd one
This example replaces the contents of the fourth to last lines with the string "add one".
[root@linuxprobe ~] $sed'/ ^ ha.*day$/c\ replace line' test.txtthis is first linethis is secondlinethis is third linethis is fourth linethis is fifth linereplace lineend
This example replaces the line that starts with ha and ends with day with "replace line".
D command
[root@linuxprobe ~] $sed'/ ^ ha.*day$/d' test.txtthis isfirst linethis issecond linethis isthird linethis isfourth linethis isfifth lineend
This example deletes lines that begin with ha and end with day.
[root@linuxprobe ~] $sed '4d' test.txtthisis first linethisis second linethisis third line
This example deletes the contents from the fourth line to the last line.
P command
[root@linuxprobe ~] $sed-n '4Jing p' test.txtthisis fourth linethisis fifth linehappy everydayend
This example prints the fourth line to the last line on the screen, and the p command is usually used with the-n option.
[root@linuxprobe ~] $sed-n'/ ^ ha.*day$/p' test.txthappy everyday
This example prints lines that start with ha and end with day.
S command
S-command is most commonly used in practical application.
[root@linuxprobe ~] $sed's Accord text test.txtthis isfirst textthis issecond textthis isthird textthis isfourth textthis isfifth texthappy everydayend
This example replaces all line in the file with text, and the last g means global, that is, global replacement. If g is not added, only the first line on the line will be replaced.
[root@linuxprobe ~] $sed'/ ^ ha.*day$/s/happy/very happy/g' test.txtthis isfirst linethis issecond linethis isthird linethis isfourth linethis isfifth linevery happy everydayend
This example first matches the line that starts with ha and ends with day. In this case, the matching line is "happy everyday", and then the happy in the line is replaced with very happy.
[root@linuxprobe ~] $sed's /\ (. *\) line$/\ 1Universe g 'test.txtthisis firstthisis secondthisis thirdthisis fourththisis fifthhappy everydayend
This example is a little complicated. Let's break it down first. First of all, the pattern of the s command is like s/old/new/g, so the old part of this example, that is, the content of the package is used in the (. *) line command to represent the part of the regular expression, and the sequence number is calculated from the beginning. in this example, there is only one so that represents the first part of the regular expression, which matches any string, so it matches any line that ends in line. Then replace the matching line with the first part of the regular expression (in this case, equivalent to deleting the line part), using\ 1 for the first part of the match, again\ 2 for the second part, and\ 3 for the third part, which can be referenced in turn. For example, the following example:
[root@linuxprobe ~] $sed's /\ (. *\) is\ (. *\) line/\ 1\ 2According to g 'test.txtthis firstthis secondthis thirdthis fourththis fifthhappy everydayend
The parts on both sides of the is in the regular expression can be represented by\ 1 and\ 2, and the purpose of this example is to delete the is in the middle part.
Thank you for your reading, the above is the content of "how to use sed commands in Linux system". After the study of this article, I believe you have a deeper understanding of how to use sed commands in Linux systems, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.