In Linux how to retrieve file contents in detail using the grep command

  • 2020-06-19 12:33:35
  • OfStack

preface

Linux system to search and find the contents of files, 1 is the most commonly used grep command, in addition to the egrep command, vi command also supports file content retrieval. Let's start with the details of how Linux USES the grep command to retrieve file contents.

The methods are as follows:

1. Search for strings in a file

Command format: grep "string to be found" filename1

Such as:


grep "0101034175" /data/transaction.20170118.log

2. Retrieve a string from multiple files

Command format:

grep "String t" filename1 filename2 filename3... grep "String to be found" *.log

3. Display the number of lines of the retrieved content in the file, using the parameter -ES33en

Command format: ES36en-ES37en "String to be found" *.log

4. The case problem should be ignored when retrieving. The parameter "-ES41en" can be used.

Command format: ES44en-ES45en "String found" *.log

5. Finds lines that do not match the specified string from the file contents

Command format: grep? v "found string" filename

6. Search and find the number of matching rows:

Command format:

grep? c "found string" filename grep "found string" file name | wc-l

Recursively search for all files in a directory and subdirectories

Command format: grep? r "found string" file directory

8. Get which files contain the search and list the file names

Command format: grep-H? r? | cut-d: -f1 [| uniq]

Such as:


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

Get the content that matches the entire search character

Command format: grep? w "found string" filename

10, grep command and find command combination, achieve joint retrieval

Command format: find . -name '*.sql' -exec grep -i ' Retrieved content ' {} \; -print

Such as:


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

The difference between Linux grep and find

Here are two different commands about the grep:

The grep command in the Linux system is a powerful text search tool that USES regular expressions to search for text and print out matching lines. grep is Global Regular Expression Print, which represents the global regular expression version and has permissions for all users.

While linux find (specific usage can reference here: https: / / www ofstack. com article / 108198. htm)

Functionality: Searches the directory structure for files and performs the specified action. This command provides quite a few lookup conditions and is very powerful.

Syntax: find start directory looking for conditional operations

The find command starts from the specified starting directory and recursively searches its subdirectories for files that satisfy the search criteria and take action on them.

So briefly, grep is the row that looks for a match, and find is the file that looks for a match

conclusion


Related articles: