Linux command grep Regular expression

Source: Internet
Author: User
Tags grep regular expression gopher expression engine egrep

in Linux there are three swords that deal with text, and what I'm going to say now is the use of the grep regular expression, which is just a notation, as long as the tool supports this notation, The tool can then handle the string of regular expressions. vi grep, awk, sed, etc. all support regular Expressions . . Regular expressions have a basic regular expression and an extended regular expression.

grep is the basic expression by default.

Basic Regular Expressions: default number of matches: greedy mode, as much as possible to match.

Extended Regular Expressions: Based on basic regular expressions, add some functionality, basically the same.

1. grep command

grep "Parameters" search for files

Parameters:

--color=auto: Highlighting the matched string

-V: Display mode does not match rows

-I: ignore character case ;

-O: Show only strings that can be matched to a pattern

-Q: silent mode

-E: Using extended regular Expressions

-N: output line number by the way

The metacharacters of the basic regular expression:

Character Matching:

.: matches any single character;

[]: matches any single character within the specified range;

[^] : matches any single character within the specified range;

Number of occurrences: used to specify the number of times after the character;

*: any time;

\\? :0 or 1 times;

\\+:1 or more times ;

\\{m\\}: Precisely limited to m times;

\\{m,n\\}: at least m times, up to n times,[M,n]

\\{0,n\\}: up to n times;

\\{m,\\}: At least m times;

. *: matches any character of any length;

Location anchoring:

^: anchor at the beginning of the line, for the leftmost mode;

$: end of line anchoring; for the rightmost side of the pattern;

\\<,\\b: The first anchor of the word, the left side of the pattern used to denote the word;

\\> \\b: the ending anchor; the right side of the pattern used to represent the word ;

^$: blank line;

Group:\ \ (\ \)

The contents of the pattern in the grouped parentheses are recorded by the regular expression engine during execution, and the built-in variables are saved: The variables are \\1, \\2, ...

\\1: from the left, the first opening parenthesis, and the matching right parenthesis in the middle of the pattern match to the content;

Back reference: Use a variable to refer to the character that matches the pattern in the preceding grouping brackets;


2. Egrep command

Extend the metacharacters of regular expressions:

Character Matching:

.: any single character

[]:

[^]:

Number of matches:

*

?: 0 or 1 times;

+: more than 1 times;

{m}: exact match M times;

{M,n}: at least m times, up to n times;

Anchoring:

^: anchoring the beginning of the line

$: Anchor Line End

\\<, \\b

\\>, \\b

Group: ()


Title: Displays the lines in the/etc/passwd file that end with bash

-bash-4.1#grep--color ' bash$ '/etc/passwdroot:x:0:0:root:/root:/bin/bashoracle:x:3000:3000::/home/database:/bin/ Bash51cto:x:3001:3001::/home/51cto:/bin/bash

title: Displays the two-digit or three-digit number in the/etc/passwd file

-

Bash-4.1#grep--color ' \\b[[:d igit:]]\\{2,3\\}\\b '/ETC/PASSWDMAIL:X:8:12:MAIL:/VAR/SPOOL/MAIL:/SBIN/NOLOGINUUCP: x:10:14:uucp:/var/spool/uucp:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologingames:x:12:100:games:/ usr/games:/sbin/nologingopher:x:13:30:gopher:/var/gopher:/sbin/nologinftp:x:14:50:ftpuser:/var/ftp:/sbin/ Nologinnobody:x:99:99:nobody:/:/sbin/nologindbus:x:81:81:systemmessage Bus:/:/sbin/nologin

title: Displays lines with ' LISTEN' followed by 0 , 1, or more whitespace characters in the ' netstat-tan ' command result

-bash-4.1#netstat -tan |grep --color  ' listen[[:space:]]*$ ' tcp         0     0 0.0.0.0:111                 0.0.0.0:*           LISTEN     tcp         0     0 0.0.0.0:22                  0.0.0.0:*                    listen     tcp         0     0 127.0.0.1:631               0.0.0.0:*                    LISTEN     tcp         0     0 127.0.0.1:25                0.0.0.0:*                    listen


title: Add User bash,testbash,basher, and nologin user (nologin user's shell /sbin/nologin), and then find the line in the/etc/passwd file that has the same user name as its shell name

-bash-4.1#grep--color ' \ \ (bash\\). *\\1 '/etc/passwdbash:x:3002:3002::/home/bash:/bin/bashtestbash:x:3003:3003::/ Home/testbash:/bin/bashbasher:x:3004:3004::/home/basher:/bin/bash

-bash-4.1#grep--color ' ^\\ (nologin\\). *\\1 '/etc/passwdnologin:x:3005:3005::/home/nologin:/sbin/nologin

title: Displays the default shell and UID for Root, CentOS, or User1 users on the current system (Please create these users beforehand, if not present)

-bash-4.1#grep-e--color ' ^root|^centos|^user1 '/etc/passwd | Cut-d:-f1,3,7root:0:/bin/bashcentos:3006:/bin/bashuser1:3007:/bin/bash

title: Find A word in the/etc/rc.d/init.d/functions file (the middle of the word can be underlined) followed by a set of parentheses line

-bash-4.1#grep--color '. * () '/etc/rc.d/init.d/functionsfstab_decode_str () {checkpid () {__readlink () {__fgrep () {__ Umount_loop () {__umount_loopback_loop () {


title: Use echo to output a path, and then Egrep to find its path base name; further use Egrep to remove its directory name

-bash-4.1#echo/etc/sysconfig/network-scripts/ifcfg-eth0 |grep--color '. * '/etc/sysconfig/network-scripts/ Ifcfg-eth0-bash-4.1#echo/etc/sysconfig/network-scripts/ifcfg-eth0 |egrep--color-o ' ^/.*/'/etc/sysconfig/ network-scripts/

title: Find the number between 1-255 in the ifconfig command execution result

-

Bash-4.1#ifconfig | egrep --color ' \ \ ' Eth0      link encap: ethernet  hwaddr 00:0c:29:7e:e8:80            inet addr:192.168.1.100  Bcast:192.168.1.255  Mask:255.255.255.0           inet6 addr: fe80::20c:29ff:fe7e:e880/64scope:link           up broadcast running multicast   mtu:1500 metric:1          rx bytes :791817  (773.2 kib)   TX bytes:379603  (370.7 kib)            inet addr:127.0.0.1  Mask:255.0.0.0           inet6 addr: ::1/128 Scope:Host            Up loopback running  mtu:65536 metric:1


This article is from the "8430482" blog, please be sure to keep this source http://8440482.blog.51cto.com/8430482/1690356

Linux command grep Regular expression

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.