grep - 文本搜索工具

  • 原创
  • Madman
  • /
  • /
  • 0
  • 8709 次阅读

grep - 文本搜索工具-min.png

Synopsis: g/re/p (Globally search a Regular Expression and Print)是类Unix系统中一个强大的文本搜索工具,在给出文件列表或标准输入后,grep会逐行搜索,判断是否与指定的一个或多个正则表达式匹配,并只输出匹配(或者不匹配)的行或文本。grep使用基本正则表达式,egrep支持使用扩展正则表达式,灵活使用正则,配合grep可以实现文件内非常强大的文本搜索功能

1. 常用选项

grep的基本语法为grep [OPTIONS] PATTERN [FILE...],其中PATTERN是匹配的模式,需要说明的是grep支持使用基本正则表达式(BRE),egrep支持使用扩展正则表达式(ERE),而fgrep不支持正则,它搜索字符串而不是搜索匹配表达式的模式

1.1 -i, --ignore-case

忽略字符大小写

# grep 'NTP' /etc/passwd
没有结果

# grep -i 'NTP' /etc/passwd
ntp:x:38:38::/etc/ntp:/sbin/nologin

1.2 -v, --invert-match

反向匹配,即显示没有 '查找字符串' 的所有行

# grep -v '/sbin/nologin' /etc/passwd
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt

1.3 -c, --count

打印每个输入文件中包含匹配模式的行数

# grep -c 'nologin' /etc/passwd
23

或者:
# grep 'nologin' /etc/passwd | wc -l
23

1.4 -n, --line-number

打印匹配到的文本所在行号

# grep -E -n 'sshd|ntp' /etc/passwd
25:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
26:ntp:x:38:38::/etc/ntp:/sbin/nologin

1.5 -l, --files-with-matches

当匹配到的结果非常多时,可以使用-l只显示文件名

# find / -type f 2> /dev/null | xargs -n 10 grep -l '\*'
/boot/grub/splash.xpm.gz
/boot/grub2/i386-pc/acpi.mod
/boot/grub2/i386-pc/date.mod
... ...

1.6 -q, --quiet, --silent

静默模式,不输出任何信息,常用于脚本中条件测试,不需要输出的信息,如果匹配到则命令执行后的返回状态值为 0 ,否则为 1

# grep -q 'sshd\|ntp' /etc/passwd
# echo $?
0

# grep -q 'sshd|ntp' /etc/passwd
# echo $?
1

1.7 -r, --recursive

递归搜索

# grep -r -n 'sshd' /etc
... ...
/etc/group-:44:sshd:x:74:
/etc/gshadow-:44:sshd:!::
/etc/group:44:sshd:x:74:
/etc/gshadow:44:sshd:!::
/etc/passwd-:25:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
/etc/shadow-:25:sshd:!!:16955::::::
/etc/passwd:25:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
/etc/shadow:25:sshd:!!:16955::::::
... ...

1.8 -A NUM, --after-context=NUM

输出匹配行的后 NUM 行

分类: Linux
标签: egrep fgrep grep
  • Unbox your new gift of cash anallador5.temp.swtest.ru HG
  • Step in and claim your winnings cr678086.tw1.ru yZ
  • Free monetary gift with this update cr678086.tw1.ru 44
  • Get Grants and Monetary Awards anallador5.temp.swtest.ru Oy
  • Receive a personal gift of money cr678086.tw1.ru xE
  • Get Cash from Prize Draws anallador5.temp.swtest.ru Md
  • You've stumbled upon serendipitous cash cr678086.tw1.ru ha
  • Your cash offer will lapse shortly anallador5.temp.swtest.ru wp
  • Triumph and Get a Money Reward anallador5.temp.swtest.ru zk
  • Capture a Unique Bonus Opportunity anallador5.temp.swtest.ru d2
  • Take Your No Cost Money anallador4.temp.swtest.ru 0U
  • Get Paid Back on the Spot anallador4.temp.swtest.ru Gz
  • Receive a brand new cash present anallador4.temp.swtest.ru lo
  • Play games and win cash instantly anallador5.temp.swtest.ru Dr
  • Achieve and Get Paid Immediately anallador4.temp.swtest.ru z6
  • Enjoy a New Investor Bonus cr678086.tw1.ru T5
  • This is the last round of payouts anallador4.temp.swtest.ru 73
  • Cash prizes awarded in real time anallador5.temp.swtest.ru OG
  • Give us a like get a cash gift cr678086.tw1.ru Gs
  • Generate Income from Side Gigs anallador4.temp.swtest.ru 4B
  • We'll Bonus You for Beginning to Invest cr678086.tw1.ru IK
  • Don't wait take your instant cash prize cr678086.tw1.ru Pi
  • Access the bonus that's been reserved cr678086.tw1.ru g4
  • Heads up Shock funds have arrived cr678086.tw1.ru tC
未经允许不得转载: LIFE & SHARE - 王颜公子 » grep - 文本搜索工具

分享

作者

作者头像

Madman

如需 Linux / Python 相关问题付费解答,请按如下方式联系我

0 条评论

暂时还没有评论.

专题系列