#7 the regular expression of shell script programming

Source: Internet
Author: User
Tags diff expression engine line editor egrep

Multiple Command execution methods:

Script composition;

1.shengbang

2.#

3. Blank lines-lines with no content, only white space characters or tabs (tab)

4. Logical Judgment


Shell script Programming:

1. Execute permission to invoke the script through the path;

2. Direct implementation using the interpreter;


This article deals with tools:

Vim, VI, nano


Text Processing Three musketeers:

grep system:

grep, Egrep, Fgrep, Text Search tool, basic "pattern" for a given text fuzzy search, grep system is the default work in greedy mode;

Sed:stream Editor, stream data converter, line editor, text editing tools;


Awk:gawk,--gun awk, text Formatting tool, text Report generator, text processing programming language;


Cut, sort


grep system:

Grep

Egrep

Fgrep


Grep:global search Regular expression and print out of the line.

The global search is performed using regular expressions and the matching rows are displayed;

grep [OPTIONS] PATTERN [FILE ...]

PATTERN: Filter condition consists of regular expression meta-characters and text characters without special meanings;

Metacharacters of regular Expressions:

is interpreted as a special meaning by the regular expression engine;

Pcre (the most complete regular expression engine)--perl the regular expression engine of the language;


Basic Regular Expressions: Bre

Extended Regular expression: ere


grep: Only basic regular expressions are supported by default;

Egrep: Only extended regular expressions are supported by default;

Fgrep: The regular expression engine is not turned on by default;


Text characters:

Only those characters that have the meaning of the character's face;

Common options:

-I,--ignore-case: Ignores the case of text characters;

-V,--invert-match: inverse match; The result is a line that the pattern does not match successfully;

-C,--count: Count, statistics match all the lines of pattern;

-O,--only-matching: Turns off greedy mode, showing only what pattern can match;

-Q,--quiet,--silent: Quiet mode, do not output any matching results;

--color[=when],--colour[=when]: Highlight the contents of the matching pattern in a special color;

--color=auto

-E,--extended-regexp: extended regular expression, grep-e equivalent to Egrep

-F,--fixed-strings,--fixed-regexp:grep-f equivalent to Fgrep

-G,--basic-regexp: basic regular expression, egrep-g equivalent to grep

-P,--perl-regexp: Use Pcre (Perl common regular expression) engine;

-A num,--after-context=num: Displays the NUM line following the line that matches the pattern;

-B num,--before-context=num: Displays the NUM line in front of the line that matches the pattern;

-C num,-num,--context=num: Displays both front and back NUM lines at the same time as the line that matches the pattern is displayed;


Pattern

Regular expression meta-characters:

Basic regular Expression meta-characters:

Globbing--------A simplified version of the regular expression: []? *


Character Matching:

. : matches any single character;

[]: matches any single character within the specified range;

[^]: matches any single character that is unexpected in the specified range;

All of the following character sets can be placed in [] to match a single character;

[: Lower:]: denotes all lowercase letters;

[[: Lower:]]: Pass with any single lowercase letter;

[: Upper:]: denotes all uppercase letters;

[[: Upper:]]: Pass with any single capital letter;

[: Alpha:]: denotes all alphabetic characters;

[:d Igit:]: denotes all decimal digits; 10

[: Alnum:]: denotes all uppercase and lowercase letters and decimal digits;

[: Space:]: denotes white space characters;

[:p UNCT:]: denotes all punctuation marks;

[: Blank:]: Indicates a blank character;

[: Xdigit:]: all hexadecimal digits;

A-Z: all lowercase letters;

A-Z: all uppercase letters;

0-9: all decimal digits;


Number of matches: the character of the characters before the class can appear;

*: The preceding characters can appear any time (0 times, 1 times or more);

\?: The preceding character is optional (0 or 1 times);

\+: The preceding character appears at least once (1 or more times);

\{m\}: The preceding character must appear m times;

\{m,n\}: The preceding characters appear at least m times, at most n times; (m<n)

\{,n\}: The preceding characters appear at least 0 times, at most, n times;

\{m,\}: The characters in front of it appear at least m times, the more the more;


In a regular expression, the method of representing any character of any length:. *


Positional anchor characters:

Line anchoring:

Beginning of line anchoring: ^

End of line anchor: $

Word Anchor:

Head anchor:\< or \b

Tail anchor:\> or \b

\b: The previous version of the anchoring method, it is recommended not to use;


For the regular expression engine, the word is a practice string consisting of non-special characters;


Grouping and referencing characters:

\ (pattern\): treats all the characters that this pattern matches to as an integral whole;


In the regular expression engine value week, there are a series of built-in variables that hold all the character information within the group for the back reference; These variables are: \1,\2,\3, ...


Pattern1\ (pattern2\) pattern3\ (pattern4\ (pattern5\) \)


\1:pattern2

\2:pattern4

\3:pattern5


\1: The character that the pattern in the first set of parentheses matches to;

\2: The pattern in the second set of parentheses matches the character;

...


Please find the user account with the same UID and GID in/etc/passwd;

grep ' \ (\<[[:d igit:]]\+\>\). *\1 '/etc/passwd: Set and view password information for the user;


Or:

\|


Note: \| treats the strings of the left and right sides as a whole;


A\|american:a or American


Please find the integer value between 100-255 in the execution result of the ifconfig command;

First digit: 1 2

Second place: 0-9 0-4 5

Third place: 0-9 0-9 0-5


Ifconfig | grep ' \<1[0-9][0-9]\|2[0-4][0-9]\|25[0-5]\> '

Please find the integer value between 0-255 in the result of the ifconfig command execution.


grep [OPTIONS] [-E PATTERN |-f file] [FILE ...]


By default, only one pattern is allowed behind the grep command;

If you want to write more than one pattern during a grep search, you need to use the-e option: Each-e selection can use only one pattern as the parameter;


Write the required pattern into a file to ensure that there is only one pattern per line: We can use the-F file method to achieve multi-pattern matching;


Egrep

Egrep [OPTIONS] PATTERN [FILE ...]

Extended regular expression meta-characters:

Character Matching:

.

[]

[^]


Number of matches:

*

?

+

{m}

{M,n}

{m,}

{0,n}


Position Rivet:

^

$

\<,\b

\>,\b


Grouping and referencing:

()

\1,\2,\3, ...


Or:

|


All the characters in the Fgrep:pattern are treated as literal characters;


Other text-processing commands;

WC: Counts the number of rows, words, and bytes in a file;

WC [OPTION] ... [FILE] ...

WC [OPTION] ...--files0-from=f

-C: Only the number of bytes is displayed;

-L: Only the number of rows is displayed;

-W: Displays only the number of words;



Cut:remove sections from all line of files

Files that can be trimmed by the cut command are usually text documents with a certain structure or format;/etc/passwd

Cut OPTION ... [FILE] ...

-D,--delimiter=delim: Specifies the delimiter to be relied upon when the pruning operation is implemented, by default the whitespace character;

-F,--fields=list: Specifies the number of the field according to the defined delimiter;

How to use address delimitation:

#: Select a single field to be specified;

#,#: Discrete number of individual fields specified;

#-#: Multiple specified fields in succession;

--output-delimiter=string: Specifies the output symbol;


Awk:pattern scanning and processing language

Awk-f "delimiter" ' [/pattern/]{print $1,$2,... $NF} ' file ...

-F "delimiter": Specifies the field delimiter, which defaults to a blank character;

$1,$2,... $NF: The fragments of text cut from the field delimiter are stored in the corresponding internal variables;


Sort:sort lines of the text files, the default collation is the sequence of characters in the ASCII table, which is sorted by line,

This sorting criterion can be modified;

Sort [OPTION] ... [FILE] ...

Sort [OPTION] ...--files0-from=f

-R,--reverse: reverse order

-R,--random-sort: Random ordering, this stochastic algorithm is very primitive, not suitable for complex environment;

-U,--unique: Repeated rows, leaving only one row, (continuous and identical rows are called duplicates) to remove weight;

-N,--numeric-sort: The numeric value of the size of the order;

-T,--field-separator=sep: Specifies the field delimiter;

-K,--key=keydef: Indicates which key field to sort by, both general and-T are used simultaneously;

Uniq:report or omit repeated lines

Uniq [OPTION] ... [INPUT [OUTPUT]]

-D,--repeated: Displays only the rows that appear repeatedly, and each group of repeating rows displays only one row;

-U,--unique: Displays only rows that are not duplicates;

-C,--count: The number of repetitions of repeated rows is displayed in the prefix of each line;

Diff:compare Files line by line

diff [OPTION] ... FILES

Different modified versions of the same file: patching;

Patch:apply Changes to Files

Patch [-blnr][-c|-e|-n][-d dir][-d define][-i Patchfile]

[-O outfile] [-P num] [-R Rejectfile] [File]


#7 the regular expression of shell script programming

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.