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:
- -i: ignore case sensitivity
- Search recursively in directories and their subdirectories.
- -l: List only the filenames containing the matching string
- -n: display the line number of the matching string
Here are a few examples:
- Search for lines containing a specific string in the file.
grep "字符串" 文件名
- Search for lines containing a specific string in multiple files.
grep "字符串" 文件名1 文件名2
- 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 "字符串" 目录名
- 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!