grep/pgrep/egrep/fgrep

本文详细介绍了Linux中grep、egrep、fgrep及pgrep命令的区别与用法,包括它们各自的特性、常用标志以及如何使用这些命令进行文件搜索。

 

Differences between grep, pgrep, egrep, and fgrep (Linux):

grep

grep is an acronym that stands for "Global Regular Expressions Print". grep is a program which scans a specified file or files line by line, returning lines that contain a pattern. A pattern is an expression that specifies a set of strings by interpreting characters as meta-characters. For example the asterisk meta character (*) is interpreted as meaning "zero or more of the preceding element". This enables users to type a short series of characters and meta characters into a grep command to have the computer show us what lines in which files match.

The standard grep command looks like:

grep <flags> '<regular expression>' <filename>

grep prints the search results to the screen (stdout) and returns the following exit values:

0    A match was found.
1    No match was found.
>1   A syntax error was found or a file was inaccessible 
     (even if matches were found).

Some common flags are: -c for counting the number of successful matches and not printing the actual matches, -i to make the search case insensitive, -n to print the line number before each match printout, -v to take the complement of the regular expression (i.e. return the lines which don't match), and -l to print the file names of files with lines which match the expression.

egrep

egrep is an acronym that stands for "Extended Global Regular Expressions Print".

The 'E' in egrep means treat the pattern as a regular expression. "Extended Regular Expressions" abbreviated 'ERE' is enabled in egrep. egrep (which is the same as grep -E) treats +?|(, and ) as meta-characters.

In basic regular expressions (with grep), the meta-characters ?+{|(, and ) lose their special meaning. If you want grep to treat these characters as meta-characters, escape them \?\+\{\|\(, and \).

For example, here grep uses basic regular expressions where the plus is treated literally, any line with a plus in it is returned.

grep "+" myfile.txt

egrep on the other hand treats the "+" as a meta character and returns every line because plus is interpreted as "one or more times".

egrep "+" myfile.txt

Here every line is returned because the + was treated by egrep as a meta character. normal grep would have searched only for lines with a literal +.

fgrep

fgrep is an acronym that stands for "Fixed-string Global Regular Expressions Print".

fgrep (which is the same as grep -F) is fixed or fast grep and behaves as grep but does NOT recognize any regular expression meta-characters as being special. The search will complete faster because it only processes a simple string rather than a complex pattern.

For example, if I wanted to search my .bash_profile for a literal dot (.) then using grep would be difficult because I would have to escape the dot because dot is a meta character that means 'wild-card, any single character':

grep "." myfile.txt

The above command returns every line of myfile.txt. Do this instead:

fgrep "." myfile.txt

Then only the lines that have a literal '.' in them are returned. fgrep helps us not bother escaping our meta characters.

pgrep

pgrep is an acronym that stands for "Process-ID Global Regular Expressions Print".

pgrep looks through the currently running processes and lists the process IDs which matches the selection criteria to stdout. pgrep is handy when all you want to know is the process id integer of a process. For example, if I wanted to know only the process ID of my mysql process I would use the command pgrep mysql which would return a process ID like 7312.

 

 

来自:http://superuser.com/questions/508881/what-is-the-difference-between-grep-pgrep-egrep-fgrep

转载于:https://www.cnblogs.com/itech/p/5535080.html

在指定目录中使用 `grep` 命令查找内容是一种常见的操作,尤其是在需要搜索文件中的特定字符串或模式时。以下是几种使用 `grep` 在 `/dir` 目录中进行搜索的方法。 ### 递归搜索目录中的所有文件 如果你想在 `/dir` 目录及其子目录中的所有文件中搜索某个特定的字符串,可以使用 `-r` 或 `--recursive` 选项: ```bash grep -r "search_string" /dir ``` 此命令会递归地搜索 `/dir` 目录下的所有文件,并输出包含 `search_string` 的行以及对应的文件名[^1]。 ### 显示行号和文件名 如果你希望在输出中看到匹配行的行号以及文件名,可以结合使用 `-n` 和 `-r` 选项: ```bash grep -rn "search_string" /dir ``` 这将显示每个匹配项所在的文件名、行号以及匹配的内容[^1]。 ### 忽略大小写 如果你希望忽略大小写来进行搜索,可以添加 `-i` 选项: ```bash grep -rni "search_string" /dir ``` 这样,`grep` 将不区分大小写地匹配 `search_string`,例如 `Search_String` 或 `SEARCH_STRING` 也会被匹配到[^1]。 ### 只显示包含匹配项的文件名 如果你只关心哪些文件包含了匹配项,而不关心具体的匹配内容,可以使用 `-l` 选项: ```bash grep -rl "search_string" /dir ``` 此命令将仅列出包含 `search_string` 的文件名[^1]。 ### 使用正则表达式 `grep` 还支持使用正则表达式进行更复杂的模式匹配。例如,要搜索以 `hello` 开头并以 `world` 结尾的行,可以这样做: ```bash grep -r "^hello.*world$" /dir ``` 这里的 `^` 表示行首,`$` 表示行尾,`.*` 表示任意数量的字符。 ### 结合 `find` 命令过滤特定类型的文件 有时候你可能只想在某些类型的文件中进行搜索,比如 `.txt` 文件。你可以结合 `find` 命令来实现这一点: ```bash find /dir -type f -name "*.txt" -exec grep -l "search_string" {} \; ``` 这条命令会在 `/dir` 目录下查找所有 `.txt` 文件,并在这些文件中搜索 `search_string`,最后只列出包含匹配项的文件名[^1]。 ### 总结 通过上述方法,你可以灵活地在指定目录中使用 `grep` 命令查找内容。根据不同的需求,可以选择适合的选项组合来提高效率。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值