Three Musketeers grep

working principle

  grep command in one or more search for a character mode file, if the pattern contains spaces, you must enclose it in quotes. grep command, the mode may be a quoted string, or a single word. Located mode after all the words are treated as a file name. grep output to the screen, it does not make any modifications or changes in the input file. grep returns an exit status of 0, indicating success. Exit status is 1, indicating not found. If you can not find the file specified, the exit status is 2. You can use a regular expression pattern.

  grep support common to all the regular expression, such as: basic regular, extended regular and Perl regular; by adjusting the corresponding parameter to specify the use of regular expressions, also called derived grep, such as: grep using -e parameter will grep into egrep, grep will use the -f argument becomes fgrep; of course, a regular expression can be turned off effect.

 

grammar

grep [OPTION]... PATTERN [FILE]...

 

parameter

Regular support

description

-E,--extended-regexp Open extended regular mode, the equivalent of using the egrep command
-F,--fixed-strings Close regular expression character string regarded as the direct matching PATTERN
-G,--basic-regexp By default, enable basic regular expression
-P,--perl-regexp Open perl-compatible regular mode, use perl language in regular matches, I do not represent, and therefore does not involve
-e,--regexp=PATTERN This option is for the "-" is not supported by default because the grep pattern contains - match "." Use -e to avoid error.
-f,--file=FILE The PATTERN written to the specified file, this option, you can call the file PATTERN to match the target file
-i,--ignore-case Match, ignore case be
-w,--word-regexp The PATTERN as a word (before and after the letter is not a non-ending), equivalent to PATTERN added to the "\ bPATTERN \ b" to delimitation.
-x,--line-regexp Pattern Matching entire row
-v,--invert-match Excluded PATTERN, displaying the remaining content is not matched to the line

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Regular refer to : https://www.cnblogs.com/guge-94/p/10678890.html

 

Improve performance options

description

-c,--count PATTERN to match the number of successful content
-r,--recursive Recursive queries matching sub-directory file, when the target file type is a directory; -R option is -r way links
-s, --no-messages Does not output an error message
-m,--max-count=NUM The maximum number of output lines matched to the content, such as three lines comprising the PATTERN, only two output lines, can -m 2
-n,--line-number Show Line Numbers
-H,--with-filename The output file name, before the contents of the line, when the target file is more than the default
-h,--no-filename No output filename
-o,--only-matching PATTERN to match only output is the content rather than the default output the entire line
-q,--quiet Information is not normal output
-L When the target file is grep multiple files, the results are output in the "File name: content matching rows" form. -L only will not contain matching lines of output file name. And -v similar.

 

 

 

 

 

 

 

 

 

 

 

 

Content control line description
--color Implemented {auto never | | always} disposed highlight color mode and can be used
-B,--before-context=NUM The first few lines of print match
-A,--after-context=NUM After a few lines of print match
-C,--context=NUM Print a few lines before and after the match

 

 

 

 

 

 

 

 

 

 

 

Examples

1, the output line b in the same file in a file

grep -f a b

 

2, the output b in a document file different rows

grep -v -f a b

 

3, a plurality of pattern matching

echo "a bc de" |xargs -n1 |grep -e 'a' -e 'bc'

 

4, remove blank lines or blank lines http.conf file number starting with #

grep -E -v "^$|^#" /etc/httpd/conf/httpd.conf

 

5, the beginning of the match case-insensitive word

echo "A a b c" |xargs -n1 |grep -i a

 

6, show only the matching string

echo "this is a test" |grep -o 'is'

 

7, the output of the matching results of the first five

seq 1 20  |grep -m 5 -E '[0-9]{2}'

 

8, how many rows match statistics

seq  1  20   | grep -c -E ' [0-9] {2} '

 

9, b character matches the beginning of the line

echo "a bc de" |xargs -n1 |grep '^b'

 

10, line matches the end of de characters and output matching lines

echo "a ab abc abcd abcde" |xargs -n1 |grep -n 'de$'

 

11, recursive search / etc directory that contains the conf file extension ip

grep -r '192.167.1.1' /etc --include *.conf

 

12, exclude search bak file suffix

grep -r ' 192.167.1.1 ' / opt --exclude * .bak

 

13, to exclude from a file in the file

grep -r '192.167.1.1' /opt --exclude-from file

 

14, 41 or 42 matching digital

seq  41  45 | grip -E ' 4 [12] '

 

15, matching at least two characters

seq  13 | grep -E ' [0-9] {2} '

 

16, matching at least two-character words, word up to 3 characters

echo "a ab abc abcd abcde" |xargs -n1 |grep -E -w -o '[a-z]{2,3}'

 

17, matching all IP

ifconfig |grep -E -o "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"

 

18, the matching result, and print the row 3

seq 1 10 |grep 5 -A 3

 

19, print matching results and the first three rows

seq 1 10 |grep 5 -B 3

 

20, the matching result and print lines before and 3

seq  1  10 | grip  5 -C 3

 

21, no error output

grep -s 'a' abc

 

22, the normal output is not displayed

grep -q 'a' a.txt

 

23, search for content in Gzip compressed file

zgrep -i error /var/log/syslog.2.gz

Guess you like

Origin www.cnblogs.com/guge-94/p/11008858.html