Linux - grep命令

基本用法

grep [选项...] 范本样式 [文件...]

grep [选项...] -e 范本样式 [文件...]

grep [选项...] -f 范本文件 [文件...]

常用参数

参数 说明
-i , --ignore-case ignore-case 忽略大小写
-n, --line-number 在输出结果前加入源文件行号 帮助定位
-c, --count 统计匹配的行数
-v, --invert-match 对输出结果取反
-E, --extended-regexp 扩展正则表达式 = egrep
-w, --word-regexp 精确匹配整个单词, 有-x参数时无效
-x, --line-regexp 精确匹配整行 = ^<u>PATTERN</u>$
-l, --file-with-matches 显示匹配文件的文件名, 不显示匹配的行
-L, --file-without-mathes 显示没有匹配文件的文件名
-h, --no-filename 显示匹配的行,但不显示文件名, 单个搜索文件时为默认参数
-H, --with-filename 显示匹配的行 + 文件名, 多个搜索文件时为默认参数
-s, --no-messages 不显示报错信息
-r, ----recursive 递归查询子目录
-m <u>NUM</u>, --max-count=NUM 最多匹配NUM次,并输出
-A <u>NUM</u>, --after-context=NUM 显示匹配行 + 它后面NUM行的内容
-B <u>NUM</u>, --before-context=NUM 显示匹配行前M行的内容 + 匹配行
-C <u>NUM</u>, --context=NUM 显示匹配行前M行的内容 + 匹配行 + 匹配行后面NUM行的内容 = -A NUM -B NUM

示例

准备测试环境

脚本 ./create_test_env.sh

#!/bin/sh

#create_test_env.sh

#create test.txt
cat > test.txt << EOF
TEST
this is a test
i am testing
#this is a comment line.

Testing testing
no one use it

nospacetesting

what if test is in the middle
#this is another comment line.
no keyword in this line
test
EOF

#make ./test_folder if not exists
destDir='./test_folder'
readonly destDir

if [ ! -d $destDir ]; then
    mkdir -p $destDir
fi

#copy test2 from test1 and copy them into ./test_folder
cp test.txt test2.txt
cp test*.txt $destDir

#create other_file.txt
echo  "12\n1\n232" > other_file.txt
$ tree
.
├── create_test_env.sh
├── other_file.txt
├── test2.txt
├── test_folder
│   ├── test2.txt
│   └── test.txt
└── test.txt
参数示例
  1. -n 参数

    匹配包含tes的行, 并显示其在输入文件/源文件的行号

    $ grep -n test test.txt 
    2:this is a test
    3:i am testing
    6:Testing testing
    9:nospacetesting
    11:what if test is in the middle
    14:test
    
  1. -c 参数

    $ grep -c test  test.txt
    6
    
  1. -i 参数

    匹配包含test(忽略大小写)的行

    $ grep -in test test.txt 
    1:TEST
    2:this is a test
    3:i am testing
    6:Testing testing
    9:nospacetesting
    11:what if test is in the middle
    14:test
    
  1. -v 参数

    取反 --- 匹配不包含test(忽略大小写)的行

    $ grep -vin test test.txt
    4:#this is a comment line
    5:
    7:no one use it
    8:
    10:
    12:#this is another comment line
    13:no keyword in this line
    
  1. -w 参数

    发现只有整个单词test被匹配, testing 则不行

    $ grep -wn test test.txt
    2:this is a test
    11:what if test is in the middle
    14:test
    
  1. -x 参数

    -x 匹配只有test的一整行 相当与 无参数匹配 ^test$

    $ grep -xn test test.txt
    14:test
    
    $ grep -n ^test$ test.txt 
    14:test
    
  1. -wx参数 同时存在

    -x参数优先级高, 同时存在会使-w失效, 应避免

    $ grep -wxn test test.txt 
    14:test
    
  1. -l 参数

    -l 参数显示包含匹配信息的文件的文件名, 一旦匹配成功,则停止对当前文件继续扫描

    如果扫描目录有有子目录会报错. 此时需要-s参数隐藏错误

    $ grep -l test *
    create_test_env.sh
    test2.txt
    grep: test_folder: Is a directory
    test.txt
    
  1. -s 参数

    $ grep -ls test *
    create_test_env.sh
    test2.txt
    test.txt
    
  1. -L 参数 对-l结果取反 => 显示没有匹配文件的文件名
```bash
$ grep -Ls test *    # * 为当前目录下的所有文件和文件夹
other_file.txt
test_folder
```
  1. -H 参数

    $ grep -H test test*txt
    test2.txt:this is a test
    test2.txt:i am testing
    test2.txt:Testing testing
    test2.txt:nospacetesting
    test2.txt:what if test is in the middle
    test2.txt:test
    test.txt:this is a test
    test.txt:i am testing
    test.txt:Testing testing
    test.txt:nospacetesting
    test.txt:what if test is in the middle
    test.txt:test
    
  2. -A, -B -C 参数

    $ grep -A2 space test.txt
    nospacetesting
    
    what if test is in the middle
    $ grep -B2 space test.txt
    no one use it
    
    nospacetesting
    $ grep -C2 space test.txt
    no one use it
    
    nospacetesting
    
    what if test is in the middle
    
  3. -r 参数

    $ grep -r space  *
    create_test_env.sh:nospacetesting
    test2.txt:nospacetesting
    test_folder/test2.txt:nospacetesting
    test_folder/test.txt:nospacetesting
    test.txt:nospacetesting
    
  4. -m 参数

    $ grep -m2 test test.txt
    this is a test
    i am testing
    
  1. -E 配合正则表达式

    $ grep -E '^test' test.txt
    test
    
    $ grep -E 'test$' test.txt
    this is a test
    test
    
    $ grep -E 'no.*test'  test.txt
    nospacetesting
    

一些练习题

  1. 去除空行

    $ grep -v '^$' test.txt 
    TEST
    this is a test
    i am testing
    #this is a comment line
    Testing testing
    no one use it
    nospacetesting
    what if test is in the middle
    #this is another comment line
    no keyword in this line
    test
    
  1. 去除注释

    $ grep -v '^#' test.txt 
    TEST
    this is a test
    i am testing
    
    Testing testing
    no one use it
    
    nospacetesting
    
    what if test is in the middle
    no keyword in this line
    test
    
  1. 显示/proc/meminfo文件中以不区分大小的s开头的行
$ grep -i '^s' /proc/meminfo 
  1. 显示/etc/passwd中以nologin结尾的行

    $ grep 'nologin$' /etc/passwd
    
  1. 显示/etc/fstab中以#开头,且后面跟一个或多个空白字符,而后又跟了任意字符的行

    $ grep -E '^# +.*' /etc/fstab
    
  1. 显示/etc/passwd中包含了:一个数字:(即两个冒号中间一个数字)的行

    $ grep :[0-9]: /etc/passwd
    
  1. 显示/boot/grub/grub.cfg文件中以一个或多个空白字符开头的行

    $ grep -E '^ +' /boot/grub/grub.cfg
    or 
    $ grep  -E '^[[:space:]]{1,}.*' /boot/grub/grub.cfg
    
  1. 显示/etc/inittab文件中以一个数字开头并以一个与开头数字相同的数字结尾的行

    $ grep -E '^([0-9]).*\1$' /etc/inittab
    
  1. ifconfig命令可以显示当前主机的IP地址相关的信息等,要求不包括127.0.0.1

    $ ifconfig eth0 | grep 'inet' | grep -v '127.0.0.1' | cut -d: -f10 | cut -d" " -f10
    
    
  1. 显示/etc/sysconfig/network-scripts/ifcfg-eth0文件中的包含了类似IP地址点分十进制数字格式的行;

    $ grep -E "([0-9]{1,3}\.){3}\.[0-9]{1,3}" /etc/sysconfig/network-scripts/ifcfg-eth0
    or
    $ ifconfig eth0 | egrep -o '([0-9]+\.){3}[0-9]+' | head -1
    

参考:
https://phoenixnap.com/kb/grep-command-linux-unix-examples

https://blog.51cto.com/tsoagta/1711832
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容