Tải bản đầy đủ - 0 (trang)
Pattern Matching (Linux in a Nutshell, 3rd Edition)

Pattern Matching (Linux in a Nutshell, 3rd Edition)

Tải bản đầy đủ - 0trang

Pattern Matching (Linux in a Nutshell, 3rd Edition)







Examples



A thorough guide to pattern matching can be found in the Nutshell handbook Mastering

Regular Expressions by Jeffrey E. F. Friedl.



9.1. Filenames Versus Patterns

Metacharacters used in pattern matching are different from those used for filename expansion.

When you issue a command on the command line, special characters are seen first by the

shell, then by the program; therefore, unquoted metacharacters are interpreted by the shell for

filename expansion. The command:

$ grep [A-Z]* chap[12]

could, for example, be interpreted by the shell as:

$ grep Array.c Bug.c Comp.c chap1 chap2

and grep then would try to find the pattern "Array.c" in files Bug.c, Comp.c, chap1, and

chap2. To bypass the shell and pass the special characters to grep, use quotes:

$ grep "[A-Z]*" chap[12]

Double quotes suffice in most cases, but single quotes are the safest bet.

Note also that * and ? have subtly different meanings in pattern matching and filename

expansion.



8.9. Built-in csh and tcsh

Commands



9.2. Metacharacters, Listed

by Linux Program



Copyright © 2001 O'Reilly & QKFIN. All rights reserved.



file:///E|/O'Reilly/O'Reilly%20-%20Linux%20in%20a%20Nutshell,%203rd%20Edition/Pages/ch09_01.htm (2 of 2) [15/05/2002 18:10:14]



Metacharacters, Listed by Linux Program (Linux in a Nutshell, 3rd Edition)



Linux in a Nutshell, 3rd

Edition



9.2. Metacharacters, Listed by Linux Program

Some metacharacters are valid for one program but not for another. Those that are available to

a given program are marked by a bullet ( ) in the following table. Notes are provided after the

table, and full descriptions of metacharacters are in the following section.

Symbol ed vi sed awk grep egrep Action

.



Match any character (can match newline in

gawk).



*



Match zero or more preceding.



^



Match beginning of line or string.



$



Match end of line or string.



\



Escape character following.



[]



Match one from a list or range.



\(\)



Store pattern for later replay.



\n



Reuse matched text stored in nth \( \).



{}



Match a range of instances.



\{\}



Match a range of instances.



\<\>



Match word's beginning or end.



+



Match one or more preceding.



?



Match zero or one preceding.



|



Separate choices to match.



()



Group expressions to match.



file:///E|/O'Reilly/O'Reilly%20-%20Linux%20in%20a%20Nutshell,%203rd%20Edition/Pages/ch09_02.htm (1 of 2) [15/05/2002 18:10:18]



Metacharacters, Listed by Linux Program (Linux in a Nutshell, 3rd Edition)



On some Linux systems, grep is a link to egrep, so whenever you run grep you actually get

egrep behavior.

In ed, vi, and sed, when you perform a search-and-replace (substitute) operation, the

metacharacters in this table apply to the pattern you are searching for but not to the string

replacing it.

In awk, {} is specified in the POSIX standard and is supported by gawk if you run it with the Wre-interval option.

In ed, vi, and sed, the following additional metacharacters are valid only in a replacement

pattern:

Symbol ex sed ed Action

\



Escape character following.



\n



Reuse matched text stored in nth \( \).



&



Reuse previous search pattern.



~



Reuse previous replacement pattern.



\e



Turn off previous \L or \U.



\E



Turn off previous /L or /U.



\l



Change single following character to lowercase.



\L



Change following characters to lowercase until /E encountered.



\u



Change single following character to uppercase.



\U



Change following characters to uppercase until \E encountered.



9. Pattern Matching



9.3. Metacharacters



Copyright © 2001 O'Reilly & QKFIN. All rights reserved.



file:///E|/O'Reilly/O'Reilly%20-%20Linux%20in%20a%20Nutshell,%203rd%20Edition/Pages/ch09_02.htm (2 of 2) [15/05/2002 18:10:18]



Metacharacters (Linux in a Nutshell, 3rd Edition)



Linux in a Nutshell, 3rd

Edition



9.3. Metacharacters

The following characters have special meaning in search patterns:

Character Meaning

.



*



^



$



[ ]



Match any single character except newline.

Match any number (or none) of the single character that immediately precedes

it. The preceding character also can be a regular expression (e.g., since . (dot)

means any character, .* means match any number of any character -- except

newlines).



Match the beginning of the line or string.



Match the end of the line or string.



Match any one of the enclosed characters. A hyphen (-) indicates a range of

consecutive characters. A circumflex (^) as the first character in the brackets

reverses the sense: it matches any one character not in the list. A hyphen or

close bracket (]) as the first character is treated as a member of the list. All

other metacharacters are treated as members of the list.



[^ ]



Match anything except enclosed characters.



\{n,m\}



Match a range of occurrences of the single character that immediately

precedes it. The preceding character also can be a regular expression. \{n\}

matches exactly n occurrences, \{n,\} matches at least n occurrences, and

\{n,m\} matches any number of occurrences between n and m.



{n,m}



Like \{n,m\}. Available in grep by default and in gawk with the -Wreinterval option.



file:///E|/O'Reilly/O'Reilly%20-%20Linux%20in%20a%20Nutshell,%203rd%20Edition/Pages/ch09_03.htm (1 of 3) [15/05/2002 18:10:21]



Metacharacters (Linux in a Nutshell, 3rd Edition)



\



Turn off the special meaning of the character that follows.



\(\)



Save the matched text enclosed between \( and \) in a special holding space.

Up to nine patterns can be saved on a single line. They can be "replayed" in

the same pattern or within substitutions by the escape sequences \1 to \9.



\n



Reuse matched text stored in nth \( \).



()



In egrep and gawk, save the matched text enclosed between \( and \) in a

holding space to be replayed in substitutions by the escape sequences \1 to \9.



\<\>



Match the beginning (\<) or end (\>) of a word.



+



Match one or more instances of preceding regular expression.



?



Match zero or one instance of preceding regular expression.



|



Match the regular expression specified before or after.



()



Group regular expressions.



Many utilities support POSIX character lists, which are useful for matching non-ASCII

characters in languages other than English. These lists are recognized only within [] ranges. A

typical use would be [[:lower:]], which in English is the same as [a-z].

The following table lists POSIX character lists:

Notation



Action



[:alnum:] Alphanumeric characters

[:alpha:]



Alphabetic characters, uppercase and lowercase



[:blank:]



Printable whitespace: spaces and tabs but not control characters



[:cntrl:]



Control characters, such as ^A through ^Z



[:digit:]



Decimal digits



[:graph:]



Printable characters, excluding whitespace



[:lower:]



Lowercase alphabetic characters



[:print:]



Printable characters, including whitespace but not control characters



[:punct:]



Punctuation, a subclass of printable characters



[:space:]



Whitespace, including spaces, tabs, and some control characters



file:///E|/O'Reilly/O'Reilly%20-%20Linux%20in%20a%20Nutshell,%203rd%20Edition/Pages/ch09_03.htm (2 of 3) [15/05/2002 18:10:21]



Metacharacters (Linux in a Nutshell, 3rd Edition)



[:upper:]



Uppercase alphabetic characters



[:xdigit:]



Hexadecimal digits



The following characters have special meaning in replacement patterns:

Character Meaning

\



Turn off the special meaning of the character that follows.



\n



Restore the nth pattern previously saved by \( and \). n is a number from 1 to

9, matching the patterns searched sequentially from left to right.



&



Reuse the search pattern as part of the replacement pattern.



~



Reuse the previous replacement pattern in the current replacement pattern.



\e



End replacement pattern started by \L or \U.



\E



End replacement pattern started by \L or \U.



\l



Convert first character of replacement pattern to lowercase.



\L



Convert replacement pattern to lowercase.



\u



Convert first character of replacement pattern to uppercase.



\U



Convert replacement pattern to uppercase.



9.2. Metacharacters, Listed

by Linux Program



9.4. Examples of Searching



Copyright © 2001 O'Reilly & QKFIN. All rights reserved.



file:///E|/O'Reilly/O'Reilly%20-%20Linux%20in%20a%20Nutshell,%203rd%20Edition/Pages/ch09_03.htm (3 of 3) [15/05/2002 18:10:21]



Examples of Searching (Linux in a Nutshell, 3rd Edition)



Linux in a Nutshell, 3rd

Edition



9.4. Examples of Searching

When used with grep or egrep, regular expressions normally are surrounded by quotes to avoid interpretation by

the shell. (If the pattern contains a $, you must use single quotes, as in '$200', or escape the $, as in "\$200".)

When used with ed, vi, sed, and awk, regular expressions usually are surrounded by / (although any delimiter

works). Here are some sample patterns:

Pattern



What does it match?



bag



The string bag.



^bag



"bag" at beginning of line or string.



bag$



"bag" at end of line or string.



^bag$



"bag" as the only text on line.



[Bb]ag



"Bag" or "bag."



b[aeiou]g



Second character is a vowel.



b[^aeiou]g



Second character is not a vowel.



b.g



Second character is any character except newline.



^...$



Any line containing exactly three characters.



^\.



Any line that begins with a dot.



^\.[a-z][a-z]



Same, followed by two lowercase letters (e.g., troff requests).



^\.[a-z]\{2\}



Same as previous, grep or sed only.



^[^.]



Any line that doesn't begin with a dot.



bugs*



"bug," "bugs", "bugss", etc.



"word"



A word in quotes.



"*word"*



A word, with or without quotes.



[A-Z][A-Z]*



One or more uppercase letters.



[A-Z]+



Same, egrep or awk only.



[A-Z].*



An uppercase letter, followed by zero or more characters.



[A-Z]*



Zero or more uppercase letters.



[a-zA-Z]



Any letter.



file:///E|/O'Reilly/O'Reilly%20-%20Linux%20in%20a%20Nutshell,%203rd%20Edition/Pages/ch09_04.htm (1 of 3) [15/05/2002 18:10:24]



Examples of Searching (Linux in a Nutshell, 3rd Edition)



[0-9A-Za-z]+ Any alphanumeric sequence.

egrep or awk pattern What does it match?

[567]



One of the numbers 5, 6, or 7



five|six|seven



One of the words five, six, or seven



80[23]?86



8086, 80286, or 80386



compan(y|ies)



company or companies



vi pattern What does it match?

\


Words like theater or the



the\>



Words like breathe or the



\



The word the



sed or grep pattern



What does it match?



0\{5,\}



Five or more zeros in a row



[0-9]\{3\}-[0-9]\{2\}-[0-9]\{4\} Social security number (nnn-nn-nnnn)



9.4.1. Examples of Searching and Replacing

The following examples show the metacharacters available to sed and vi. We have shown vi commands with an

initial colon because that is how they are invoked within vi. A space is marked by a ; a tab is marked by tab.

Command



Result



s/.*/(&)/



Reproduce the entire line, but add parentheses.



s/.*/mv & &.old/



Change a wordlist (one word per line) into mv commands.



/^$/d



Delete blank lines.



:g/^$/d



Same as previous, in vi editor.



/^[ tab]*$/d



Delete blank lines, plus lines containing spaces or tabs.



:g/^[ tab]*$/d



Same as previous, in vi editor.



s/



Turn one or more spaces into one space.



:%s/



*/ /g

*/ /g



Same as previous, in vi editor.



:s/[0-9]/Item &:/



Turn a number into an item label (on the current line).



:s



Repeat the substitution on the first occurrence.



:&



Same as previous.



:sg



Same, but for all occurrences on the line.



:&g



Same as previous.



file:///E|/O'Reilly/O'Reilly%20-%20Linux%20in%20a%20Nutshell,%203rd%20Edition/Pages/ch09_04.htm (2 of 3) [15/05/2002 18:10:24]



Examples of Searching (Linux in a Nutshell, 3rd Edition)



:%&g



Repeat the substitution globally.



:.,$s/Fortran/\U&/g Change word to uppercase, on current line to last line.

:%s/.*/\L&/



Lowercase entire file.



:s/\<./\u&/g



Uppercase first letter of each word on current line. (Useful for titles.)



:%s/yes/No/g



Globally change a word (yes) to another word (No).



:%s/Yes/~/g



Globally change a different word to No (previous replacement).



Finally, here are some sed examples for transposing words. A simple transposition of two words might look like

this:

s/die or do/do or die/



Transpose words



The real trick is to use hold buffers to transpose variable patterns. For example:

s/\([Dd]ie\) or \([Dd]o\)/\2 or\1/



9.3. Metacharacters



Transpose, using hold buffers



10. The Emacs Editor



Copyright © 2001 O'Reilly & QKFIN. All rights reserved.



file:///E|/O'Reilly/O'Reilly%20-%20Linux%20in%20a%20Nutshell,%203rd%20Edition/Pages/ch09_04.htm (3 of 3) [15/05/2002 18:10:24]



The Emacs Editor (Linux in a Nutshell, 3rd Edition)



Linux in a Nutshell, 3rd

Edition



Chapter 10. The Emacs Editor

Contents:

Introduction

Typical Problems

Summary of Commands by Group

Summary of Commands by Key

Summary of Commands by Name

This chapter presents the following topics:













Introduction

Typical problems

Summary of Emacs commands by group

Summary of Emacs commands by key

Summary of Emacs commands by name



10.1. Introduction

Although Emacs is not part of Linux, this text editor is found on many Unix systems because

it is a popular alternative to vi. Many versions are available. This book documents GNU

Emacs, which is available from the Free Software Foundation in Cambridge, Massachusetts.

For more information, see the O'Reilly book Learning GNU Emacs, 2d ed., by Debra

Cameron, Bill Rosenblatt, and Eric Raymond.

To start an Emacs editing session, type:

emacs [file]



9.4. Examples of Searching



10.2. Typical Problems



file:///E|/O'Reilly/O'Reilly%20-%20Linux%20in%20a%20Nutshell,%203rd%20Edition/Pages/ch10_01.htm (1 of 2) [15/05/2002 18:10:27]



The Emacs Editor (Linux in a Nutshell, 3rd Edition)



Copyright © 2001 O'Reilly & QKFIN. All rights reserved.



file:///E|/O'Reilly/O'Reilly%20-%20Linux%20in%20a%20Nutshell,%203rd%20Edition/Pages/ch10_01.htm (2 of 2) [15/05/2002 18:10:27]



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Pattern Matching (Linux in a Nutshell, 3rd Edition)

Tải bản đầy đủ ngay(0 tr)

×