How to find lines containing a specific string in Linux?

In Linux, you can use the grep command to search for lines containing a specific string. The basic syntax for the grep command is as follows:

grep [选项] 字符串 文件名

The string is the string you want to search for, and the file name is the name of the file you want to search in.

Here are some commonly used options for the grep command:

  1. -i: ignore case sensitivity
  2. Search recursively in directories and their subdirectories.
  3. -l: List only the filenames containing the matching string
  4. -n: display the line number of the matching string

Here are a few examples:

  1. Search for lines containing a specific string in the file.
grep "字符串" 文件名
  1. Search for lines containing a specific string in multiple files.
grep "字符串" 文件名1 文件名2
  1. Recursively search for lines containing a specific string in both the directory and its subdirectories, and only list the file names that contain the matching string.
grep -r -l "字符串" 目录名
  1. Search for lines containing a string in a document and display the line numbers where the matching strings are found:
grep -n "字符串" 文件名

I hope the information above can help you!

Leave a Reply 0

Your email address will not be published. Required fields are marked *