Grep [acivn] [-- color = auto] 'query string' filename
-A: query the data of binary files as text files.
-C: calculates the number of times a 'query string' is found.
-I: case insensitive
-V: Reverse Selection
-N: list the row numbers of the search results.
-- Color = Auto: keyword coloring in search results
* ********* Grep example **********
(1) list the names of directories in a directory:
Ll | grep '^ d' | awk' {print $9 }'
In awk, $ is followed by the column position.
(2) list an object and remove blank lines:
Grep-V '^ $'/etc/httpd/CONF/httpd. conf
(3) Remove comment lines
Grep-V '^ $'/etc/httpd/CONF/httpd. conf | grep-V '^ #'
(4) Ignore the case and indicate the row number:
Dmesg | grep-in 'network'
(5) Search for a word that starts with M and ends with M.
Grep-In'm [A-Z] * m' regular_express.txt
* ********* Regular Expression basics **********
-- Word string
-- Char character
-- List Character Sequence
^ Word indicates that it starts with word
Word $ indicates ending with word
. Indicates that there must be an arbitrary character.
Char * Indicates zero or Infinitely multiple repeated char
\ Escape characters
[LIST] indicates selecting a character match. For example, a [BC] A indicates ABA & ACA
[Char1-char2] represents any consecutive character, char1 to char2, for example [A-Z] represents any lowercase letter
[^ List] indicates removing unwanted characters and indicates reverse selection. For example, [^ ABC] indicates removing rows containing ABC strings.
Char \ {n, m \} indicates n to M Char. escape characters are required.
Eg: a script to simulate service httpd status output
#!/bin/bash#The output of commond "service httpd status" or "/etc/init.d/httpd status"m=$(ps -ef |grep httpd |sed ‘s/^.*grep.*$//g‘ |sed ‘/^$/d‘)n=$(ps -ef |grep httpd |sed ‘s/^.*grep.*$//g‘ |sed ‘/^$/d‘ |grep root |grep /usr/sbin/httpd |awk ‘{print $2}‘)#n=$(ps -ef |grep httpd |grep -v grep |grep root |grep /usr/sbin/httpd |awk ‘{print $2}‘)#n=$(ps -ef |grep httpd |sed ‘/.*grep.*/d‘ |grep root |grep /usr/sbin/httpd |awk ‘{print $2}‘)if [ "$m" = "" ];then echo "httpd is stopped"else echo "httpd (pid "$n") is running..."fi
This article from the "Little cainiao" blog, please be sure to keep this source http://akiny09.blog.51cto.com/1299657/1429980