2024年4月19日发(作者:)

Linux grep命令用法

grep用于查找文件中符合字符串的那行。e.g. grep -nr "network_ssl" ./ [查找当前

文件夹下所有文件内容,列出包含有 network_ssl该字串的行,并显示行号],那么你知

道Linux grep命令用法么?接下来是小编为大家收集的Linux grep命令用法,欢迎大家

阅读:

Linux grep命令用法

首先创建我们练习grep命令时需要用到的demo文件demo_file。

$ cat demo_file

THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.

this line is the 1st lower case line in this file.

This Line Has All Its First Character Of The Word With Upper Case.

Two lines above this line is empty.

And this is the last line.

1.从单个文件中搜索指定的字串

grep的基础用法是如下例的从指定的文件中搜索特定的字串。

语法:

grep "literal_string" filename

$ grep "this" demo_file

this line is the 1st lower case line in this file.

Two lines above this line is empty.

And this is the last line.

2. 在多个文件中检索指定的字串

语法:

grep "string" FILE_PATTERN

先拷贝demo_file为demo_file1。grep的结果在符合条件的行前将包括文件名。当

文件名包含元字符时,linux shell会将匹配的所有文件作为输入到grep中去。

$ cp demo_file demo_file1