Summary of common grep commands

Source: Internet
Author: User

1. grep is the simplest method to use. match a word: grep word filename

2. Match: grep word filename1 filenam2 filename3 from multiple files

3. You can use a regular expression to match: grep-e pattern F1 F2 F3...

4. You can use-O to print only matching characters, as shown below:

[email protected]:command$ echo this is a line. | grep -E -o "[a-z]*\."line.
5. Print the rows other than matching rows, and use-V

[email protected]:command$ echo -e "1\n2\n3\n4" | grep -v -E "[1-2]"34

6. count the number of rows that match the string. Use-C

[email protected]:command$ echo -e "1111\n2222" | grep -E "[1-2]" -c2

7. If we count the number of times the string mode matches, we can combine-O and-C, as shown below:

[email protected]:command$ echo -e "1111\n2222" | grep -o -E "[1-2]"  | wc -l8

8. to display the row number, open-N, as shown below:

[email protected]:command$ echo -e "1111\n2222\n33333\n44444" | grep -n -E "3"3:33333

9. The-B option can print the offset (starting from 0) of the starting position of the row where the matched string is located. It is usually used with-o, as follows:

[email protected]:command$ echo "0123456789" | grep -b -o 44:4

10. When the string matches multiple files, the-L option prints only the file name.


11.-L is opposite to-L. Only file names that do not match are printed.

[email protected]:command$ cat test1.txtlinuxisfun[email protected]:command$ cat test2.txta verypopular os,linux[email protected]:command$ cat test3.txtwhatthefxxk[email protected]:command$ grep -l linux test1.txt test2.txt test3.txttest1.txttest2.txt[email protected]:command$ grep -L linux test1.txt test2.txt test3.txttest3.txt

12. Enable recursive search

[email protected]:command$ grep -n -R linux . ./test2.txt:5:linux./test1.txt:1:linux

13. Ignore case sensitivity:-I

[email protected]:command$ echo "HELLO WORLD" | grep -i "hello"HELLO WORLD

14. Match multiple string Modes

[email protected]:command$ echo "This is a line." | grep -e "This" -e "is" -e "line" -oThisisline

15. A matching style is provided with a separate file, and each matching style is taken as a line, as shown in the following example:

[email protected]:command$ cat pattern.txt1$23[email protected]:command$ cat num.txt 12345678910[email protected]:command$ grep -f pattern.txt num.txt 123

16. print the context information of the matching row, use-a n to print the information of the matching row and the last n rows, use-B n to print the information of the matching row and the first n rows, and use-C n, print the information of matching rows and the N rows before and after them. If Multiple matching rows exist, -- isolation is used. Example:

[email protected]:command$ seq 1 10 | grep 5 -A 35678[email protected]:command$ seq 1 10 | grep 5 -B 32345[email protected]:command$ seq 1 10 | grep 5 -C 32345678[email protected]:command$ echo -e "a\nb\nc\nd\na\nb\nc\nd\n" | grep a -A 2abc--abc

17. Use-Q to enter silent mode. In this mode, the grep command runs only to execute a conditional test, which is usually used in scripts. Perform the next step by checking its return value. Example:

[email protected]:command$ cat tmp.txthelloworld[email protected]:command$ cat tmp.csh#!/bin/bashif [ $# -ne 2 ]; thenecho "Usage: $0 match_pattern file_name"exitfimatch=$1file=$2grep -q $match $fileif [ $? -ne 0 ]; thenecho "$match not exist in $file"elseecho "$match exist in $file"fi[email protected]:command$ ./tmp.csh hello tmp.txthello exist in tmp.txt

18. When the-Z option is used to output a matching file name, it will end with/0 with xargs-0. For example, deleting a file that matches a certain pattern is as follows:

[email protected]:command$ ls -llrttotal 28-rw-rw-r-- 1 lichao lichao  13 Nov  1 20:38 test1.txt-rw-rw-r-- 1 lichao lichao  27 Nov  1 20:39 test2.txt-rw-rw-r-- 1 lichao lichao  14 Nov  1 20:39 test3.txt-rw-rw-r-- 1 lichao lichao  21 Nov  1 20:45 num.txt-rw-rw-r-- 1 lichao lichao   7 Nov  1 20:45 pattern.txt-rw-rw-r-- 1 lichao lichao  12 Nov  1 21:25 tmp.txt-rwxr-xr-x 1 lichao lichao 217 Nov  1 21:27 tmp.csh[email protected]ntu:command$ cat test1.txtlinuxisfun[email protected]:command$ cat test2.txta verypopular os,linux[email protected]:command$ grep "linux" * -lZ | xargs -0 rm[email protected]:command$ lsnum.txt  pattern.txt  test3.txt  tmp.csh  tmp.txt
Delete test1.txtand test2.txt in the linuxlinuxstring by using the command above.


19. exclude/include files or directories: 1) -- include *{. C ,. CPP} Only searches in the directory. C and. CPP file; 2) -- exclude "readme" exclude all README files 3) -- include-Dir search only in some Directories 4) -- exclude-Dir exclude some directories 5) -- exclude-from file: read the list of files to be excluded from the file.

[email protected]:test$ lsdir1  dir2  exclude.config  test1.txt  test2.doc  test3.word[email protected]:test$ cat test1.txt linux is fun[email protected]:test$ cat test2.doc wonderful os,linux[email protected]:test$ cat test3.word wonderful os,linux[email protected]:test$ ls dir1/test1.txt  test2.doc  test3.word[email protected]:test$ ls dir2/test1.txt  test2.doc  test3.word[email protected]:test$ cat exclude.config *.txt[email protected]:test$ grep "linux" -R -n . ./test2.doc:3:linux./test3.word:3:linux./test1.txt:1:linux ./dir2/test2.doc:3:linux./dir2/test3.word:3:linux./dir2/test1.txt:1:linux ./dir1/test2.doc:3:linux./dir1/test3.word:3:linux./dir1/test1.txt:1:linux [email protected]:test$ grep "linux" -R -n . --include *.txt --include *.doc./test2.doc:3:linux./test1.txt:1:linux ./dir2/test2.doc:3:linux./dir2/test1.txt:1:linux ./dir1/test2.doc:3:linux./dir1/test1.txt:1:linux [email protected]:test$ grep "linux" -R -n . --exclude *.txt --eclude *.docgrep: unrecognized option '--eclude'Usage: grep [OPTION]... PATTERN [FILE]...Try 'grep --help' for more information.[email protected]:test$ grep "linux" -R -n . --exclude *.txt --exclude *.doc./test3.word:3:linux./dir2/test3.word:3:linux./dir1/test3.word:3:linux[email protected]:test$ grep "linux" -R -n . --exclude-dir dir1./test2.doc:3:linux./test3.word:3:linux./test1.txt:1:linux ./dir2/test2.doc:3:linux./dir2/test3.word:3:linux./dir2/test1.txt:1:linux [email protected]:test$ grep "linux" -R -n . --exclude-dir dir1 --exclude-dir dir2./test2.doc:3:linux./test3.word:3:linux./test1.txt:1:linux [email protected]:test$ grep "linux" -R -n . --exclude-from exclude.config ./test2.doc:3:linux./test3.word:3:linux./dir2/test2.doc:3:linux./dir2/test3.word:3:linux./dir1/test2.doc:3:linux./dir1/test3.word:3:linux

It is a common grep option.


Note: Please indicate the source for reprinting.





Summary of common grep commands

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.