Linux-shell script command grep

Source: Internet
Author: User

[grep profile : ]

grep is used to filter lines that contain specific charactersusage: grep ' pattern ' fileFor example: grep ' BBB ' aaa.txt--color # Search for the keyword BBB from the file Aaa.txt and highlight it.

[Regular meta-character : ]

grep can be used in conjunction with regular expressions, the following describes the use of some commonly used regular expression filter characters .
1. ^ Denotes line startgrep ' ^bbb ' aaa.txt --color # finds lines beginning with BBB from the file aaa.txt.
2. $ means end of linegrep ' bbb$ ' aaa.txt --color # finds lines ending in BBB from the file aaa.txt.
3. . Represents a single character that can match all characters except a newline character . grep ' bbb ... ' aaa.txt --color # Find The three-character line after the BBB from the file aaa.txt, ' dots ' can match spaces.
4. * indicates that the character in front of it can appear any timegrep ' bbb.* ' aaa.txt --color # finds rows with any character following the BBB from the file Aaa.txt .
5. + indicates that the character in front of it must appear at least oncegrep-e ' bbb.+ ' aaa.txt--color # finds rows from file Aaa.txt that appear at least one character after the BBB .
6. Indicates that the character in front of it may or may not be
grep-e ' bbbc? ' aaa.txt--color # Find rows with BBB or aaa.txt from file Bbbc . Note: grep does not support the +,? Two metacharacters, and if you want to use it, you can only use the extended grep (egrep or GREP-E)
7. [] means match a character, the character that appears in [] is or the relationshipgrep ' ^[bb]bb ' aaa.txt--color # finds lines beginning with BBB or BBB from the file Aaa.txt . grep ' [0-9] ' aaa.txt--color # Find rows containing numbers from file Aaa.txt grep-p ' \d ' aaa.txt--color # Find rows with numbers from File Aaa.txt,-p means regular expressions using Perl     \d represents a number, \d represents a non-numeric\s represents a space or tab, \s means not a space or tab\w represents any character (uppercase and lowercase letters, numbers, underscores), and \w is not a letter, number, or underscore.
Note: ^ appears in [] outside the words ' start ', if it appears in [] the words ' negative 'grep ' ^[^bb]: ' Aaa.txt--color # finds lines from file aaa.txt that do not start with B or B, and are followed by two characters .
8. \< matches the beginning of a word
grep ' \<tom ' aaa.txt--color # Find the line from the file aaa.txt that starts with Tom .
9. \> matches the beginning of a word
grep ' \>tom ' aaa.txt--color # finds the line from the file aaa.txt that the word ends with Tom .
10. Match a word, not part of a string
grep ' \btom\b ' aaa.txt--color # Find the line that contains the word tom from the file Aaa.txt, if it's atomb or something like that, ignore it..
11. Use \ (\) to label, the back of the reference to use \ n (is a number), \1 means the first one is caused by the
grep ' \ (tom\) ... \1 ' aaa.txt--color # looks for Tom from the file Aaa.txt, and it's followed by three characters, then there's a Tom line. For example: Tomxxxtom
\{n\} indicates that the preceding character will appear n times
grep ' tomx\{3\} ' aaa.txt--color # find tomx from File Aaa.txt, x to appear 3 times, such as: Tomxxxgrep ' tomx\{3,\} ' aaa.txt--color # find tomx from file Aaa.txt, x must appear at least three times, such as tomxxx or tomxxxxgrep ' tomx\{3,4\} ' aaa.txt--color # Find tomx from File Aaa.txt, X appears 3 or 4 times, such as tomxxx or tomxxxx
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.