$ cd /etc $ LANG=POSIX Set the locales to "POSIX" (equivalent to "LANG=C" or "LANG=" to clear all language settings) $ ls -d [A-Z]* | head -n 5 List the first 5 files or directories whose first character is uppercase using wildcards ConsoleKit DIR_COLORS Muttrc DIR_COLORS.xterm Muttrc.local $ LANG=en_US.UTF-8 Set the locales to "en_US.UTF-8" $ ls -d [A-Z]* | head -n 5 Repeat the same action once again bashrc The output is different now. blkid bluetooth bonobo-activation capi.conf |
$ cd /etc $ LANG=POSIX $ ls -d * | grep '[A-Z].*' | head -n 5 Use ls with grep to list the first 5 files or directories whose first character is uppercase. ConsoleKit DIR_COLORS DIR_COLORS.xterm Muttrc Muttrc.local $ LANG=en_US.UTF-8 Set the locales to "en_US.UTF-8" $ ls -d * | grep '[A-Z].*' | head -n 5 Test the command again to see the results. ConsoleKit The results are consistent now DIR_COLORS DIR_COLORS.xterm Muttrc Muttrc.local |
Utility | basic regular expressions | extended regular expressions |
shell | ||
vi | ○ | |
locate | ○ | |
find | ○ | ○ |
grep | ○ | ○ |
sed | ○ | ○ |
awk | ○ | ○ |
busy buzzing bumblebees buzzing busying 6 silly sisters selling shining shoes The driver was drunk and drove the doctor's car into deep ditch. can you can a can as a canner can can a can google Goggles Solves SUDOKU Puzzles. How much oil boil can a gum boil boil if a gum boil can boil oil? Where's the peck of pickled peppers Peter Piper picked? 55 Flags freely flutter from the floating frigate |
busy buzzing bumblebees buzzing busying 6 silly sisters selling shining shoes The driver was drunk and drove the doctor's car into deep ditch. (The middle output portion is omitted) /bu[nms] ←"In the vi status line, search for a string following 'bu' that is followed by either 'n', 'm', or 's'. |
Regular Expressions | Matches |
[0-9] | any number |
[a-z] | all lowercase letters |
[A-Z] | all uppercase letters |
[a-zA-Z] 或 [A-Za-z] | all letters |
[0-9a-zA-Z] | any numbers and letters |
busy buzzing bumblebees buzzing busying 6 silly sisters selling shining shoes The driver was drunk and drove the doctor's car into deep ditch. can you can a can as a canner can can a can google Goggles Solves SUDOKU Puzzles. How much oil boil can a gum boil boil if a gum boil can boil oil? Where's the peck of pickled peppers Peter Piper picked? (The middle output portion is omitted) /[A-Z]he ←Search for a string where the first letter is capitalized and followed by 'he'. |
busy buzzing bumblebees buzzing busying 6 silly sisters selling shining shoes The driver was drunk and drove the doctor's car into deep ditch. can you can a can as a canner can can a can (The middle output portion is omitted) /[^A-Z]he ←search 1st word The element is not capitalized, followed by a string of "he" |
POSIX Characters matches characters | |||
POSIX | ASCII | Illustrate | Note |
[:alnum:] | [A-Z,a-z,0-9] | English alphabet and numbers | |
[:alpha:] | [A-Z,a-z] | English alphabet | |
[:blank:] | Space(ASCII = 20H)和 TAB(ASCII = 9H) | ||
[:cntrl:] | [0H-1FH,7FH] | Control character | |
[:digit:] | [0-9] | Number | |
[:graph:] | [21H-7EH] | Characters that will be displayed | |
[:upper:] | [A-Z] | Uppercase letter | |
[:lower:] | [a-z] | Lower case letters | |
[:print:] | [20H-7EH] | Characters that will be displayed + spaces | |
[:punct:] | [\]\[!"#$%&')(*+,./:;<=>?@\^_`{|}~-] | Punctuation and symbols | |
[:space:] | [ \t\r\n\v\f] | Whitespace(characters not displayed) | Whitespace characters (such as newlines ) are generally not displayed, but Linux has its fixed representations for some commonly used Whitspace characters (refer to the column "Linux Common Representations" in [note] ASCII) |
[:xdigit:] | [A-F,a-f,0-9] | Character in hexadecimal |
busy buzzing bumblebees buzzing busying 6 silly sisters selling shining shoes The driver was drunk and drove the doctor's car into deep ditch. can you can a can as a canner can can a can google Goggles Solves SUDOKU Puzzles. How much oil boil can a gum boil boil if a gum boil can boil oil? Where's the peck of pickled peppers Peter Piper picked? 55 Flags freely flutter from the floating frigate (The middle output portion is omitted) /[[:digit:]] ← Search for Arabic numerals with POSIX Character |
busy buzzing bumblebees buzzing busying 6 silly sisters selling shining shoes The driver was drunk and drove the doctor's car into deep ditch. (The middle output portion is omitted) /s.ll ←Because "." can match any character, so the "sill" or "sell" both match |
Regular Expression | Matches | Example |
.* | Zero to any character. | [\]\[!"#$ABCabc0123\n\a\t etc. |
[0-9][0-9]* | One or more consecutive digits. | 0、11、12345、 543543543 |
[A-Z][A-Z]* | Greater than or equal to one or more consecutive uppercase letters. | Y、IJK、ZZZZZZ |
[a-z][a-z] * | Greater than or equal to one or more consecutive lowercase letters. | z、xyz、abcdefghijk |
Goo*gle | The "o" between G and gle can range from one to infinity | Gogle、Google、Gooooooooooogle |
yaho* | yah "o" can be zero to infinite | yah、yaho、yahoooooooooooooo |
.*k | from the first arbitrary character until "k" is encountered | This is a book |
G.* | "G" starts and continues until newline | Good morning Mr. Chen |
busy buzzing bumblebees buzzing busying 6 silly sisters selling shining shoes The driver was drunk and drove the doctor's car into deep ditch. can you can a can as a canner can can a can (The middle output portion is omitted) /can* |
busy buzzing bumblebees buzzing busying 6 silly sisters selling shining shoes The driver was drunk and drove the doctor's car into deep ditch. can you can a can as a canner can can a can (The middle output portion is omitted) /^can |
busy buzzing bumblebees buzzing busying 6 silly sisters selling shining shoes The driver was drunk and drove the doctor's car into deep ditch. can you can a can as a canner can can a can google Goggles Solves SUDOKU Puzzles. (The middle output portion is omitted) /^[^A-Z] ← matches characters whose starting position is not uppercase |
busy buzzing bumblebees buzzing busying 6 silly sisters selling shining shoes The driver was drunk and drove the doctor's car into deep ditch. can you can a can as a canner can can a can (The middle output portion is omitted) /an$ |
busy buzzing bumblebees buzzing busying 6 silly sisters selling shining shoes The driver was drunk and drove the doctor's car into deep ditch. can you can a can as a canner can can a can ←blank line google Goggles Solves SUDOKU Puzzles. (The middle output portion is omitted) /^$ ← matches a blank line |
busy buzzing bumblebees buzzing busying 6 silly sisters selling shining shoes (The) driver was drunk and drove the doctor's car into deep ditch. can you can a can as a canner can can a can google(Goggles)(Solves) SUDOKU (Puzzles). (The middle output portion is omitted) :1,$ s/[A-Z][a-z][a-z]*/(&)/g ←Add "( )" to the words whose first letter is uppercase and the subsequent letter is lowercase |
What are the numbers for I II III IV V VI VII VIII VIIII in Roman numerals? (The middle output portion is omitted) /VI\{2,3\} ←Search for 2~3 consecutive "I" strings after "V" |
gogle google gooogle goooogle gooooogle is a gd god good goood (The middle output portion is omitted) /go\{,2\}d ← search for a string between "g" and "d" where the upper limit of "o" is 2 |
gogle google gooogle goooogle gooooogle is a gd god good goood (The middle output portion is omitted) /o\{3\} ←Search for strings where the lower limit of "o" is 3 |
1,000 milliliter equal 1 liter. (The middle output portion is omitted) /\(li\)*ter ← Matches "li" from 0~∞ in front of "ter" |
busy buzzing bumblebees buzzing busying 6 silly sisters selling shining shoes The driver was drunk and drove the doctor's car into deep ditch. can you can a can as a canner can can a can google Goggles Solves SUDOKU Puzzles. (The middle output portion is omitted) /\<[[:upper:]]*\> ← matches words with uppercase letters |
busy buzzing bumblebees buzzing busying 6 silly sisters selling shining shoes> (The middle output portion is omitted) /\([a-zA-Z]\)\1 ←Search for a string of two adjacent and identical letters |
busy buzzing bumblebees buzzing busying silly 6 sisters selling shining shoes The driver was drunk and drove the doctor's car into deep ditch. (The middle output portion is omitted) :1,$ s/\(\<[0-9]*\>\) \(\<[a-z]*\>\) /\2 \1 /g ← If the matching string is all numbers and the next string is all lowercase letters, exchange positions |
busy buzzing bumblebees buzzing busying 6 silly sisters selling shining shoes The driver was drunk and drove the doctor's car into deep ditch. can you can a can as a canner can can a can google Goggles Solves SUDOKU Puzzles. (The middle output portion is omitted) /bus\|buzz ←matching "bus" or "buzz" |
$ grep -E 'ca(r|n)' re.txt ← match "car" or "can" (note! No need to escape characters) The driver was drunk and drove the doctor's car into deep ditch. can you can a can as a canner can can a can How much oil boil can a gum boil boil if a gum boil can boil oil? $ grep 'ca\(r\|n\)' re.txt ←If the option "-E" is removed For the basic regular expressions, or-match to add escape characters |
$ grep -E 'go+gle' re.txt ← list "gogle" or "google" or "goooooooooooogle" google Goggles Solves SUDOKU Puzzles $ seq 1 10000 | grep -E '^199+' ← list 199, 199, 19999999 Related figures 199 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 |
$ grep -E '[+-]?[0-9]+\.[0-9]+([Ee][+-][0-9]+)?' fileA ←List the real numbers in "fileA" |
Dec | Hex | Abbr | Linux Common Representations | Name | Dec | Hex | Glyph | Dec | Hex | Glyph | Dec | Hex | Glyph | |||
0 | 0 | NUL | Null | 32 | 20 | (空格) | 65 | 41 | A | 98 | 62 | b | ||||
1 | 1 | SOH | start of heading | 33 | 21 | ! | 66 | 42 | B | 99 | 63 | c | ||||
2 | 2 | STX | star of text | 34 | 22 | " | 67 | 43 | C | 100 | 64 | d | ||||
3 | 3 | ETX | end of text | 35 | 23 | # | 68 | 44 | D | 101 | 65 | e | ||||
4 | 4 | EOT | end of transmission | 36 | 24 | $ | 69 | 45 | E | 102 | 66 | f | ||||
5 | 5 | ENQ | enquiry | 37 | 25 | % | 70 | 46 | F | 103 | 67 | g | ||||
6 | 6 | ACK | acknowledge | 38 | 26 | & | 71 | 47 | G | 104 | 68 | h | ||||
7 | 7 | BEL | \a | bell | 39 | 27 | ' | 72 | 48 | H | 105 | 69 | i | |||
8 | 8 | BS | \b | backspace | 40 | 28 | ( | 73 | 49 | I | 106 | 6A | j | |||
9 | 9 | TAB | \t | horizontal tab | 41 | 29 | ) | 74 | 4A | J | 107 | 6B | k | |||
10 | 0A | LF | \n | line feed,new line | 42 | 2A | * | 75 | 4B | K | 108 | 6C | l | |||
11 | 0B | VT | \v | vertical tab | 43 | 2B | + | 76 | 4C | L | 109 | 6D | m | |||
12 | 0C | FF | \f | NP form feed, new page | 44 | 2C | , | 77 | 4D | M | 110 | 6E | n | |||
13 | 0D | CR | \r | carriage return | 45 | 2D | - | 78 | 4E | N | 111 | 6F | o | |||
14 | 0E | SO | Shift out | 46 | 2E | . | 79 | 4F | O | 112 | 70 | p | ||||
15 | 0F | SI | Shift in | 47 | 2F | / | 80 | 50 | P | 113 | 71 | q | ||||
16 | 10 | DLE | data link escape | 48 | 30 | 0 | 81 | 51 | Q | 114 | 72 | r | ||||
17 | 11 | DC1 | device ctrl. 1 (XON enable software control speed) |
49 | 31 | 1 | 82 | 52 | R | 115 | 73 | s | ||||
18 | 12 | DC2 | device ctrl. 2 | 50 | 32 | 2 | 83 | 53 | S | 116 | 74 | t | ||||
19 | 13 | DC3 | device ctrl. 3 (XOFF disable software control speed)) |
51 | 33 | 3 | 84 | 54 | T | 117 | 75 | u | ||||
20 | 14 | DC4 | device ctrl. 4 | 52 | 34 | 4 | 85 | 55 | U | 118 | 76 | v | ||||
21 | 15 | NAK | negative ack. | 53 | 35 | 5 | 86 | 56 | V | 119 | 77 | w | ||||
22 | 16 | SYN | syn. idle | 54 | 36 | 6 | 87 | 57 | W | 120 | 78 | x | ||||
23 | 17 | ETB | end of trans. block | 55 | 37 | 7 | 88 | 58 | X | 121 | 79 | y | ||||
24 | 18 | CAN | cancel | 56 | 38 | 8 | 89 | 59 | Y | 122 | 7A | z | ||||
25 | 19 | EM | end of medium | 57 | 39 | 9 | 90 | 5A | Z | 123 | 7B | { | ||||
26 | 1A | SUB | substitute | 58 | 3A | : | 91 | 5B | [ | 124 | 7C | | | ||||
27 | 1B | ESC | escape | 59 | 3B | ; | 92 | 5C | \ | 125 | 7D | } | ||||
28 | 1C | FS | file separator | 60 | 3C | < | 93 | 5D | ] | 126 | 7E | ~ | ||||
29 | 1D | GS | group separator | 61 | 3D | = | 94 | 5E | ^ | 127 | 7F | DEL (Invisiable) |
||||
30 | 1E | RS | record separator | 62 | 3E | > | 95 | 5F | _ | |||||||
31 | 1F | US | unit separator | 63 | 3F | ? | 96 | 60 | ` | |||||||
127 | 7F | DEL | delete | 64 | 40 | @ | 97 | 61 | a | |||||||