grep Command: Search String in File

To search for a specific string in a file using the shell, you can use the grep command.

Syntax is as follows:

grep "字符串" 文件路径

Suppose you want to search for the string “hello” in the file example.txt, you can use the following command:

grep "hello" example.txt

To ignore case sensitivity, you can use the -i parameter.

grep -i "hello" example.txt

To search for multiple files, you can separate the file paths with spaces.

grep "hello" file1.txt file2.txt

To display matching line numbers, you can use the -n parameter.

grep -n "hello" example.txt

The grep command has many other parameters and uses, you can use the man grep command to view more detailed information.

bannerAds