In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Shell script extension
A correct expression
The regular expression acts as a template that matches a character pattern with the searched string.
1. Supported commands:
Grep, vim, find, awk, sed, etc.
1 、. Represents any single character, such as: / l..e/ matches a line containing an l, followed by two characters, and then an e
Object that matches the s character in the wang.txt file.
2. ^ represents the beginning of the line. ^ love such as: matches all lines at the beginning of love
Retrieve the file that begins with N in the wang.txt file.
3. $represents the end of the line. Love$ such as: matches all lines at the end of love
Then'^ $'represents a blank line.
Wildcard character
Retrieves the matching lowercase letters a to z in the wang.txt file.
Note:
[abc] means to match a single character an or b or c
[123] means to match a single character 1 or 2 or 3
[Amurz] indicates a match between the capital letters Amurz
[^ a] denotes inversion and matches those with the lowercase letter a.
5. * is used to modify the leading character, indicating that the leading character appears 0 or any number of times
Retrieve 0 or more T occurrences in the wang.txt file
6.\? Used to modify the leading character, indicating that the leading character appears 0 or 1 times
7.\ + is used to modify the leading character, indicating that the leading character appears 1 or more times
8.\ {m\} is used to modify the leading character, indicating that the leading character appears n to m times (n and m are integers, and nn
Other forms:
N consecutive leading characters
At least n consecutive leading characters
Retrieve 2 to 4 n in the wang.txt file
9.\ is used to escape a single special character immediately following it, making it a normal character
The search wang.txt file contains. Of.
10. | indicates or such as: a | b | c matches an or b or c. Such as: grep | sed matches grep or sed
Retrieve the wang.txt file that contains an or b.
11. (), combine part of the content into a unit group. For example, to search for glad or good, you can use the following'g (la | oo) d'.
II. The usage of the grep command
Parameters:
1.-A NUM,--after-context=NUM lists not only matching lines, but also subsequent NUM lines.
List the Tom lines and the last two lines in the chai.txt file
2.-B NUM,--before-context=NUM is relative to-A NUM, but this parameter displays the NUM line that appears before it, in addition to matching lines.
List the Tom lines and the first two lines in the chai.txt file.
3.-C [NUM],-NUM,-- context [= NUM] lists the matching lines and lists the upper and lower NUM lines. The default value is 2.
List the Tom lines and the front and back lines in the chai.txt file.
4.-c,-- count does not display rows that match the style, only the total number of lines that match. If you add-v _ moment _ color _ match, the parameter shows the total number of lines that do not match.
List the number of lines in the chai.txt file that contain Tom
Lists the number of lines in the chai.txt file that do not contain Tom.
5. Ignore the difference between case and case.
Lists the lines in the chai.txt file that contain tom (case).
6. The line number is printed in front of the matching line
List the lines in the chai.txt file that contain tom (case) and print the line number.
7. The anti-search of-vjingjinghewashi match shows only the lines that do not match
Lists the lines in the chai.txt file that do not contain tom.
8. Exact match:
For example, when the string "48" is extracted, the return result contains other strings such as 484 and 483 that contain "48". In fact, the lines containing only 48 should be precisely extracted.
List exactly the lines in the chai.txt file that contain 30.
9.-s does not display error messages that do not exist or have no matching text
The xin.txt file does not exist, and the error message that cannot be searched is prompted when searching.
If you don't need an error message, add-s.
III. The usage of the sed command
1sed is an online editor that processes one line of content at a time. When processing, the currently processed line is stored in a temporary buffer, called "pattern space" (pattern space), and then the contents of the buffer are processed with the sed command, and when the processing is completed, the contents of the buffer are sent to the screen.
Send the contents of the buffer to the screen. Then process the next line, which is repeated until the end of the file.
3 the contents of the file have not changed unless you use redirect to store the output.
1. Replace: s command
1.1 basic usage
Replace The in chai.txt with chai and output to aaa.txt file.
Sed replace command
/.. / delimiter (can be replaced by other matches)
String searched by The
String replaced by chai
Files searched for
File to which aaa.txt exports
1.2 use & to represent a matching string
Sometimes you may want to add some characters around or near the matching string.
For example: sed 's/abc/ (abc) /' new
Sed only replaces the first occurrence of the search string by default, and all search strings can be replaced with / g.
If you need to make multiple changes to the same file or line, use the "- e" option
3. Delete lines: d command
Delete all lines containing "how" from a file
Display the passwd content and print the line number, while deleting 2 to 5 lines.
The nl command is used in linux systems to calculate the line number in a file. Nl can automatically add line numbers to the contents of the output file.
4. Add lines: a command (new after the specified line) or I command (new before the specified line)
A can be followed by strings, and these strings appear on a new line
Add the word "XXXXX" after the second line of / etc/passwd
The passwd content is displayed and the line number is printed, while adding hello after the second line.
The passwd content is displayed and the line number is printed, while adding hello before the second line.
If you want to add multiple lines at the same time, use a backslash\ between each line to add new lines.
5. Replace the line: C command
C can be followed by strings, which can replace the lines between N1 and N2
Substitution line
The passwd content is displayed and the line number is printed, replacing the content between lines 2 and 5.
6. Print: P command
Sed'/ north/p' datafile output all lines by default. Find the lines of north and print them repeatedly.
Only lines 5 through 7 in the passwd file are listed.
The-I option of sed can directly modify the contents of the file.
6. Extend:
There are three ways to call sed:
Type a command on the command line
Insert the sed command into the script file, and then call sed
Insert the sed command into the script file and make the sed script executable.
A. use sed command line format as follows:
Sed [options] sed command input file.
Remember that when you use the sed command, the actual command should be in single quotation marks. Sed also allows double quotation marks.
B. Use sed script files in the following format:
Sed [options]-f sed script file input file
C. To use the first line of the sed script file with the sed command interpreter, the format is:
Sed script file [options] input file
The first line is the sed command interpretation line. The script looks for sed on this line to run the command, which is located at / bin.
The second line starts with / company/, which is the starting position of the additional operation. A\ notify sed
Is an additional operation, two new rows should be inserted first.
The third and fourth lines are the actual text that the additional operation will add to the copy.
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.