In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Today, I would like to talk to you about the experience of using Shell sed, many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Sed is a lightweight stream editor on the UNIX/LINUX platform, which is generally used to process text files on a daily basis. Sed has many good features. First of all, it is quite small; second, sed can perform many complex functions with powerful shell. In my opinion, sed can be seen as a script interpreter, doing a lot of things in a programming-like way.
Simple example of sed
The format of the sed command is
Sed [- options] [command] [stdin]
For example, this example
$sed-e 'd'. / Mydir/Myfile
Execute this command and you will not get any output. In this case, sed;sed is called with an editing command'd'to open. / Mydir/Myfile reads a line into its pattern buffer and executes'd'("delete lines"); then prints the pattern buffer (the buffer is empty); it repeats these steps for each subsequent line, which does not produce output!
A few things to say about this command are: first of all, it doesn't modify. / Mydir/Myfile at all. This is because sed is a stream processor, it only reads the file specified on the command line, and it does not attempt to modify the file. Second, sed is line-oriented, and the'd' command does not simply tell sed to delete all input data at once; instead, sed reads each line of. / Mydir/Myfile line by line into its internal buffer called the pattern buffer, and once it reads a line into the pattern buffer, it executes the'd 'command and then prints the contents of the pattern buffer. The last thing to note is to enclose the use of single quotation marks for the'd 'command, which disables the shell extension.
Specify the scope of sed
Look at the following three examples
$sed-e '1d'. / Mydir/Myfile
$sed-e'1m 10d'. / Mydir/Myfile
$sed-e'/ begin/,/end/p'. / Mydir/Myfile
The first example shows that only the buffer of the first line of. / Mydir/Myfile is deleted, and the second example removes the buffer of lines 1-10. The third example is the most complex, defining the scope that begins with the string 'begin'' and ends with 'end''.
Regular expressions in sed
Sed supports regular expressions, such as
$sed-n-e'/ regexp/p'. / Mydir/Myfile
For example, to delete all blank lines
$sed-e'/ ^ $/ d'. / Mydir/Myfile
Some special definitions in sed are
Regular expression description
/. / will match any line containing at least one character
/. / will match any line that contains at least two characters
/ ^ # / will match any line that starts with'#'
/ ^ $/ will match all blank lines
/} ^ / will match any line that ends with'}'(no spaces)
/} * ^ / will match any line followed by'} 'followed by zero or more spaces
/ [abc] / will match any line containing lowercase'a','b'or'c'
/ ^ [abc] / will match any line that starts with'a','b'or'c'
Another classic example is to print the contents of the main function in the c source code, and the command can be written as follows
$sed-n-e'/ main [[: space]] * (/, / ^} / p'. / Mydir/*.c
Where [: space] represents spaces, and [[: space]] * indicates that there are 0 ~ multiple spaces, so main [[: space]] * (means to match "main (" string; "^}" means that the line has one and only one character}. Of course, the above orders are not very strict.
Sed-e's file regexp
Substitution is the most commonly used command in sed commands, such as the following example
$sed-e's _
$sed-e's _ Mydir/Myfile _
The first command replaces the character a that appears for the first time on each line in. / Mydir/Myfile with the character b; the second command replaces the character an into the character b by adding'/ g', indicating that the character an is a global replacement.
You can often see the following two forms of sed
$sed-e '1gr 10s Universe b'. / Mydir/Myfile
$sed-e'/ ^ $/, / ^ END/s/a/b/g'. / Mydir/Myfile
The first command is to replace lines 1 through 10; the second command is to globally replace the contents of lines starting with a blank line and starting with END.
Replacement commands do not have to be separated by /, such as
$sed-e's VRUSERGUR _ Mydir/Myfile
Is to replace all usr/local in. / Mydir/Myfile with / usr
To make good use of replacement commands, of course, use regular expressions. In addition to the examples of expressions mentioned above, there are also some that are very useful.
Character class description
[: alnum:] alphanumeric [amurz Amurz 0-9]
[: alpha:] letter [amurz Amurz]
[: blank:] spaces or tabs
[: cntrl:] any control character
[: digit:] numbers [0-9]
[: graph:] any visual character (no spaces)
[: lower:] lowercase [amurz]
[: print:] non-control character
[: punct:] punctuation character
[: space:] Spac
[: upper:] uppercase [Amurz]
[: xdigit:] hexadecimal digits [0-9 amurf Amurf]
For example, the following example
$sed-e's /] * > / / g'. / Mydir/Myfile
< [^>] * > matches such a string (… Does not include >). By running this command, you can set "This is what I meant." Replace such a string with "This is what I meant."
Examine the following commands
$sed-e's _ Mydir/Myfile.
This command comments out all non-blank lines with "#"-& tells sed to insert characters at the beginning of the line.
'sswap 'allows us to define regions in regular expressions, which can then be referenced in the replacement string. These areas are separated by (and).
For example, if you define three areas that match non-empty characters, you can now define the actions of these three areas, such as
$sed-e's / (. *) / PreFix 1-2 Fix 3Compact'. / Mydir/Myfile
If the three regions are named a, b, c respectively, the final result is PreFix a murb Fix c
Combining commands in sed
Commands in sed can be combined and separated by; signs, such as
$sed-n-e'=; p'. / Mydir/Myfile
= for print line number and p for print. For more complex instructions, you can write a command script and import it with the-f option, such as
$sed-n-f MyScript.sed. / Mydir/Myfile
For operations at the same address, you can combine them with {}, such as
$sed-n '1pm 20 {s/samba/Samba/g s/posix/POSIX/g}'. / Mydir/Myfile
Line additional commands in sed
Insert row "I" before the current row, or separate multiple lines if you want to insert multiple rows
Insert row "a" after the current line, using similar to the above
Change the current line "c"
After reading the above, do you have any further understanding of what you have learned from using Shell sed? If you want to know more knowledge or related content, please follow 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.