grep命令实例

  grep一般用于查找文件中含有某些字符串的行,其命名格式如下

 grep [OPTIONS] PATTERN [FILE...]

  

  下面例举grep在linux使用过程中其常用使用实例:

  1、grep递归查找/etc目录及其子目录中含有”user“字符的文件,并显示文件与其含有”user“字符串的行

grep -r "user" /etc

  2、只匹配一个单词,而不是最为单词的一部分去匹配 

grep -w user /etc/passwd

  3、查找两个字符串

grep "user|USER" /etc/passwd

  4、统计查到的函数

grep -c "user" /etc/passwd

  5、显示查到的行在文件中的行号

grep -n "user" /etc/passwd

  6、只显示不匹配的行

grep -v "user" /etc/passwd

  7、只输出包含的文件名而不输出文件行

grep -l "user" /etc/*

  

原文地址:https://www.cnblogs.com/kekukele/p/4842891.html