grep命令常见用法

来源:互联网 发布:木村文乃 知乎 编辑:程序博客网 时间:2024/05/16 11:50

grep命令常见用法

(1)通过端口号找进程

占用8005端口的进程

netstat -anp |grep ":8005[ ]\+"|awk -F" "   {'print $7'}

 

占用49790端口的进程

netstat -anp |grep ":49790[ ]\+"|awk -F" "   {'print $7'}

 

占用48713端口的进程

netstat -anp |grep ":48713[ ]\+"|awk -F" "   {'print $7'}



 

netstat -anp |grep ":49790[ ]\+"|awk -F" "   {'print $6"\t"$7'}

 

(2)搜索包含关键字的文件

例如搜索包含字符串"syn.c" 的文件:

grep  "syn\.c" /etc/*

注意:句点需要转义

 

[root@iZ25tti3rxdZ tem]# grep  "syn\.c" ./* --color=auto

 

./b.txt:syn.c

(3)grep的通配符

.:任意一个字符

 

*:任意多个字符

Shell代码  收藏代码
  1. [root@iZ25tti3rxdZ tem]# cat b.txt   
  2. abc  
  3. def  
  4. syn.c  
  5. synxc  
  6. synxxxxxc  
  7. [root@iZ25tti3rxdZ tem]# grep  "syn\.c" ./* --color=auto  
  8. ./b.txt:syn.c  
  9. [root@iZ25tti3rxdZ tem]# grep  "syn.c" ./* --color=auto  
  10. ./b.txt:syn.c  
  11. ./b.txt:synxc  

 

 

(4)grep 匹配数字

netstat -anp |grep ":8005[ ]\+"|awk -F" "   {'print $6"\t"$7'}|cut -d"/" -f1|grep "[1-9]\+"

0 0
原创粉丝点击