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 > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about how to use sed, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
By default, Sed does not modify the source file directly, but copies it through pipes or file reads to a buffer called the pattern space, where the instructions are completed and printed to the screen.
Syntax format:
It is mentioned above that the positioning is at the end of the line, so the line temporarily defines it as the address, that is, the operation object.
The range of addresses has the following centralized format
N # specify a unique line number
1'2 # specifies to start at line 1 with an interval of 2, that is,
$# specify the last line
/ regular expression / # matches the Operand of the regular expression
\ a regular expression a # matches the regular expression between\ an and a
3 # 5 # match 3-5 lines
1 match 10 # 1 and 10 lines after 1
Options for using sed
Sed [options] [script instructions] [input file]
Option:-n # silent output, does not print the contents of the mode space, and automatically prints by default
-e script # allows multiple script instructions to be executed
-f # read instructions from a file
-the line length of the l # output
-r # extended regular expression
-I # modify the source file directly
Instruction: a (append) # append
D (delete) # Delete
S (substitution) # replace
C # change
Y # replace by character
P # print
Q # exit
I # insert
L # print (display non-print characters)
L # print (display print characters)
R # read the contents of the file
Let's find a file and manipulate it.
Example: append "This is an example" after the first line
[root@pan-S~] # sed "1a This is an example" / opt/fstab_Mrpan
Example: add "This isa example" before line 6 # Note that the source file has changed
[root@pan-S~] # sed-I "6i This is an example" / opt/fstab_Mrpan
Example: replace command
[root@pan-S~] # sed "s/defaults/Mr.pan/g" / opt/fstab_Mrpan
[root@pan-S~] # sed-I "3s/fstab/fstab_Mr.pan/g" / opt/fstab_Mrpan
Example: change command
[root@pan-S~] # sed "/ pan/c exit" / etc/passwd | more
Example: using regular expressions
Match the line units that contain fstab
[root@pan-S~] # sed-I "/ fstab/a This is an example" / opt/fstab_Mrpan
[root@pan-S~] # sed-n "/ ^ $/ d" / opt/fstab_Mrpan
You can use-e-f or a delimiter semicolon when you need to execute multiple instructions; you can also use a branch, but it's best to use the-f file so that it's easy to modify and not error-prone.
Example: delete the line containing example and replace all zeros with 1
(1) # sed-e "/ example/d"-e "s swap 0UniUnix g" / opt/fstab_Mrpan
(2) # sed "/ example/d;s/0/1/g" / opt/fstab_Mrpan
All right, you can operate it yourself and see what's changed.
The above is how to use sed, the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.