grep full explanation

The regular used is the regular in POSIX format. \d cannot be used. Only abbreviations like [:digit:] can be used.

But I also don't recommend using the form [:digit:] [0-9] is not more direct \w use [a-zA-Z0-9] instead anyway to match letters or numbers or underscores according to your needs [\w or Chinese characters]

grammar

grep [-abcdDEFGHhIiJLlmnOopqRSsUVvwxZ] [-A num] [-B num] [-C[num]]
[-e pattern] [-f file] [–binary-files=value] [–color[=when]]
[–colour[=when]] [–context[=num]] [–label] [–line-buffered]
[–null] [pattern] [file …]

The difference from the find command is that grep is placed at the end and the find command is to put the folder at the top

Under normal circumstances grep is to retrieve the line matching the pattern in that file, the matching content does not contain \n, an empty statement matches each line, the matching line will be output to standard output

grep is used for simple pattern regex, egrep is used for extended regex, fgrep is faster than grep and egrep but uses compiled regex, pattern

zgrep, zegrep, zfgrep are the same as mentioned above, but for compress file


Below are the detailed options

  1. -A num , --after-context=num output num lines of
    demo after matching lines
tbapi/file1.txt:72059704415233307
tbapi/file1.txt-adklahda
--
tbapi/file2.txt:72059704415233307
tbapi/file2.txt-asdad
tbapi/file2.txt-adhasd
tbapi/file2.txt-dahs

General program information

  1. -V, --version View version

Matcher options

grep is used for simple patterns and basic regular expressions (BREs);
egrep can handle extended regular expressions (EREs)

  1. -E Enhanced regularization similar to egrex ERE
  2. -F –fixed-strings Understand Pattern as a fixed string
    instead of a regular expression, divided by \n, if one of them is satisfied, it will return
  3. -G , --basic-regex interprets pattern as an ordinary regular BRE This is the default option
  4. -P --perl-regex interprets Pattern as a Perl regular expression, PCRE is experimental and will report some errors

Matcher control

  1. -e Pattern , --regex=PATTERN Use PATTERN as pattern, multiple -e can be used to describe multiple different matching patterns. This option is especially useful when using pattern starts with "-"
  2. -f FILE, --file=FILE get matching data from FILE, multiple -f means to match data from multiple files instead of direct output
    grep "test" file*can be written as grep -f grep.cmd file*Write the command to the grep.cmd file
demo
cat > grep.cmd <<EOF
>test$
>^n
>san.*o
>EOF

grep -f grep.cmd file*
  1. -i , --ignore-case ignore pattern and file content case
  2. -v --invert-match take lines without a match
  3. -w –word-regexp only selects the entire words that match only if they are surrounded by characters that are not part of the word. The document is very obscure, that is, the word is right, and the components can be letters, numbers and underscores
  4. -x --line-regex match only records that match exactly one line
  5. -y is exactly the same as -i

output control

  1. -c –count [suppress] suppress, suppress the original line output, and instead return the number of lines matching the file

  2. --color[=WHEN] The optional values ​​are never, always, auto. When outputting, the output color can use the GREP_COLORS environment variable to define the highlighted color. The outdated GREP_COLORS is outdated but can still be used

  3. -L --files-without-match output only filenames with matching filenames
  4. -l only show matching filenames where the lookup is at the first match of each file
  5. -m NUM, –max-count=NUM There are two cases. One is that the input value is a file [multiple], and each file only returns up to NUM lines. If the data passed by the pipeline finally matches NUM lines
  6. -o only output the matching part of the line that is not empty The ls | grep "file"returned value is only file without -o option will have the whole file name
  7. -q --quiet, --silence do not print anything, even if an error occurs
  8. -s, --no-messages suppress non-existent or unreadable file errors

output line prefix control

  1. -b, --byte-offset Print the 0-based byte offset before the file name [position information starting from 0, for example, the first character is 0, the second is 1, and so on, but it looks like on mac Useless】
  2. -H --with-filename This is the default option if there are multiple files to print out the filename for each matching line
  3. -h , --no-filename When there are multiple files, do not print out a file in the file name. This is the default option
  4. --label=LABEL gzip -cd foo.gz | grep --label=foo -H somethingThis is more useful when zgrep [not too familiar with]
  5. -n --line-number Display line number
    See the demo below

    martin@ubuntu:~/Desktop/shell_test$ ls | sort
    abc.txt
    awk.txt
    file1.txt
    file2.txt
    file3.txt
    list_salary.awk
    mail_test.php
    martin@ubuntu:~/Desktop/shell_test$ ls | grep -n "file"
    3:file1.txt
    4:file2.txt
    5:file3.txt
  6. -T --initial-tab makes the actual printed content separated by a tab
    Often used with -b -n -H
    demo
martin@ubuntu:~/Desktop/shell_test$ ls | grep -nT "file"
   3   :file1.txt
   4   :file2.txt
   5   :file3.txt

control of printed content

  1. -A NUM --after-context=NUM print the lines after the match

  2. -B NUM–before-context=NUM

  3. -C NUM–context=NUM means NUM lines before and after

    Note all capitals

file or directory selection

  1. -a --text treat a binary file as a file with the --binary-files=text option
  2. --binary-files=TYPE There are several ways 1. The default is binary to output content according to binary 2. without-match grep If a binary file is not matched, there will be one more line than normal * Similar to option -I 3. text Equivalent to -a, read binary as a file and grep may interpret non-text characters as line terminators. For example, pattern "." will not match the empty byte because the empty byte will be used as a newline.

  3. -D ACTION --device=ACTION If the input is a device, FIFO or socket use ACTION to handle it. By default read these devices will be read as normal files and skip will be skipped

  4. -d ACTION –directories=ACTION If the input file is a directory, use ACTION to process it. The default is to read read and treat the directory as a normal file. If it is skip, it will be skipped by default. If it is recurse, all files will be output recursively.
  5. --exclude=GLOB skip GLOB-compliant files can use *,? […] and \ escape characters
  6. --exclude-from=FILE Read skipped files from file can also use the syntax in --exclude GLOB
  7. –Exclude-dir = DIR
  8. -I and --binary-files=without-match are actually ignoring files
  9. --include=GLOB only look in files that satisfy GLOB
  10. -r --recursive searches the folder recursively, but only retrieves the files of symbolic links in the command. If there is no file, it will default to the current folder
  11. -R is similar to -r but also includes symbolic links

other options

  1. --line-buffered use line buffering on output
  2. -U –binary Under normal circumstances, grep will determine whether the file type is binary or text according to the 32k read of the file. This option directly treats the file as binary. It will only work under MS-DOS and MS-Windows.
  3. -z --null-data use NUL in ASCII for newline recognition

regular expression

grep understands three types of regular expressions
1. basic BRE
2. extended ERE
3. perl PCRE

example

http://www.cnblogs.com/sky-heaven/p/6034777.html

Your own demo
1. View the information about the special status in the xml file

grep -ohE '<sid>[0-9]{12,}</sid><status>TRADE_NO_CREATE_PAY</status>' 2017-11-*

By default, the file name will be output -h do not output the file name -o means only output the matching content -E means use the enhanced regular expression

example

The function of grep -F is similar to fgrep syntax and grep is similar below is the example

# test.txt 
Demo Hello.*? World
This is second line
This is third line

dingmac@Downloads$ egrep -oni "hello.*?" test.txt
1:Hello
dingmac@Downloads$ fgrep -oni "hello.*?" test.txt
1:Hello.*?

The -F parameter indicates that the pattern will be interpreted as a simple text and not interpreted as a regular

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325881711&siteId=291194637