Grep and Regular Expression

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.