Basic Linux operations 10 ----- regular expressions and file formatting

Source: Internet
Author: User
Regular expression 1: a regular expression is a string processing method. it processes strings in units of behavior. Regular expressions are assisted by special symbols, allows you to easily find, delete, and replace a specific string.

A regular expression

1. in simple terms, a regular expression is a method for processing strings. it processes strings in the unit of behavior. a regular expression can be easily searched by some special symbols, delete the handler that replaces a specific string

2 when grep looks for a string in the data, it selects the data in the unit of the whole action. that is to say, if there are 10 rows in a file, then two rows are useful, the two rows are displayed, and the other rows are discarded.

3 In bash, we know that the wildcard * can be used to represent any character, but the * in the regular expression represents different meanings.

In a regular expression, '.' indicates an arbitrary character.

In a regular expression, '*' indicates the meaning of repeating the previous character 0 to infinity multiple times.

4. basic regular expressions

RE character meaning

^ Search for the row whose first line is word

Word $ search for rows ending with word

. Indicates that there must be an arbitrary character.

\ Escape characters to remove the special meaning of special characters

* Repeat the previous character 0 to infinite

[List] select a character from the character set combination

[N1-n2] selecting a character from the range to be selected

[^ List] Finding a character from the combination of not this character set

\ {N, m \} is the first character from n to m in a row, \ {n \} is the first n consecutive characters, \ {n, \} is the first character connecting n to an infinite number

Two sed tools

1 sed is a pipeline command in this province. it can analyze and convert data into input, and sed can also replace, delete, add, and select a specific row.

2 sed usage

Sed [-nefr] [action]

-N uses quiet mode. in general mode, all data from stdin is usually listed on the screen. However, if the-n parameter is added, only the row that has been specially processed by sed will be listed.

-E directly edits sed actions in command mode

-F directly writes the sed action in a file, and-f filename can execute the sed action in filename.

-The action of r sed supports the syntax of extended regular expressions.

-I: directly modify the file content, instead of output by the screen.

3 Action description n1, n2 function

N1 and n2 do not necessarily exist. Generally, they represent the number of rows of the action.

Function has the following parameters:

A: New. a can be followed by strings. these strings will appear in a new row. The current next row

C: Replace. c can be followed by strings. these strings can replace rows between n1 and n2.

D: Delete. because it is delete, d is usually followed by no parameters.

I: insert, I can be followed by strings, and these strings will be in the new line, the current previous line

P: print the selected data.

S: replacement, which can be directly replaced

3. file formatting and related processing

1. Format and print

Printf 'print content' actual content

Common Variable formats in C language

The n in % ns is a number, and s represents a string.

% Nd the n is a number, and d represents an integer.

% N. nf the n and N are numbers, and f represent floating point numbers.

2. file comparison tool diff

Diff is used to compare the differences between two files and compare the behavior units. it is generally used to compare ASCLL plain text files.

Diff usage

Diff [-bBi] from_file to_file

-B ignores the difference between multiple blank spaces in a row.

-B: the difference between Ignore blank rows

-I: case insensitive

3. if we want to upgrade the old file to a new file, we can use diff to compare the differences between the files and make the files into patch files, update the old file by using the patch file.

In general, the comparison file created using diff usually has the. patch extension.

Update: patch-pN <patch_file

Restore: patch-R-pN <patch_file

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.