Linux Regular expressions

Source: Internet
Author: User
Tags clear screen control characters diff print format

Five scenarios for grep to search for strings

    1. Search for a specific string
    2. Use [] to search for characters. ^ in the character set symbol ([]) represents the inverse selection, which stands outside [] to locate at the beginning of the line
    3. Beginning and end of line character ^$
    4. Any one character. (with and only one, use the escape character \ Convert. to normal characters) with repeating characters * (0 or more)
    5. Qualifying consecutive repeating character ranges {}

Several situations in which character sets are used together

???? 1) [Range]???? Represents only one character to be searched

???? 2) [^range]???? Reverse selection, as opposed to [range]

???? 3) \{n\}???? Repeat the previous one with the same character n

???? 4) \{n,m\}???? Repeat the previous n-m of the same character

???? 5) \{n, \}???? Repeat the previous one with the same character n more than

?

In universal characters, * represents 0 to an infinite number of characters. In a regular expression, * means repeating the previous character 0 to an infinite number of meanings

?

printf ' Print format ' actual content

???? Parameter \a???????? Warning Sound output

???? Parameter \b???????? BACKSPACE (BACKSPACE)

???? Parameter \f???????? Clear Screen

???? Parameter \ n???????? Output a new line

???? Parameter \ r???????? Enter key

???? Parameter \ t???????? Horizontal Tab key

???? Parameter \v???????? The Vertical Tab key

???? Parameter \xnn???? NN is a two-digit number that can be converted into characters

?

????? The value returned from the previous execution command

$???? Represents the current shell's process number

?

▲ sed Tools

sed is a non-interactive text flow editor that must specify the row to be changed by a line number or regular expression.

sed read the data flow: sed read data from a line of text in a file or from a standard input, copy the data read to an edit buffer, and then read the command line or the first command of the script, and edit it using either the lookup mode or the anchor line number. Repeat this process until the command ends.

How to invoke sed:

    1. At the command line, type the command???? sed [options] sed command input file
    2. Insert the SED command into the script file and then call sed???????? sed [option]–f sed script file input file
    3. Insert the SED command into the script file and make the SED script execute???????? sed script file [options] input file

sed [-NEF] [action]

???? Parameter-n???????? All data from stdin is not printed. Only the line that has been specially processed by SED will be displayed.

???? Parameter-e???????? Action editing of SED directly in command line mode

???? Parameter-F???????? Directly write the SED action in a file, and-f filename to perform the SED action within the filename

???? Action Description???? [N1,[n2]]function

???????? N1,N2 does not necessarily exist, generally represents the number of rows selected for the action.

?

The function has the following:

???? A???? New. A can be followed by a string (appearing on the next line of the added row)

???? I???? Insert. I can be followed by a string (appearing on the previous line of the add line)

???? C???? Replace. C can be followed by a string (can replace the line between n1,n2)

???? D???? Delete. The back of D usually does not receive the content????

???? P???? Print. P is often used in conjunction with n

???? s???? Replace. can be directly substituted for work. Usually paired with regular expressions.

?

How to position text in a file using SED

X

X is a line number

X, y

Line numbers range from X to Y

/pattern/

Querying rows that contain patterns

/pattern/pattern/

Querying rows that contain two patterns

/pattern/,x or x,/pattern/

Query matching words by line number and pattern

x,y!

Query does not contain rows with the specified line number x and Y

?

SED edit command

P

Print matching lines

Q

Exit or exit immediately after the first pattern match is completed

=

show file line number

l

Display control characters equivalent to octal ASCII code

a\

{}

The command group executed on the anchor row

i\

n

Read the next line of text from another file and append it to the next line

D

delete anchor row

g

The matching line is replaced by a blank line

c\

Replace the anchor text with news book

y

transfer character

S

replace the corresponding mode with the replacement mode

n

R

read text from another file

g

add a blank line

W

Write text to a file

? ?

?

If you need to make multiple modifications to the same file or the same row, you have the following methods:

???? 1) Use the-e parameter???? Cat File | Sed ' s/old1/new1/' –e ' s/old2/new2 '

???? 2) use a semicolon;???????? Cat File | Sed ' s/old1/new1/;s/old2/new2 '

???? 3) Multi-line???????? Cat File | Sed '

???????????????? S/old1/new1

???????????????? S/old2/new2 '

?

Create an SED script???????? VI allsed.sed

?

▲ AWK Introduction

called awk three ways to

???? 1) command line mode ???? awk [-F field-separator] ' input-file

???? 2) write awk to the script and use the awk command Interpreter (#!/bin/awk-f) as the first line of the script, The settings script has the executable permission to invoke it by typing the script name

???? 3) Insert the awk command into a separate file and execute

Note: If you set the -f parameter, awk read one record or row at a time and split the specified field with the specified delimiter. But if the -f option is not set, awk Assume that the space is a field delimiter and keep this setting until a new row is found. When a new row appears, awk command learned to read the full record, and then the next record starts the Read command , the read process continues until the end of the file or the file no longer exists.

Note: awk statements are made up of patterns and actions. Omitting the mode section, the action remains in the execution state at all times. A pattern can be any conditional statement or compound statement or regular expression. The pattern consists of two special fields BEGIN and END. the begin statement is used before any text-browsing action, and then the text-browsing action is executed according to the input file. the END statement is used to Print relevant information after awk completes the text-browsing action. The actual action is indicated within the curly braces {} .

awk ' Begin{print ' This is the start ... "} {print $1,$2,$3,...} End{print "This is the end"} ' filename

?

Domain and record???? The field is marked as $1,$2 ... $n. $ A line information

Meta-character???????? \ ^ $ . [ ] | * + ?

+ ????????? Applies only to awk and not to grep or sed. + Match one or more characters.? match 0 or 1 characters

~???????? Matches a regular expression.!~ is a mismatched regular expression

=???????? equals.! = does not equal.

++ --???????? Prefixes and suffixes

?

awk built-in variables

ARGC???????? Number of command line arguments

Argv???????? Command line parameter arrangement

ENVIRON???? Support for the use of system environment variables in queues

FILENAME???? The file name that awk browses

FNS???????? Number of records to browse files

Fs???????? Set input field delimiter, equivalent to command line-f option

Nf???????? Number of fields to browse records

Nr???????? The number of records that have been read

OFS???????? Output field delimiter

ORS???????? Output record delimiter

Rs???????? Control record delimiter

?

Awk built-in string functions

Gsub (r,s)???? Replace R with S in the whole of $

Gsub (r,s,t)???? Replace R with S in the whole t

Index (S,T)???? Returns the first position of the string T in S

Length (s)???? Returns the length of S

Match (S,R)???? Tests if s contains a string matching R

Split (S,A,FS)???? Dividing s into sequence a on FS

Sub (r,s)???????? Replace s with the longest substring in the leftmost string

substr (s,p)???? Returns the suffix part of the string s starting from P

substr (s,p,n)???? Returns the suffix part of the string s from p starting at length n

Sprint (FMT,EXP)???? Returns the format of the FMT after exp

?

String masking sequence

\b Backspace key???? \ t Tab key???? \f Paper Change page

\DDD Octal value???????????? \ n New Line

\c any other special characters???????? \ r Enter

?

printf Modifiers and formatting

Modifier

???? -Align Left

???? The stride length of the width field

????. Prec maximum string length, or the number of digits to the right of the decimal point

Format

???? %c???? ASCII characters

???? %d or I???? Integer

???? %e or E???? Floating point number, scientific counting method

???? %f???? Floating point, with decimal point

???? %g???? awk determines which floating-point number to use for the conversion of E or F

????%o???? Octal number

???? %s???? String

???? %x???? Hexadecimal number

?

Awk built-in numeric functions

???? int (x)

???? sqrt (x)

???? EXP (x)

???? Sin (x)

???? RAND ()???????? Get a random number (between 0 and 1)

?

diff [-bbi] from-file To-file

???? Parameter-a???????? Treat all files as text files

???? Parameter-B???????? Ignore the differences caused by whitespace

???? Parameter-B???????? Ignore the differences caused by empty rows

???? Parameter-c???????? Using the outline output format

???? Parameter-I???????? Ignore Case changes

?

CMP [] file1 file2???? Diff is mainly based on "line", CMP is in "bit" units to compare

???? Parameter-s???????? Print nothing for differing files;return exit status only

Linux Regular expressions

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.