Shell Programming-Regular expressions

Source: Internet
Author: User

1. What is a regular expression

It is mainly used for string pattern segmentation, matching, finding and replacing operations.

2. Regular expressions and wildcard characters

A regular expression is used to match a qualifying string in a file, and the regular contains a match. Commands such as grep,awk,sed can support regular expressions.

Wildcards are used to match qualifying file names, and wildcard characters are exact matches. LS, FIND,CP These commands do not support regular expressions, so they can only be matched using the shell's own wildcard character.

3. Basic Regular Expressions

. Equivalent to a wildcard character?

[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}

[0-9]\{1,3\}\. [0-9]\{1,3\}\. [0-9]\{1,3\}\. [0-9]\{1,3\}\

3. Character Intercept commands

Cut [Options] file name

-F Column Number: Extract the first few columns

-D: Delimiter: Splits columns by the specified delimiter, the default delimiter is the TAB key (tab)

ID name Gender make
1 Nyan N 89
2 Luke L 87
3 Mak N 67

Cut-f 1,3 Student.txt

grep "/bin/bash"/etc/passwd | Grep-v "Root" | Cut-f 1-d ":"

printf ' output type output format ' output content

Output Type:

%ns: Output string. N is a numeric reference to output several characters

%ni: Output integer. n is the number of digits that the digit refers to

%M.NF: Output floating-point number. m and n refer to output integer and scale digits

printf does not support pipe breaks, so printf $ (cat 2.txt)

awk ' Condition 1{action 1} Condition 2{action 2} ... ' Filename

Conditional pattern:

General use of relational expressions as a condition: >,<>=,<=

Actions action:

Formatted output

Process Control Statements

awk ' {printf $ "\ t" $4 "\ n"} ' Student.txt

Begin:awk ' Begin{print "test"}{print $ "\ T" $4} ' Student.txt

FS built-in variable: cat/etc/passwd |grep/bin/bash |awk ' {fs= ': '}{print ' \ t ' $ $ '

If no begin is added, it is time to read the data assignment to the specified variable $n, and then tell FS to be ":" as the delimiter.

CAT/ETC/PASSWD |grep/bin/bash |awk ' begin{fs= ":"}{print "\ T" $ "

sed character substitution command

sed [options] ' [action] ' file name

-N: The general sed command will output all data to the screen, and if you join this option, only the lines processed by the SED command will be output to the screen.

-e: Allow multiple sed command edits to input data

-I: Use sed to modify the result to directly modify the data read file, instead of having the screen output.

Action:

A: Append, add one or more rows after the current line

C: Line substitution, replacing source data rows with a string following C

I: INSERT, insert one or more rows in the current line.

D: Delete the specified line

P: Print, output the specified line

S: string substitution, replacing another one by one strings with one string. The format is "line range s/old string/new string/g"

Sed-n ' 2p ' student.txt

SED ' 2a 3 f v m ' student.txt

Sed ' 2,4d ' student.txt

The preceding number is the line number

4. Character processing commands

Sort [Options] file name

-F: Ignore case

-N: Sort by numeric type, by default using string type sorting

-R: Reverse Sort

-T: Specifies the delimiter, which is a tab character by default

-K n[,m]: Sorts by the specified field range. Starting with the nth field, the M field ends (default to end of line)

Sort-n-T ":"-K "3,3"/etc/passwd

WC: Statistics Command

WC [option] File name

-L: Count rows only

-W: Counts only the number of words

-M: Count only characters

Shell Programming-Regular expressions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.