Use regular expressions and grep, sed, awk (1), grepawk
Some nonsense:
For regular expressions, I have always been confused. I can see some code. The script contains regular expressions, awk, sed, and I am totally confused. I can't remember how to use it at ordinary times, so I won't summarize it. If you are free, Let's sum up. By the way, we can overcome the weak points of shell and js with regular expressions. I plan to write in three articles
Body:
Generally, regular expressions are divided into several types, but they are basically the same. What I know is "basic regular expressions", "extended regular expressions", and "perl regular expressions ", this article prefers to record "basic regular" and "extended regular", which play an important role in grep, egrep, sed, and awk.
Before starting regular expressions, review some common grep parameters:
-N: the row number,-v. Select-I in the reverse direction to ignore case sensitivity.
Next, let's start copying books. For more information, see laruence's Linux Private food. Basic Learning. First download the text we want to use:
Http://linux.vbird.org/linux_basic/0330regularex/regular_express.txt
The content is as follows:
First, learn "basic regular expressions"
1. Direct Matching
Example 1. Find the text containing apple and is respectively
This should be the simplest way to use regular expressions.
2. square brackets []
[] It mainly performs set matching. The usage is illustrated by examples.
Example 2 match the text containing test and tast
It can be seen that [] is to select one from the set [AE] to match
3. brackets [] and hyphens-Combination
If you want to match a text containing a number, we can write it as [0123456789], but this is too troublesome. This requires a hyphen. For numbers, you can write it as [0-9]. similarly, for letters can be applied up, uppercase letters [A-Z], lowercase letters [a-z], can also be combined in a piece, such as uppercase and lowercase letters [a-zA-Z].
Example 3: Find the text containing numbers
4. square brackets [],-, ^, and combination of the three
In [], ^ indicates inverse. For example
Example 4. extract text with oo But without g before oo
Last "19: goooooogle yes !" Why does it match? Although goo is in the front, it is obviously not satisfied, but go (oo) oogle is satisfied, so it is matched. This may be one of the difficulties of regular expressions. The regular expressions you write may have bugs, but you have not found them yet.
Example 5: match the text with oo, but the front of oo does not contain lowercase letters
As you can see, this is [],-, ^ used together. Note: ^ indicates the inverse in.
5. ^ and $
^ Is displayed here, but it is different from the above. ^ indicates the beginning of the row, and $ indicates the end of the row.
Example 6 retrieve the text starting with
Example 7 retrieve text ending with a number or letter
Example 8 retrieve empty rows
Use '^ $' to match empty rows
6. Dot. And asterisk *
Point number. indicates that there is only one arbitrary character
The asterisk indicates that the previous 0 or multiple characters are repeated.
Example 9: g ?? String of d (there are two characters between gd)
As shown in the result, the dot. represents any character.
In this example, 10 matches at least two consecutive o characters.
Note that the meaning of "*" is different from that of the wildcard * we know.
Example 11: match the text starting with g and ending with g
You cannot use 'G * G' because * is different from the wildcard character. The correct character is 'G. * G'
So remember, the regular expression * is different from the wildcard!
7. Escape \
If the text we want to match exactly represents some special characters ("laruence's linux house dish" says it has special meanings in shell, I think it is incorrect, or it is misleading, is it in shell? For example, the point number in his example represents the local directory in shell, right? In fact, the real reason is. Is it regular expression Characters ?), What should I do? Escape!
If you match the text ending with a dot., we know that the regular expression matches only one character, so you can use '\. $'
Write the basic regular expression first today.
References:
Laruence's Linux private house dish
Linux Programming
Http://www.ibm.com/developerworks/cn/education/aix/au-unixtips3/
Http://www.cnblogs.com/chengmo/archive/2010/10/10/1847287.html
Grep sed awk vim regular expression, What are them? What did you do?
There are many problems. O (∩ _ ∩) O ~
1. grep is a query tool in shell.
2. sed is an online editor that processes a row of content at a time. You can change the content of a string or file.
3. awk is a programming language used to process text and data in linux/unix. Data can come from standard input, one or more files, or the output of other commands. It supports user-defined functions and dynamic regular expressions.
4. vim is an editing tool.
5. Regular Expressions are a type of pattern matching. For example, you can use grep to query rows that contain xx.
Ps: grep, sed, and awk can all be combined with regular expressions to meet your needs.
What are the differences between shell scripts and regular expressions?
Different things. A shell script is a set of automatically executed commands for shell. It is a programming language. The regular expression is a very powerful string search tool for text search and matching. The two are not the same. Regular Expressions can be used in shell scripts, and regular expressions can be used in Javascript scripts, such as Php, asp, C, C ++, and C #, java and other programming languages can also use regular expressions.