1.3 Text Match Some Unix text handles allow you to search, some of the text mode, not a fixed string. These applications include editing tools ED, EX, VI, and SED, as well as the AWK program language and command line commands GREP and EGREP. Text mode (more formally should be called regular expressions) containing normal characters and a series of special characters (Called element characters)
1.3.1 File Name The metamorphism used in mode matching is different from the metadature of the file name extension. When you send a command on the command line, the special character is first seen in the shell before it is the program, so the metamor of the quotation is not changed by the shell to modify the extension. Such as command: $ grep [a-z] * CHAP [12] cannot be converted to: $ grep array.c bug.c comp.c chap1 chap2
This begins to find the Array.c string in the file bug.c comp.c, chap1, and chap2. In order to pass the shell and pass the special character to grep, it is enough to use quotation marks below: $ grep "[a-z] *" CHAP [12] double quotes in most cases, but single quotes are the safest usage.
Note In mode matching, "?" Will match 0 or 1 regular expression instance; in the file name extension, matches a character.
1.3.2 Emotive characters are different, so that they have a relationship. In particular, the regular expression is used to search in text, and the metammat is often used in the process of processing different sets. These colors are also different, this section describes how these metacomposes are used when searching and replacement, and illustrates different variables in different applications.
1.3.2.1 Search Mode The characters in the table below are only special in search mode:
Character mode. Match any single character outside of the new line. Match the blank line * in AWK, match any (or not), the characters before it. Previous characters can be a regular expression. For example, because. You can represent any characters, then indicate that "Matching any number of any character" ^ After the start of one line or a string, the regular expression is matched in a row or a string tail section. The previous regular expression / cancel the special meaning of the next character [] Matches any character enclosed with parentheses. Connect characters "-" represents the range of consecutive characters. ^ Symbols indicate that the characters that match anything that are not in parentheses are in the first character position to be a hyphen or hierarchy is considered to be an element in the list. All metamatics are considered to be a member {n, m} in the list. The range of occurrences of the previous character. The previous characters can also be metammatics, {n} matches just N times; {n,} matches at least N times {n, M} matches the N times to M times. n, m must be greater than 0 less than 255. / {n, m} is just like {n, m}, but just add a backslapped before parentheses ./ (/) put the mode included between / (and /) in a special In the storage space. Up to 9 modes can be stored in a row. The text that matches the quilt pattern can be "playback" / N-return from / (and /) in the nth mode of "playback" / N back to / (and /) by / 1 to / 9. N is a number from 1 to 9. Start with the leftmost position ./ > Match the character of the word in /
Many UNIX systems allow the POSIX character set in a set of characters enclosed in square brackets. That is, refers to the contents enclosed in [: and:]. For example [: alnum:] matches a character of a text number
Character Class Matching Character AlNum Text Number Character Alpha Alphabet Character Blank Space or TabCntrl Control Character DIGIT Extreme Digital GRAPH Non-spaced Character LOWER Cell Small Writing Character Print Printable Character Space Space Character Upper Upperword XDigit Sixteen Binary Number 1.3.2.2 Replacement mode
The characters in the table below are only special in the replacement mode:
Character Mode / Close The special meaning / n Storage / (and /) Save N mode matches the text. N is a number from 1 to 9, 1 is the text from the left end & reuse search mode to be used as a replacement mode ~ reuse the previous replacement mode as the current replacement mode. Only the previous replacement mode for the only one-character (EX and VI)% reuse in the replacement mode is the current replacement mode. It can only turn the first character of the current replacement mode to uppercase / u to transfer the entire replacement mode to uppercase / u for the unique character (ED) / U of the replacement mode. / L transfers the first character of the current replacement mode to lowercase / L to transfer the entire replacement mode to / u or / l / L before lower writing / E close before U or / L
1.3.3 Metacity, listing some metamorphors in a UNIX program is valid for a program, but it may be invalid for the other. We are valid in which UNIX program will be marked in the following table. (This table is correct for SVR4 and Solaris and most UNIX systems, but for your system, it does not always perform valid) P "The item indicated by the POSIX standard is specified.
Symbol ed ex vi sed awk grep egrep operation. Y Y Y Y Y Y Y match any one character * Y Y Y Y Y Y Y character matches zero or more previous ^ Y Y Y Y Y Y Y matches a row or The start section of the string matches the end of a row or a string section / y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y Y Y Y y y Y Y Y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y matches an instance of a range / > zero or one character before the Y Y Y one or more characters before the first word of a word ending match Y Y Y Y match or a match |? Y Y isolation match selection () Y Y for a match Group expression
Note In ED, EX, VI, SED, you can specify the search mode (left) or you can specify a replacement mode (right). The metammathered listed in the table is only valid in the search mode.
In ED, EX, VI, and SED, the following metammatics are only valid in the replacement mode: symbol EX VI SED ED Action / Y Y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y match The mode stored mode & y y y y y y y y y y y y y y y y y y y y y ybe becomes a larger write / l / l y y y y y y y y Come down / e y y close / u or / l / e y y close / u or /L1.3.4 Search When using GREP or EGREP, the regular expression should be caused by quotation marks. (If mode In contractions, you must use single quotes) When using regular expressions with ED, EX, SED, and AWK, they must always be surrounded by / surrounded (except AWK), and any delimiter is valid. The following is an example of several modes:
Pattern matching result BAG BAG string ^ BAG started in Bag BAG $ with BAG End ^ Bag $ contains BAG's row [bb] AG BAG or BAGB [AEIOU] G The second character is a word audio letter B [^ aeiou] g The second letter is a counseling letter BG second letter is any letter ^ ... $ any only one line containing three characters ^ /. "1". " AZ] [AZ] With the previous mode, the two characters will be lowercase ./[AZ]/} is the same as the previous mode, only in ED, GREP, and SED ^ [^.] any one Take ".". Bugs * bug, bugs, bugss, etc. "Word" word "* word" caused by quotation marks, there is or without quotation points to guide [AZ] [AZ] * one or more Upperword Letters [AZ] , only in EGREP and AWK [[: Upper:]] 同上, POSIX's EGREP or AWK uses [AZ]. * A uppercase letter, followed by 0 or more characters [AZ] * 0 or more uppercase characters [A-ZA-Z] Any letter, no matter whether it is uppercase or lowercase [^ 0-9A-ZA-Z] any symbol or space (not a letter or number) [ ^ [: alnum:]] 同 上, using POSIX character set
EGREP or AWK mode matching results [567] 5, 6,7 in a number FIVE | SIX | Seven a word 80 [2-4]? 86 8086, 80286, 80386 or 8048680 [2 -4]? 86 | Pentium 8086, 80286, 80386 or 80486 or PentiumCompan (Y | IES) Company or companies
EX or VI pattern matching result /
The results of ED, SED or GREP mode match 0 / {5, /} have more than five 0 [0-9] / {3 /} - [0-9] / {2 /} - [0-9] / {2 /} - [0-9] / {2 /} - [0- 9] / {4 /} NNN-NN-NNNN format Number / (Why/ ).*1 Two WHY-line /([[[Alpha: ]]_][[ :alnum:】 /) = / 1; C assignment statement
1.3.4.1 Search and replace the examples of the following examples illustrate the emotes in the SED or EX. Note that the EX command starts with a colon. When the target is empty, it is S, Tab is recorded as T
Command Results S /. (&) / Reduction, but add parentheses S /.*/ mv & & .old / transform MV command one word list (word per line) / ^ $ / D Delete empty Row: g / ^ $ / d Delete the blank line in the above command / ^ [st] * $ / d, and only the space and [] line: g / ^ [st] * $ / d, in Use S / SS * / S / G in EX to turn one or more spaces into one:% s / s/ / s / g, in EX, use: S / [0-9] / item &: / A number becomes a group of current rows: s Reconside again when the first appears: &: SG is the same, but is the time: & g is the same as that:% & g Resembles on all lines: ., $ s / fortran // u & / g from the current line to the last line, change the word to uppercase:% s /.*// L & / Whole file becomes lowercase: s //<.// u & / g The first character of all the words of the current row is converted to uppercase:% s / yes / no / g replaced the word in full text to NO:% s / yes / ~ / g replaced a different word with NO in full, Here, there are some examples of SED replacement words. A simple replacement example is as follows: S / DIE or DO / DO or DIE /
Real skills are the use of buffers for the replacement variable mode. For example, replace with a buffer: s // ([DD] IE /) OR / ([DD] O /) // 2 OR / 1 /