Linux command regular expression

Source: Internet
Author: User
The linux command line can do all the work, which is very different from the ms product. Up to now, many fans have never been tired of typing on the keyboard, in heavy management and keyboard work, regular expressions play a major role in simplifying work. Perlregularex...
The linux command line can do all the work, which is very different from the ms product. Up to now, many fans have never been tired of typing on the keyboard, in heavy management and keyboard work, regular expressions play a major role in simplifying work. Perl regular expressions man page. http://www.perldoc.com/perl5.8.0/pod/perlre.html
I. INTRODUCTION to regular expressions in fact, regular expressions are not only applicable to linux, but can also be used in multiple programming languages, because they are actually expressions of certain patterns. If you want to talk about all of his applications, this article will become too long to be able to read. I will only introduce how he works with several of the most important commands in linux. Basic metacharacters: ^ The first $ line at the end of the row * matches the characters before the asterisk once or multiple times [] matches the characters in, it can be either a character or a character column \ to block the special meaning of a metacharacter, which can make it meaningless. Pattern \ {n \} is used to match the number of occurrences of the previous pattern. N is the number of times. The most commonly used are the beginning and end of a row, which may be used anywhere. for example, if you enter $, you can jump to the end of a row on bbs, you can also enter $ to jump to the last article of this version. What we will talk about later is an example of their combination with specific commands. 2. grep and the regular expression grep are very common tools. linux is actually like the internet, and is full of information and data, how to effectively filter unused information and obtain useful information reflects a person's level. when I was a new linux user, I still didn't know how to use grep. The simplest application example is how to view only folders in a directory. ls does not provide such a parameter for you, but you need to use grep (or awk mentioned later). We know that the property of a folder is dxxx, so we can use ls-l | grep ^ d to print the project that matches the first letter of a row with d, in this way, all the folders are printed out and other non-folder files are ignored. we can use them to match all permissions. for example, if I want to find a file with all permissions rwx, in this way, we can enter ls-l | grep ^. rwx, where. match any character. Let's look at the example below. to find a file with the permission of ---- rw ----, you can use ls-l | grep ^ .... rw ...., how about it? it's much simpler. we can also find all the files ending with "*" (which is used to represent executable files in some linux systems), that is, ls | grep \ * $, note that \ must be added to block the original special meaning of *, while $ is added at the end of the row. You can use the ^ symbol to describe the meaning of a non-regular expression (I .e., no). as I mentioned earlier, what should I do if I want to see a file that is not a folder: ls-l | grep ^ [^ d]. This means that you can view all the items whose names start with a letter other than d. you can also use the-v option of grep to implement the same function. However, the focus today is on regular expressions, so we will not talk about parameters. I can also find multiple files. if there is a directory containing my own content, I only know that he is in a .doc file. I can use the grep "expression "*. doc, and you need to think about this expression. There are www.2cto.com methods that can specify fuzzy expressions, such as k... d, which indicates a string starting with a lowercase letter k and ending with an uppercase letter D, and k * D, it is a string that can start with k and can be any character in length and end with D. It can also be specified by [], for example, [12345] day, it refers to a string that starts with any character from 1 to 6 and ends with day. for example, 3 days matches this expression, but 13 days does not. [Ss] can be used to specify uppercase or lowercase characters, while combining pattern \ {n \} can also be used in a more amazing way. 3. awk and regular expression awk are very useful for extracting specified data packets from large data files. for example, you can use it to manage passwd files or log files (quite large files, when awk is used, it will become very easy.) generally, awk is used in combination with grep, grep is used for traveling, and the columns specified in awk is used for traveling, however, even if awk is used separately, it is equally useful. Take a look at the example awk-F: '{if ($6 ~ /Bash/) print $0} '/etc/passwd this is a complicated example, the purpose of this example is to demonstrate that the combination of command syntax and regular expressions can implement a considerable filtering effect, the purpose of this command is to extract all bash-containing projects from www.2cto.com in the sixth column of the passwd file and print the first item of these projects, that is, their username. I suppose passwd is root: *:/bin/bash user :*:*:*:*:*: /bin/bash xie: *:/bin/csh ftp :*:*:*:*:*: /bin/zsh, the output should be analyzed one by one as the root user. first,-F: specifies: As the delimiter, because the default delimiter is space, and then we use a hypothetical statement, assume that the statement and the print statement are combined into an action, so they must be enclosed in curly brackets. This means that if (if) column 6 ($6) matches (~) String bash, then print (print) column 0 ($0 ). Among them,/bash/refers to matching bash, // only plays a role and is not involved in judgment. Awk has its own regular expressions and conditional operators. It has two more metacharacters than a regular expression, that is, + and ?, + Match one or more characters. The occurrence frequency of the matching mode, for example,/XY? Z/matches XYZ, or YZ. Conditional operators = greater than or equal ~ Match regular expression !~ If the regular expression does not match the above expression, more actions can be performed, for example, ls-l | awk 'In in {print "Name \ t SIZE"} {if/^ [^ d]/} {print $9 "\ t" $5} {tot + = 5} END {print "total KB: "tot} 'can be used to print the length of the file name, and then output the total size of all files. He finds out files that are not folders first, prints their size and length, and puts these size values in the tot variable, add (+ =) at a time and print the tot to obtain the size of all files. Logical symbols & AND must be both true! No, reverse awk '{if ($3 = "root" & $5 = "/root ") print $0} '/etc/passwd: print the user name when both the group and the personal directory are root. Hey, in fact, awk still has arithmetic operators. I will not write them for laziness, but the above example already exists (with ++ =). let's take a look. 4. Regular expressions are used for editing VI and regular expressions. for example, if you enter ^ in the editing status, you can jump to the first non-null character in the row and enter $, you can jump to the end of the row (when the row number is represented, $ represents the last row ). Enter/to search for strings. you can also use regular expressions to match strings. for example,/^ str matches the str at the beginning of the line. if you enter/str $, the str at the end of the line matches the str at the end of the line. Others are the same as regular expression methods. 5. Summary in linux, more complex sed expressions will also be used. In fact, most of the information filtering problems are inseparable from him. However, this article is the only reason why I can't write it, however, you can never say hello to me because I wrote this article ...... Another point is for beginners: remember to use multiple channels | pipeline and file operators. 0 indicates standard input, 1 indicates standard output, and 2 indicates error output, you can use the other 3-9 files at will. for example, you can first point a file to operator 4 and then point 4 to 0, then you can use it as the input of the program.
Father-in-law
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.