“每天一命令之grep”的版本间的差异

来自linux中国网wiki
跳到导航 跳到搜索
 
(未显示同一用户的14个中间版本)
第1行: 第1行:
  
 
=常用参数=
 
=常用参数=
 +
==shell从字符串中提取子串(正则表达式) ==
 
<pre>
 
<pre>
 +
#grep的-o选项,可以只打印匹配的部分,否则会打印整行。
 +
root@agent1:~# echo "libgcc-4.8.5-4.h5.x86_64.rpm" | grep -Eo "[0-9]+\.[0-9]+.*x86_64"
 +
4.8.5-4.h5.x86_64
 +
 +
#sed使用\1反向引用前面匹配的组。
 +
#但是sed没有只显示匹配部分的功能,会显示整行,所以采用的思路是将整行替换为子串,比较麻烦
 +
root@agent1:~# echo "libgcc-4.8.5-4.h5.x86_64.rpm" | sed -r "s/libgcc-([0-9]+\.[0-9]+.*)\.rpm/\1/g"
 +
4.8.5-4.h5.x86_64
 +
 +
</pre>
 +
[https://www.cnblogs.com/jmliao/p/11808592.html shell从字符串中提取子串]
 +
 +
<pre>
 +
#子匹配,把匹配到的打印出来
 +
cat test.txt | grep -E "'.*REDIS_PASSWORD'" -o
 +
 +
 +
cat test.txt |grep -E ":.*REDIS_PASSWORD=" -o | sort  | uniq -c
 +
 +
#eg
 +
查看 所有databases.php 里REDIS_PASSWORD 的
 +
 +
find ./ -type f  -name  "database.php" | xargs grep "REDIS"  >> redisvar.txt
 +
 +
[root@prod-access nginx]# head redisvar.txt
 +
./service/sscf-course-server/server.course.com/config/database.php:            'host' => env('REDIS_HOST', '127.0.0.1'),
 +
./service/sscf-course-server/server.course.com/config/database.php:            'password' => env('REDIS_PASSWORD', null),
 +
./service/sscf-course-server/server.course.com/config/database.php:            'port' => env('REDIS_PORT', 6379),
 +
./service/sscf-course-server/server.course.com/config/database.php:            'host'    => env('AUTH_USER_REDIS_HOST', '127.0.0.1'),
 +
./service/sscf-course-server/server.course.com/config/database.php:            'port'    => env('AUTH_USER_REDIS_PORT', 6379),
 +
./service/sscf-course-server/server.course.com/config/database.php:            'database' => env('AUTH_USER_REDIS_DATABASE', 0),
 +
./service/sscf-course-server/server.course.com/config/database.php:            'password' => env('AUTH_USER_REDIS_PASSWORD', null),
 +
./service/cfb-question-server/question.service.cfb.com/vendor/laravel/lumen-framework/config/database.php:        'cluster' => env('REDIS_CLUSTER', false),
 +
 +
#得出所有的 REDIS_PASSWORD 变量
 +
[root@prod-access nginx]# cat  redisvar.txt  | grep -E "'.*REDIS_PASSWORD'" -o | sort |uniq -c
 +
      5 'password' => env('AUTH_AGENT_REDIS_PASSWORD'
 +
    10 'password' => env('AUTH_USER_REDIS_PASSWORD'
 +
      2 'password' => env('CRM_JWT_REDIS_PASSWORD'
 +
    77 'password' => env('REDIS_PASSWORD'
 +
      1 'password'  => env('REDIS_PASSWORD'
 +
 +
 +
 
#多个and  
 
#多个and  
 +
ps aux | grep 'nginx\|php-fpm'
 +
 +
 
ps -ef | grep  -E 'tomcat1 |tomcat2 |grep -v grep '
 
ps -ef | grep  -E 'tomcat1 |tomcat2 |grep -v grep '
 +
 +
 +
 +
grep -E 'dd|77' file
 +
dd
 +
77
 +
[root@evantestvm ~]# cat file
 +
dd
 +
ee
 +
44
 +
66
 +
77
 +
 +
 +
 +
  
 
grep -rn  sth  somefiles #比dat 好
 
grep -rn  sth  somefiles #比dat 好
第9行: 第73行:
 
##并  fstab 文件中 有  /data  和 UUID 关键词
 
##并  fstab 文件中 有  /data  和 UUID 关键词
 
grep  /data /etc/fstab | grep UUID  
 
grep  /data /etc/fstab | grep UUID  
 +
 +
 +
grep 同时满足多个关键字和满足任意关键字
 +
① grep -E "word1|word2|word3"   file.txt
 +
   满足任意条件(word1、word2和word3之一)将匹配。
 +
② grep word1 file.txt | grep word2 |grep word3  试了好像不行
 +
   必须同时满足三个条件(word1、word2和word3)才匹配。
 +
 +
 +
  -r, --recursive          等同于--directories=recurse
 +
  -i, --ignore-case        在模式和数据中忽略大小写 不区分大小写
 +
  -n, --line-number        输出的同时打印行号
 +
  -v: --invert-match 反转          显示没有匹配的行。 反选
 +
 +
 +
  
 
</pre>
 
</pre>
第19行: 第99行:
 
               non-word constituent character.  Similarly, it must be either at the end of the line or followed by a non-word  constituent  character.  Word-constituent  characters  are
 
               non-word constituent character.  Similarly, it must be either at the end of the line or followed by a non-word  constituent  character.  Word-constituent  characters  are
 
               letters, digits, and the underscore.  This option has no effect if -x is also specified.
 
               letters, digits, and the underscore.  This option has no effect if -x is also specified.
 +
 +
 +
 +
grep "but container not found" application* | grep -oP 'asnId: \K\d+(?= )' | sort --unique
 +
 +
第一个grep过滤出所有符合要求的行,第二个grep用regex格式得到匹配组,sort可以保证输出的匹配值是唯一的。至于\K还有( ?=)的意思可以参考这里:https://unix.stackexchange.com/questions/13466/can-grep-output-only-specified-groupings-that-match
 +
 +
原文链接:https://blog.csdn.net/weixin_42325834/article/details/110254058
 +
 +
 +
 +
 +
 
</pre>
 
</pre>
 +
 +
==过滤注释和空行==
 +
cat  /etc/zabbix/zabbix_agentd.conf | grep  -v \^# |  grep -v \^$
 +
 +
=see also=
 +
[https://blog.51cto.com/future/88653 grep 参数使用和实例]
 +
 +
[https://blog.csdn.net/weixin_34408717/article/details/85854069  grep和sed匹配多个字符关键字的用法]
 +
 +
[https://blog.csdn.net/mmbbz/article/details/51035401 Linux: grep多个关键字“与”和“或”]
 +
 +
 +
[https://blog.csdn.net/pan_tian/article/details/7685815  Linux/Unix下grep命令使用的几个例子[grep Examples<nowiki>]</nowiki>]
 
  [[category:shell]]
 
  [[category:shell]]

2022年12月8日 (四) 05:50的最新版本

常用参数

shell从字符串中提取子串(正则表达式)

#grep的-o选项,可以只打印匹配的部分,否则会打印整行。
root@agent1:~# echo "libgcc-4.8.5-4.h5.x86_64.rpm" | grep -Eo "[0-9]+\.[0-9]+.*x86_64"
4.8.5-4.h5.x86_64

#sed使用\1反向引用前面匹配的组。
#但是sed没有只显示匹配部分的功能,会显示整行,所以采用的思路是将整行替换为子串,比较麻烦
root@agent1:~# echo "libgcc-4.8.5-4.h5.x86_64.rpm" | sed -r "s/libgcc-([0-9]+\.[0-9]+.*)\.rpm/\1/g"
4.8.5-4.h5.x86_64

 

shell从字符串中提取子串

#子匹配,把匹配到的打印出来 
cat test.txt | grep -E "'.*REDIS_PASSWORD'" -o 


cat test.txt |grep -E ":.*REDIS_PASSWORD=" -o | sort  | uniq -c

#eg 
查看 所有databases.php 里REDIS_PASSWORD 的 

find ./ -type f  -name  "database.php" | xargs grep "REDIS"  >> redisvar.txt

[root@prod-access nginx]# head redisvar.txt 
./service/sscf-course-server/server.course.com/config/database.php:            'host' => env('REDIS_HOST', '127.0.0.1'),
./service/sscf-course-server/server.course.com/config/database.php:            'password' => env('REDIS_PASSWORD', null),
./service/sscf-course-server/server.course.com/config/database.php:            'port' => env('REDIS_PORT', 6379),
./service/sscf-course-server/server.course.com/config/database.php:            'host'     => env('AUTH_USER_REDIS_HOST', '127.0.0.1'),
./service/sscf-course-server/server.course.com/config/database.php:            'port'     => env('AUTH_USER_REDIS_PORT', 6379),
./service/sscf-course-server/server.course.com/config/database.php:            'database' => env('AUTH_USER_REDIS_DATABASE', 0),
./service/sscf-course-server/server.course.com/config/database.php:            'password' => env('AUTH_USER_REDIS_PASSWORD', null),
./service/cfb-question-server/question.service.cfb.com/vendor/laravel/lumen-framework/config/database.php:        'cluster' => env('REDIS_CLUSTER', false),

#得出所有的 REDIS_PASSWORD 变量 
[root@prod-access nginx]# cat  redisvar.txt  | grep -E "'.*REDIS_PASSWORD'" -o | sort |uniq -c
      5 'password' => env('AUTH_AGENT_REDIS_PASSWORD'
     10 'password' => env('AUTH_USER_REDIS_PASSWORD'
      2 'password' => env('CRM_JWT_REDIS_PASSWORD'
     77 'password' => env('REDIS_PASSWORD'
      1 'password'  => env('REDIS_PASSWORD'



#多个and 
ps aux | grep 'nginx\|php-fpm'


ps -ef | grep  -E 'tomcat1 |tomcat2 |grep -v grep '



 grep -E 'dd|77' file
dd
77
[root@evantestvm ~]# cat file
dd
ee
44
66
77





grep -rn   sth  somefiles #比dat 好

##并  fstab 文件中 有  /data  和 UUID 关键词
grep  /data /etc/fstab | grep UUID 


grep 同时满足多个关键字和满足任意关键字
① grep -E "word1|word2|word3"   file.txt
   满足任意条件(word1、word2和word3之一)将匹配。
② grep word1 file.txt | grep word2 |grep word3  试了好像不行 
   必须同时满足三个条件(word1、word2和word3)才匹配。


  -r, --recursive           等同于--directories=recurse
   -i, --ignore-case         在模式和数据中忽略大小写 不区分大小写
  -n, --line-number         输出的同时打印行号
   -v: --invert-match 反转          显示没有匹配的行。 反选 




find & grep 总结

精准 非贪婪匹配

 -w, --word-regexp         强制 PATTERN 仅完全匹配字词
              Select  only  those  lines containing matches that form whole words.  The test is that the matching substring must either be at the beginning of the line, or preceded by a
              non-word constituent character.  Similarly, it must be either at the end of the line or followed by a non-word  constituent  character.   Word-constituent  characters  are
              letters, digits, and the underscore.  This option has no effect if -x is also specified.



grep "but container not found" application* | grep -oP 'asnId: \K\d+(?= )' | sort --unique

第一个grep过滤出所有符合要求的行,第二个grep用regex格式得到匹配组,sort可以保证输出的匹配值是唯一的。至于\K还有( ?=)的意思可以参考这里:https://unix.stackexchange.com/questions/13466/can-grep-output-only-specified-groupings-that-match

原文链接:https://blog.csdn.net/weixin_42325834/article/details/110254058





过滤注释和空行

cat  /etc/zabbix/zabbix_agentd.conf | grep  -v \^# |  grep -v \^$

see also

grep 参数使用和实例

grep和sed匹配多个字符关键字的用法

Linux: grep多个关键字“与”和“或”


Linux/Unix下grep命令使用的几个例子[grep Examples]