Linux through grep command to retrieve a few lines before and after the contents of the file and specify the content

Original link:  https://www.linuxidc.com/Linux/2017-11/148390.htm

 

Linux system to search, find content file, is generally the most common grep command, in addition to the egrep command, while vi command also supports the contents of the file retrieval.

  1. Search for a file which contains the string

    Format: grep "search string" filename1

    E.g:

    grep "0101034175" /data/transaction.20170118.log
  2. Retrieving a string in multiple files

    Format:

    1. grep "is looking for a string t" filename1 filename2 filename3 ...
    2. grep "search string" * .log
  3. The number of lines displayed in the retrieved content file, the parameters may be used -n

    Format: grep -n "search string" * .log

  4. Ignore case a problem retrieving the need, you can use the parameter "-i"

    Format: grep -i "search string" * .log

  5. Find the line does not match the specified string from the file contents

    Format: grep -v "search string" filename

  6. Search to find the number of matching rows:

    Format:

    1. grep -c "search string" filename
    2. grep "search string" filename | wc -l
  7. Recursive search for a directory and all files in subdirectories

    Format: grep -r "search string" file directory

  8. Gets which files contain content search and lists the file name

    Format: grep -H -r "search string" directory | cut -d: -f1 [| uniq]

    E.g:

    grep -H -r "v\$temp_space_header" /u01/app/Oracle/product/11.1.0/dbhome_1/rdbms/admin/ | cut -d: -f1
    
    grep -H -r "v\$temp_space_header" /u01/app/oracle/product/11.1.0/dbhome_1/rdbms/admin/ | cut -d: -f1 | uniq
  9. Get content with the entire search character match

    Format: grep -w "search string" filename

  10. Grep command in conjunction with the find command to achieve Federated Search

    Command Format: find -name '* .sql' -exec grep -i 'is retrieved content' {} \; -print

    E.g:

    find . -name '*.sql' -exec grep -i 'v\$temp_space_header' {} \; -print

Linux Command - text file operations grep   http://www.linuxidc.com/Linux/2015-12/126259.htm

grep regular expression  http://www.linuxidc.com/Linux/2015-09/123035.htm

Linux file format with the regular expression processing command (awk / grep / sed)  http://www.linuxidc.com/Linux/2013-03/81018.htm

Linux foundation of grep and regular expressions  http://www.linuxidc.com/Linux/2016-10/136250.htm

Examples 14 grep command  http://www.linuxidc.com/Linux/2015-05/117626.htm

Linux text processing tools like grep and regular expressions and egrep to distinguish grep   http://www.linuxidc.com/Linux/2016-08/134046.htm

Linux Foundations - regular expressions (grep, sed, awk)   http://www.linuxidc.com/Linux/2017-05/144221.htm

Linux text processing tools and grep   http://www.linuxidc.com/Linux/2017-03/142277.htm 

Regular expressions with grep and sed   http://www.linuxidc.com/Linux/2017-10/147269.htm

grep command Chinese Manual (info grep translation)   http://www.linuxidc.com/Linux/2017-09/146645.htm

grep command series: how to find files based on file content in UNIX  http://www.linuxidc.com/Linux/2016-01/128017.htm

 

other:

If you want to get a file in the last few lines, using the tail command can easily achieve. But in some cases, you will need to use the function as described in the title.

Let me give a concrete example: when you print the log stack information, that is, after the string "exception" in a few lines, but this is a whole section in the middle of the file, if this part of the data is extracted out of it?

Use the grep command can be relatively easy to achieve the goal, the specific use as follows:

01
$ grep-A | B the n-"Key" File
where:

A: represents a string after context after
B: represents before context string before
n-: To obtain the number of text lines Number Line
Key: string to be searched
file: a file name
such as the above-mentioned examples, the following may be used command to obtain the elastic constants:

0
grep -B 10 -A 10 -i "Key" filename
-i represents the case is ignored.

Few lines of text to other commands may be acquired for further extraction or piped.

for example:

To get behind the log contains the "exception" string of 20 rows stack information: 

command:

grep -A 20 -i "exception" filename
as follows:

 


What if we want to get the first 10 rows and 10 rows after abnormal log, not with -A and -B, use the following command on it:

-10 -i grep "Exception" filename
---------------------
Disclaimer: This article is the original article CSDN bloggers' crayon zlx ", following the CC 4.0 by -sa copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/zhaolixin0726/article/details/53123453

Guess you like

Origin www.cnblogs.com/dfyg-xiaoxiao/p/11345044.html
Recommended