Regular expression and document editor sed of Linux O & M Yunsheng notes

Overview of regular expressions

It uses a single character to describe and match a series of strings that conform to the rules of a statement. It is composed of ordinary characters and special characters. Regular expressions are widely used in script programming and text editor.
Regular expressions are abbreviated to regex, regexp, RE.
In most languages, regular expressions are included in two forward slashes "/"
Regular expressions have a feature of greedy matching: try to match the longest

Regular expression classification

  • Basic regular expression
    \Escape character. For example: "!" will be logical no! As a normal character, you can also break the line
    Example 1:
    [root@localhost ~]# ls -l /home \
    > /tmp
    //Example 2:
    [root@localhost ~]# grep "b.*in" a.txt 
    12 bin 34
    23 sb.*in 23
    44 binary 49
    23 binlog 23
    [root@localhost ~]# grep "b\.\*in" a.txt 
    23 sb.*in 23

    ^Matches the starting position of the string to For the first time
    The position at the end of the $match string to Ending
    ^$indicates a blank line, not a space

    [root@localhost ~]# cat a.txt 
    aa bb aa
    a b a
    [root@localhost ~]# grep "^aa$" a.txt 
    . match any string
     *Match face subexpression (character) 0 times or more
    [root@localhost ~]# grep "a 0*" a.txt 
    aa 0
    aa 00
    aa bb 0000 aa 00
    a b a 000
. * match any string

[list] matches a character in the list, for example: [abc], [a-z], [a-zA-Z0-9]
[root@localhost ~]# ifconfig ens32 | grep "TX [pe]"
        TX packets 1776  bytes 206946 (202.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
 [^ list] matches a character in any non list table, for example: [^ a-z], [^ 0-9], [^ a-z 0-9]
[root@localhost ~]# ifconfig ens32 | grep "TX [a-f]"
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@localhost ~]# ifconfig ens32 | grep "TX [^a-f]"
        TX packets 1814  bytes 210662 (205.7 KiB)
\{n \} matching face sub expression n times
[root@localhost ~]# grep "a 0\{2\}" a.txt 
aa 00
aa bb 0000 aa 00
a b a 000
\{n,\}      Face subexpression before matching at least n second
[root@localhost ~]# grep "a 0\{2,\}" a.txt 
aa 00
aa bb 0000 aa 00
a b a 000
\{n,m\}     Least face subexpressions before matching n Second largest m second
\<          Fixed word head
\>          Fixed ending 
[root@localhost ~]# grep "\<a\>" a.txt 
a b a 000
Escape character Significance
\a Bell (BEL)
\b Backspace (BS), move current position to previous column
\n Line feed (LF) moves the current position to the beginning of the next line
\r Enter (CR) moves the current position to the beginning of the line
\t Horizontal tabulation (HT) to next TAB
\v Vertical tabulation (VT)
\ Represents a backslash character "\"
  • Extended regular expression

    +Face subexpression 1 or more times before matching
    ? match face subexpression 0 or 1 times before
    () string in parentheses as a whole
    |Match characters in or

Common text processing tools in Linux

  • grep matching, querying (filtering)
  • sed edit (add, delete, modify)
  • awk text formatting (string extraction)

Matters needing attention

  • In Linux, regular processes files in behavioral units
  • alias grep='grep --color=auto'
  • Note character set, LANG=C

Regular expressions are fundamentally different from the wildcards we use on the command line.
Wildcards usually deal with filenames
ls -l .txt
: any string of any length, belonging to wildcard.
?: a single arbitrary string, belonging to wildcard.

Parameters of grep command:
-v exclude matching content (reverse)

-E support extended regular expression = egrep

[root@localhost ~]# grep -Ev "^$|#" /etc/httpd/conf/httpd.conf 
ServerRoot "/etc/httpd"
Listen 80
Include conf.modules.d/*.conf

-i ignore case

[root@localhost ~]# cat a.txt
[root@localhost ~]# grep -i "a" a.txt 
[root@localhost ~]# 

-o output only matching content

[root@localhost ~]# grep -o "root" /etc/passwd

– color=auto matching content display color (use less alias to view aliases)

-n display line number at the beginning of line

[root@localhost ~]# grep -n "root" /etc/passwd

-q matching content is not displayed (silent output, usually used when writing scripts but not output to the screen)

-w match filter conditions as words

[root@localhost ~]# grep -w "bin" /etc/passwd
[root@localhost ~]# grep "bin" /etc/passwd

-c output only matching row count

[root@localhost ~]# grep -c "root" /etc/passwd

Tags: Linux Programming less

Posted on Wed, 06 Nov 2019 04:11:13 -0500 by Jr0x