Linux grep commands and common Regular Expressions

Source: Internet
Author: User

1 grep (General Regular Expression Parse) Command Format: grep [option] pattern fileoption: n (-- line-number)-display row I (-- ignore-case) -case-insensitive E (-- regexp = PATTERN)-use an extended regular metadatchset or use egrepl (-- files-with-matches) -list matching file names v (-- invert-match)-reverse matching r (-- recursive) -recursive search for all files in the directory. 2 common Regular Expressions: Basic metacharacter set $ grep-n ^ public t1.txt $ grep-n public $ t1.txt $ grep-n ^ $ t1.txt $ grep-n \ <public t1.txt $ grep-n' \ <public 't1.txt $ grep-n' public \> 't1.txt $ grep-n "\ <public" t1.txt $ grep-n ^. $ t1.txt $ grep-n 11 [MNX] t1.txt $ grep-n 11 [^ MNX] t1.txt $ grep-n go * d t1.txt double quotation marks differ from single quotation marks, double quotation marks cannot suppress \ and $ characters $ grep-n' \ 't1.txt $ grep-n "\" t1.txt extended metacharacters $ grep-nE go + d t1.txt $ grep -nE 10 {3} A t1.txt $ grep-nE "10 {2, 4} A" t1.txt $ grep-nE 10 \ {2, 4 \} A t1.txt $ grep-nE '11a | B 't1.txt $ grep-nE net \ (works \) * use the example in t1.txt3 grep to query multiple files with wildcards $ grep-n test *. txt reverse match $ ps aux | grep sshd | grep-v grep recursive search $ grep-nr telnet/etc only list matching file names $ grep-l test *. in txt 4 vi, use a regular expression to delete all characters from aa to zz in the current row: s/aa. * zz // replace and in the entire file with &: 1, $ s/and/\ &/g, insert new: 1 at the beginning of each row, $ s/^/new/Insert @: 2 at the end of row 2nd to row 5th, 5S/$/@/g remove all the numeric characters at the beginning of the line: s/^ [0-9] + // g example: Switch Parameter foo (, 2) foo (, 2) foo (x + 13, y-2, 10) foo (y-2, x + 13, 10) foo (bar (8), x + y + z, 5) foo (x + y + z, bar (8), 5 ): 1, $ s/foo (\ ([^,] * \), \ ([^,] * \), \ ([^ \)] *) /foo (\ 2, \ 1, \ 3)/g

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.