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 > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces regular expressions and Linux what are the three major text processing tools, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian with you to understand.
Grep, sed and awk are all text processing tools. Although they are all text processing tools, they all have their own advantages and disadvantages. One text processing command cannot be completely replaced by another, otherwise there will not be three text processing commands.
Regular expressions
1. The type of matching character
[amurz]: lowercase letters
[Amurz]: uppercase letters
[aMuz]: lowercase or uppercase letters
[0-9]: number
[a-zA-Z0-9]: means to match a character that is a letter or a number
. Match 1 arbitrary character, except for spaces
[0murf]: hexadecimal number
Abc | def:abc or def
A (bc | de) f:abcf or adef
\: end of word
[^ form]: all characters except lowercase letters, and so on.
2. The number of matches controlled by the following symbols
The left side of this kind of symbol should have the expression of the first point above.
Expression *: 0 or n characters
Expression +: 1 or n characters
Expression?: 0 or 1 character
Expression {n}: n characters
Expression {nvir m}: n to m characters
Expression {n,}: at least n characters
[example] [amerz] * means to match 0 or more lowercase letters
3. Control the matching characters at the beginning and the end.
^ expression: header matches
Expression $: tail matches
2. The three major text processing tools of Linux
1. Egrep filtering tool
An extended version of grep that can use regular expressions
Syntax:
Egrep-option 'regular expression' file name
Options:
-n: displays the line number
-o: show only matching content
-Q: silent mode, without any output, $? To determine whether the execution was successful, that is, whether the desired content was filtered.
-l: if the match is successful, only the file name is printed, and if it fails, it is not printed. It is usually used together with-rl, grep-rl 'root' / etc.
-A: if the match is successful, print out the matching line and the n lines that follow it.
-B: if the match is successful, print out the matching line and its first n lines together
-C: if the match is successful, the matching line and its n lines before and after it are printed out.
-- color
-c: if the match is successful, the number of lines matched will be printed.
-I: ignore case
-v: inverted, mismatch
-w: match the word
2. Sed stream editor
Syntax:
Syntax 1:sed-option 'numeric location + command' file name
Options:
-n: silent mode, no output
-e: multiple editors, this is not very clear
-I: directly modify the contents of the file, not output
-r: extended mode, using regular expressions
-f: specify the file name and write the action in the new file
Command:
A ∶ appends append
C ∶ change change
D ∶ delete delete
I ∶ insert insert, I can be followed by strings, and these strings will appear on a new line (the current previous line)
P ∶ print print
S ∶ replaces substitute and can be replaced directly. Usually the action of this s can be matched with a regular expression. For example, 1BZ 20s Universe OldUnip newCompact g
The * s command specifically states:
Use {Command 1: command 2: command 3} to add multiple commands
Syntax 2:sed-r 'replace command s/ regular expression / replace content / greedy option g' filename
There are two ways to locate:
① digital positioning (enter line serial number positioning)
Decimal number
1: single Lin
1pr 3: from the first line to the third line
2 match lines 4: several lines after matching lines
4 # 3: a multiple line from the fourth line to the next 3
2x 3: lines at intervals of three lines from the second line
$: trailing line
1 except for the first line
Sed-n'1p'/ etc/passwd
② regular expression location
Regularities must be wrapped in / /
Extending regularity requires the use of the-r parameter or escape
Replace subpatterns that can use regular expressions, that is, parentheses (), which can represent subpatterns\ 1 and\ 2
[example] sed-r's / (.) (.) /\ 2\ 1 / file1 indicates the replacement of the first and second parts to be matched
* greedy option: enter g to replace all matches in a line
3. Awk text analysis tool
A combination of commands, regularities (which need to be surrounded by /), comparisons, and relational operations
Use the-F parameter in option to define the interval symbol
The order of $1 and so on is used to represent the different fields of each row in the files separated by an interval symbol, and the NF variable represents the number of fields in the current record.
Grammar
Awk-option parameter 'logical judgment {command variable 1, variable 2, variable 3}' file name
Option
-F defines the field delimiter, the default delimiter is consecutive spaces or tabs
-v defining variables and assigning values can also be introduced from shell variables on a secondary basis.
AWK variable
The number of current records in NR (statistics after all files are connected)
Number of current records in FNR (statistics of current files only, not all)
The FS field delimiter defaults to consecutive spaces or tabs, and you can use multiple different symbols as delimiters-F [: /]
The delimiter for OFS output characters is a space by default
# awk-F: 'OFS= "=" {print $1 recording 2}' / etc/passwd
Root=x
Number of fields currently read into the row by NF
The ORS output record delimiter is newline by default
# awk-F: 'ORS= "=" {print $1 recording 2}' / etc/passwd
Root x=bin x =
FILENAME current file name
[example 1] use AWK variable
# awk'{print NR,FNR,$1} 'file1 file2 1 1 aaaaa2 2 bbbbb3 3 ccccc4 1 dddddd5 2 eeeeee6 3 ffffff#
[example 2] method of referencing shell variable
# a=root# awk-v var=$a-F:'$1 = = var {print $0}'/ etc/passwd or take the whole command apart and pass it to expose the shell variable, # awk-F:'$1 = "'$a'" {print $0}'/ etc/passwd# a=NF# awk-F:'{print $'$a'}'/ etc/passwd
Logical operation (can directly refer to the domain for operation)
= + =-/ * = assignment
& & | |! Logic and logic or logic is not
~! ~ matching regular or mismatched, regular needs to be surrounded by / regular / enclosed
< >When comparing strings, enclose the strings in double quotation marks
$field reference: the field reference needs to be added with $, while the variable reference is directly named by the variable.
+-* /% +-- operator
Escape sequence
\\ itself
\ $escape $
\ t Tab character
\ b backspace character
\ r carriage return
\ nLine feed character
\ c cancel line wrapping
Thank you for reading this article carefully. I hope the article "what are the three major text processing tools of regular expressions and Linux" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.