grep 技巧10则
欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 warning:文中提到的选项如果posix未指定,会特殊说明。 1 多个关键字查找 1.1 关键字间 or i. 使用-e #包含了string.h或者stdlib.h的头文件grep -l -e 'string\.h' -e 'stdlib\.h' /usr/include/*.h
欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入
warning:文中提到的选项如果posix未指定,会特殊说明。
1 多个关键字查找
1.1 关键字间 or i. 使用-e
#包含了string.h或者stdlib.h的头文件grep -l -e 'string\.h' -e 'stdlib\.h' /usr/include/*.h
-e 选项还可以避免 关键字是-开头的导致选项解读失败。
ii. 使用元字符 \|
grep 'strint\.h\|stdlib\.h' /usr/include/*.h #使用-E 看着舒服grep -E 'string\.h|stdlig\.h' /usr/include/*.h
iii. 使用-f file cat >multi_pattern stdlib\.h string\.h grep -l -f multi_pattern /usr/include/*.h
1.2 关键字间 and i. 通过管道
#同时包含'hello','world'的行echo hello world | grep '\
ii. 通过正则 |
grep -E 'pattern1.*pattern2|pattern2.*pattern1'
如果包含2个关键字还好,要是n个就有n!种可能!
2. 单词匹配i. -w(gnu 选项)
grep -w 'main' /usr/include/*.h
这个-w很方便ii. \ grep '\
3.善用 -E -E选项启用 extended expression,正则写起来更加灵活#查看gcc帮助文件里两个the/that/and/or连在一起的行man gcc | grep -E '(\
使用-E让书写更方便,省去很多的\,同时功能更强大。
4 忽略大小写 -i #查看INT_MAX的值grep -i 'int_max' /usr/include/limits.h
-i与\n同时使用的乱象#匹配连续相同单词echo 'it IT' | grep -i -w -E '([a-z]+) \1' echo 'it IT' | grep -E -w '([a-zA-Z]+) \1'
这是两个相同的单词吗?是的,因为告诉grep不计大小写的!
有的时候不要光图方便会不准确。
5 递归查找 -r(posix 未说明)
#查看日志的错误信息grep -i -w -r -E 'error|failed|failure' /var/log |less
6 显示匹配行周围行 (posix 未说明)
B/A/C(before/after/context -B n -A n -C n
7 取反-v grep -v -w 'hello' filename
如果没有取反,世界将不再美丽
8 匹配数 -c echo aaaa | grep -c 'a'
这个输出是1!因为grep是行匹配的
9 输出文件名 -l
grep -l -r -i -w 'filename_max' /usr/include/*.h
10 只输出匹配部分-o (gnu 选项)
echo abcddf |grep -o 'dd'
可以用于验证自己写的正则
如果出现错误或不准确,感谢指出。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

VS Code One-step/Next step shortcut key usage: One-step (backward): Windows/Linux: Ctrl ←; macOS: Cmd ←Next step (forward): Windows/Linux: Ctrl →; macOS: Cmd →

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

Installing Git software includes the following steps: Download the installation package and run the installation package to verify the installation configuration Git installation Git Bash (Windows only)
