Common usage of grep command

Common usage of grep command

(1) Find the process by port number

Processes occupying port 8005

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

 

Process occupying port 49790

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

 

Process occupying port 48713

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



 

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

 

(2) Search for files containing keywords

For example to search for files containing the string "syn.c":

grep  "syn\.c" /etc/*

Note: Periods need to be escaped

 

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

 

./b.txt:syn.c

(3) wildcards for grep

.: any character

 

*: any number of characters

[root@iZ25tti3rxdZ tem]# cat b.txt
abc
def
syn.c
synxc
synxxxxxc
[root@iZ25tti3rxdZ tem]# grep  "syn\.c" ./* --color=auto
./b.txt:syn.c
[root@iZ25tti3rxdZ tem]# grep  "syn.c" ./* --color=auto
./b.txt:syn.c
./b.txt:synxc

 

 

(4) grep matches numbers

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



 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326843419&siteId=291194637