文本处理CLI Manual
$grep
使用正则表达式或匹配字符串在文本文件与输出流中搜索内容。
命令语法POSIX Standard
grep [OPTIONS] PATTERN [FILE...]在交互式终端沙盒中实时运行
无需安装的客户端实时仿真环境。
bash — terminal
AST Sandbox (Free)
概述与工作原理
Grep 是 Linux/Unix 环境下最著名的文本过滤与正则检索工具,逐行扫描输入内容并输出符合条件的行。
常用参数与选项说明
| Flag / Option | Description |
|---|---|
| -i | Ignore case distinctions in both patterns and input data. |
| -n | Prefix each matched output line with its 1-based line number. |
| -v | Invert the match to print lines that do NOT match the pattern. |
| -r, -R | Recursively search all files under each directory. |
| -c | Suppress normal output and print only the count of matching lines. |
| -E | Interpret pattern as an Extended Regular Expression (ERE). |
实际生产案例与代码范例
#1Search for errors in system log files
grep -i -n "error" /var/log/syslogSearches /var/log/syslog for case-insensitive occurrences of "error" and prints line numbers.
#2Filter process list through a pipe
ps aux | grep nginxPipes process snapshot data and isolates lines containing the nginx web server process.
#3Count total failed authentication attempts
grep -c "Failed password" /var/log/auth.logOutputs the integer count of failed login attempts without listing every individual line.
相关 Linux 常用命令
$find文件操作
根据文件名、类型、大小或修改时间在目录层级中高效搜索文件与目录。
$cat文件操作
连接文件并在终端标准输出上打印文件内容。
$head文本处理
Output the first part (top lines) of text files.
$tail文本处理
Output the last part (bottom lines) of files or stream real-time logs.
$wc文本处理
Print newline, word, and byte counts for files.
$echo文本处理
Print text strings or write data to files using shell redirection.