Easy-to-learn Linux tutorial six regular expressions

Source: Internet
Author: User
Tags clear screen integer numbers print format

This series of articles by @ Superman Einstein produced, reproduced please indicate the source.  

        < Span style= "font-size:16px" > Superman Einstein     personal site http://www.hpw123.net  

article Links: http://hpw123.net/a/Linux/Linuxjichu/2014/1101/104.html

Email: [email protected]

CSDN: http://blog.csdn.net/u010283694


Regular expressions are the arrangement of some special characters to find, replace, delete some or more lines of text string, in short, the regular expression is used in the processing of the string "expression" is really convenient to use it, so study hard, really useful.


OK, let's get into the theme:


I. Regular expressions


1. What is a regular expression

A regular expression is basically a notation, and as long as the tool program supports this notation, the tool can be used as a string for regular expressions. Tools such as vi,grep,,awk,sed, as they have support for regular expressions


2. The effect of the family language on regular expressions

When using regular expressions, you need to pay special attention to the language of your environment, or you may find a different selection result than others.

Since we are generally practicing regular expressions, we use the POSIX compliant standard, so we use the "C" language, and in order to avoid the problem of the English and digital selection caused by such coding, there are some special symbols that we need to understand, as follows



3. Base regular expression character (characters)



4.grep,sed use

Grammar:


[[email protected] ~]# grep [-A] [-b] [--color=auto] ' search string ' filename option parameter:-A: The following can add a number, for after means, in addition to listing the row, the subsequent n rows are also listed; : The following can add a number, for the meaning of befer, in addition to listing the row, the preceding n rows are also listed;--color=auto can list the correct selection of the data color
[[Email protected] ~]# sed [-NEFR] [actuator] Option parameter:-N: Use Quiet (silent) mode. In the usage of general sed, all data from STDIN is generally listed on the screen. However, if you add the-n parameter, only the line (or action) that is specially processed by SED is listed. -E: Action editing of SED directly in command-line mode;-F: The action of SED is written in a file directly, and-f filename can execute the SED in filename; the-r:sed action supports the syntax of an extended regular expression. (the default is the underlying regular expression syntax)-I: Directly modifies the contents of the read file instead of being output by the screen.  Action Description: [N1[,n2]]function N1, N2: Not necessarily exist, generally represents the "select the number of lines of action", for example, if my action is required between 10 to 20 rows, then the "10,20[action behavior" function has the bottom of these drums: a : New, a can be followed by a string, and these strings will appear on a new line (the current next line) ~ C: Replace, C can be followed by strings, these strings can replace the line between N1,N2! D: Delete, because it is deleted ah, so d usually do not take any knock; I: Insert, I can be followed by strings, and these strings will appear on a new line (the current line), p: Print, that is, a selected data printed. Normally p works with parameter Sed-n ~ s: Replace, work that can be replaced directly! Usually this s action can be paired with regular expressions! For example 1,20s/old/new/g is!


Case:

Step one: First observation of the original information, using/sbin/ifconfig query IP? [[email protected] ~]#/sbin/ifconfig eth0 eth0 Link encap:ethernet HWaddr 00:90:cc:a6:34:84 inet addr : 192.168.1.100 bcast:192.168.1.255 mask:255.255.255.0 inet6 addr:fe80::290:ccff:fea6:3484/64 scope:link up BROADCAST RUNNING multicast mtu:1500 metric:1 ..... (omitted below) ..... Step two: Use keyword with grep to select a key row of data [[email protected] ~]#/sbin/ifconfig eth0 | grep ' inet addr ' inet addr:192.168.1.100 bcast:192.168.1.255 mask:255.255.255.0 # Only one line left on the spot! Next L come, we will start to addr: All delete, just like this: # inet addr:192.168.1.100 bcast:192.168.1.255 mask:255.255.255.0 # above the deletion is the key is "^.*inet ad DR: "Now! Regular expressions appear! ^_^ Step Three: Remove the previous part of IP [[email protected] ~]#/sbin/ifconfig eth0 | grep ' inet addr ' | \ > sed ' s/^.*addr://g ' 192.168.1.100 bcast:192.168.1.255 mask:255.255.255.0 # Careful comparison with the previous step, the front part is gone! The next step is to delete the following sections, namely: # 192.168.1.100 bcast:192.168.1.255 mask:255.255.255.0 # The regular expression you need at this point is: "bcast.*$" Is it! Step four: Remove the portion of the IP back [[email protected] ~]#/sbin/ifconfig eth0 | grep ' inet ADDR ' | \ > sed ' s/^.*addr://g ' | Sed ' s/bcast.*$//g ' 192.168.1.100# Another example of using SED to end each line in the Regular_express.txt. Then replace it!  [[email protected] ~]# sed-i ' s/\.$/\!/g ' Regular_express.txt # Top of the-I option allows your sed to directly modify the contents of the subsequent files instead of the screen output Oh!


5. Extending the regular expression



Two. File formatting and related processing

1. Format printing: printf

Grammar:


[[email protected] ~]# printf ' print format ' actual content option parameter: Several special styles about formatting: \a warning sound output \b backspace (backspace) \f clear screen (form feed) \ n output a new line \ R is the Enter button \ t horizontal [tab] key \v the vertical [tab] key \xnn NN is a two-digit number, you can convert the number into a character. In the C programming language, the common variable format%ns that n is a number, S is a string, that is, how many characters;%ni that n is a number, I represents an integer, that is, how many integer numbers;%n.nf that n and n are numbers, and F for float ing (floating point), if there is a small number, if I have a total of 10 digits, but there are two decimal places, that is%10.2f!
Case LIST:


List (printf.txt) content only name and score: ([tab] [[email protected] ~]# printf '%s\t%s\t%s\t%s\t%s\t \ n ' $ (cat printf.txt) name Chinese 中文版 Math Average dmtsai vbird 77.33 70.00 60 90 70 73.33
2.awk: Handy data processing tool

Awk is also a great data-processing tool, and awk tends to divide a line into several "fields" rather than sed, which is a good way to handle small data processing, and awk typically runs the same pattern.


[[email protected] ~]# awk ' condition type 1{action 1} condition type 2{action 2} ... ' filename
awk built-in variables


Case:


[Email protected] ~]# last-n 5| awk ' {print $ \ t lines: ' NR ' \ t columes: ' NF} ' root lines:1 columes:10root lines:2 columes:10 root lines:3 colume S:10 dmtsai lines:4 columes:10 root lines:5 columes:9 # Note that the NR, NF and other variables in awk are capitalized and do not need to be $!
the logical operator of awk (for conditional judgment )




—————————— Superman Einstein updated on November 01, 2014



OK, today's content is probably the case, if there is something wrong, ask the big God, we see in the next article:)

Oh, let's relax a bit. Positive Energy

more articles at: www.hpw123.net



Easy-to-learn Linux tutorials six 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.