Linux tutorial 6: detailed explanation of regular expressions and linux Regular Expressions

Source: Internet
Author: User

Linux tutorial 6: detailed explanation of regular expressions and linux Regular Expressions

This series of articles is produced by @ Superman Einstein. Please indicate the source for reprinting.

Author: Superman Einstein's personal website:Http://www.hpw123.net 

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

Email: 424346976@qq.com

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


Regular Expressions are the arrangement of some special characters for searching, replacing, and deleting some or multiple lines of text strings. In short, A regular expression is an "expression" used to process strings. It is really convenient to use it. So study it and it is really useful.


Now, let's go to the topic:


I. Regular Expression


1. What is a regular expression?

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


2. Influence of the language family on Regular Expressions

When using regular expressions, pay special attention to the language families of the environment at that time. Otherwise, you may find the selection results different from those of others.

Since we generally use POSIX-compatible standards when practicing regular expressions, we use the "C" language. In addition, to avoid the problem of selecting English and numbers caused by such encoding, we need to understand some special symbols, as shown below:



3. characters)



4. Use grep and sed

Syntax:


[Root @ www ~] # Grep [-A] [-B] [-- color = auto] 'search string 'filename' option parameter:-A: numbers can be added to the end, meaning after, in addition to listing this row, the next n rows are also listed;-B: numbers can be added to the end, which means befer. In addition to listing this row, the previous n rows are also listed; -- color = auto: you can list the colors of the correct selected data.
[Root @ www ~] # Sed [-nefr] [Quiet] Option parameter:-n: Quiet Mode. In general sed usage, all data from STDIN is usually listed on the screen. However, if the-n parameter is added, only the row (or action) that has been specially processed by sed will be listed. -E: directly edit the sed action in the Command column mode.-f: directly write the sed action in a file, -f filename can be used for sed encoding in filename.-r: sed supports the syntax of extended regular expressions. (The default is the basic regular expression syntax)-I: directly modify the content of the Read File, rather than the screen output. Action Description: [n1 [, n2] function n1, n2: does not necessarily exist. Generally, it indicates "number of rows selected for action". For example, if my actions need to be performed between 10 and 20 rows, then the "10, 20 [Action Behavior]" function has the following Doon: a: new, and a can be followed by a string, these strings will appear in the new line (the current next line )~ C: replace. c strings can be followed by strings. These strings can replace rows between n1 and n2! D: delete, because it is delete, so d is usually followed by no comment; I: insert, I can be followed by a string, these strings will appear in the new line (the previous line currently); p: print, that is, print the selected data. Usually p will work with the sed-n parameter ~ S: replacement. You can directly replace it! Generally, this s action can be combined with a regular expression! For example, 1, 20 s/old/new/g!


Case:

Step 1: first observe the original information and query the IP address using/sbin/ifconfig? [Root @ www ~] #/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 2: Use keywords with grep to select a key row of data [root @ www ~] #/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 is left on the spot! Next, we will delete the service from addr: Tongtong, as shown below: # inet addr: 192.168.1.100 Bcast: 192.168.1.255 Mask: 255.255.255.0 # The Key to the above deletion is "^. * inet addr! Regular Expression appears! Pai_^ Step 3: delete the previous part of the IP Address [root @ www ~] #/Sbin/ifconfig eth0 | grep 'inet addr '| \> sed's/^. * addr: // G' 192.168.1.100 Bcast: 192.168.1.255 Mask: 255.255.255.255.0 # compare it with the previous step. The previous part is missing! The next step is to delete the subsequent part, namely: #192.168.1.100 Bcast: 192.168.1.255 Mask: 255.255.255.0 # the required regular expression is "Bcast. * $! Step 4: delete the part after the IP Address [root @ www ~] #/Sbin/ifconfig eth0 | grep 'inet addr '| \> sed's/^. * addr: // G' | sed's/Bcast. * $ // G' 192.168.1.100 # Another example uses sed to end each line in regular_express.txt. switch! [Root @ www ~] # Sed-I's/\. $ /\! /G'regular_express.txt # The-I option in the header allows your sed to directly modify the content of the subsequent file instead of the screen output!


5. Extended Regular Expression



Ii. File formatting and related processing

1. Format and print: printf

Syntax:


[Root @ www ~] # Printf 'print format' actual content option parameter: special formats: \ a warning sound output \ B backspace \ f form feed) \ n output a new line \ r, that is, the Enter key \ t horizontal [tab] key \ v vertical [tab] key \ xNN is a two-digit number, it can be converted into characters. In C programming language, the common variable format % ns that n is a number, s represents string, that is, how many characters; % ni that n is a number, I represents integer, that is, the number of integer words; % N. the n and N values of nf are numbers. f indicates floating (floating point). If there are decimal words, if I want a total of ten digits, but there are two decimal places, that is, % 10.2f!
Case column:


Only names and scores are listed in (printf.txt): (separated by [tab]) [root @ www ~] # Printf '% s \ t \ n' $ (cat printf.txt) name Chinese English Math Average DmTsai 80 60 92 77.33 VBird 75 55 80 70.00 Ken 60 90 70 73.33
2. awk: An easy-to-use data processing tool

Awk is also a great data processing tool. Compared with sed, awk often acts on a whole row. awk tends to divide a row into several "fields" for processing. Therefore, awk is quite suitable for processing small data processing. The normal running mode of awk is like this.


[Root @ www ~] # Awk 'condition type 1 {Action 1} Condition Type 2 {Action 2}... 'filename
Awk built-in Variables


Case:


[Root @ www ~] # Last-n 5 | awk '{print $1 "\ t lines:" NR "\ t columes:" NF} 'root lines: 1 columes: 10 root lines: 2 columes: 10 root lines: 3 columes: 10 dmtsai lines: 4 columes: 10 root lines: 5 columes: 9 # note that NR in awk, variables such as NF must be capitalized and $ is not required!
The logic operator of the awk (used for condition judgment )




---------- Superman Einstein updated on August 1, November 01, 2014



OK. This is probably the content of today. If there is anything wrong with it, please correct it. For details, refer to the following article :)

Haha, let's take it easy.Positive energy

More articles: www.hpw123.net




How can I learn Regular Expressions in Linux? I am exploring, but it is not very smooth. Hope you can give me some suggestions?

Hello friend
Regular Expressions are widely used in various fields related to UNIX/Linux and in multiple programming languages. From common shell commands to the famous Perl language to the popular PHP language, it plays an important role. Even the windows command line console supports regular expressions. If you are a Linux server administrator, you will often see it in some server setting scripts.

It can be said that it is a required knowledge point to learn Linux/UNIX well, otherwise you do not even understand the Linux Startup Script. However, it is indeed a bit obscure, and most of the relevant information is in English, which makes it more difficult to learn. Even if some Chinese translation materials are used, different translators may translate a wide variety of terms. To this end, I decided to write a concise tutorial for it and try to cover the main concepts involved in regular expressions.

I don't want to write this article as a detailed regular expression syntax manual. In fact, these manuals already exist, but it is hard to read them. I hope that after completing this tutorial, you can easily read the regular expression syntax manual of various tools and get started quickly. However, you must make good use of regular expressions, it is not a short tutorial that can solve the problem. It is the result of countless practical exercises. However, the last part of this article puts forward some principled suggestions for writing regular expressions. Learning the experiences of applying these regular expressions to the pioneers will reduce our detours in future practices.

The regular expression is a translation of the English "regular expressions". It is said that it can be traced back to more advanced theories such as "Neural Networks. So what is a regular expression?

A regular expression is a set of regular expressions that match the target string from left to right. Most characters represent themselves in the mode and match the corresponding characters in the target. For The simplest example, The pattern "The quick brown fox" matches a part of The target string that is exactly The same as that of The string.

As mentioned above, regular expressions are used by many tools rooted in UNIX/Linux, but the regular expression syntax of these tools is not exactly the same, some of these extensions of the regular expression syntax are not recognized by other tools, which makes it more difficult to use the regular expression. Therefore, when you use a regular expression in a specific environment, you must first look at the syntax range supported by the target environment to ensure that your regular expression is correctly parsed.

Hope it will be helpful to you. Good luck ~

How can I learn Regular Expressions in Linux? I am exploring, but it is not very smooth. Hope you can give me some suggestions?

What are you doing upstairs? Is it copy and paste? The problem cannot be solved properly.

There is no shortcut to learning regular expressions. If you want to use it, you can search for a change on the Internet. Otherwise, let's take a look at regular expressions and grasp the regular expressions.
There is no other way to really study regular expressions. to read "proficient regular expressions", other books are useless. This book is the Bible of the regular world. Don't be intimidated by the thickness of the book, as a person who has been here, you can tell you responsibly that on the day when you go through the regular expression, you will find that your efforts are very worthwhile. If you cannot get through, I have nothing to say...

The true core of Regular Expressions lies in the principle of parsing and the efficiency of returning

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.