Linux starter grep Regular expression case

Source: Internet
Author: User
Tags grep regular expression egrep


grep Regular Expression case

Case one:

1. Display the line in the/proc/meminfo file that begins with the size S; (Requires: use two ways)

#grep-i ' ^s '/proc/meminfo#grep ' ^[ss] '/proc/meminfo#grep-e ' ^ (s| S) '/proc/meinfo

2. Display lines in the/etc/passwd file that do not end in/bin/bash

#grep-V '/bin/bash$ '/etc/passwd

3. Show user RPC default shell program

#grep ' ^\<rpc\> '/etc/passwd | Cut-d:-f7

4. Find out the two-bit or three-digit number in/etc/passwd

#grep ' \<[0-9][0-9]\?\> '/etc/passwd#grep ' \<[0-9]\{2,3\}\> '/etc/passwd

5. Display a line in a/etc/grub2.cfg file that starts with at least one whitespace character and that is followed by a non-whitespace character

#grep ' ^[[:space:]]\+.*[^[:space:]] '/etc/grub2.cfg

6. Find the line ending with ' LISTEN ' followed by any whitespace character in the result of the "Netstat-tan" command

#netstat-tan '. *listen[[:space:]]*$ '

7. Add user bash, Testbash, basher, and Nologin (whose shell is/sbin/nologin), then find the line in the/etc/passwd file with the name of the shell

#grep ' ^\<\ (. *\) \>.*/\1$ '/etc/passwd#grep ' ^\<\ (. *\) \>.*\<\1\>$ '/etc/passwd


Case TWO:

1. Displays the UID and default shell of the current system root, mage, or Wang user

#grep-E ' ^ (root|mage|wang) \> '/etc/passwd |cut-d:-f7

2. Find the line at the beginning of the/etc/rc.d/init.d/functions file that has a word (including an underscore) followed by a parenthesis

#grep-e-o ' ^\<[[:alnum:]_]+\>\ (\) '/etc/rc.d/init.d/functins

3. Use Egrep to remove its base name in/etc/rc.d/init.d/functions

#echo "/etc/rc.d/init.d/functions" | GREP-E-O ' [^/]+/?$ '

4. Use Egrep to remove the directory name of the path above

#echo "/etc/rc.d/init.d/functions" | Grep-e ' (/.*/) '

5. Count the number of logons per remote host IP address logged in as root

#last | Grep-o ' root\>.*\ ([[:d igit:]]\{1,3\}\.\) \{3\}[[:d igit:]]\> ' | Tr-s ' | Cut-d '-f3 | Uniq-c

6. Use extended regular expressions to represent 0-9, 10-99, 100-199, 200-249, 250-255, respectively

#grep-E ' \< ([0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]) \> '

7. Display all IPV4 addresses in ifconfig command results

#ifconfig | Grep-o ' \ (\ ([0-9]\|1[0-9]\|1[0-9]{2}\|2[0-4][0-9]\|25[0-5]\) \.\) \{3\}\ ([0-9]\|1[0-9]\|1[0-9]{2}\|2[0-4][0-9]\| 25[0-5]\) '


Case THREE:

4, take the local IP address

#ifconfig | grep ' inet addr ' | GREP-E-O ' ([[1-9]|1[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-4]) \.) {3} ([1-9]|1[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-4]) '

5, the use of the number of partition utilization

DF | Grep-o ' \<[0-9]\+% '

6. Count the number of occurrences of each word in the/etc/init.d/functions file and display it from high to low by frequency

Non-Regular:

#cat/etc/init.d/functions | Tr-cs ' [: Alpha:] ' |  Tr ' \ n ' | Sort | uniq-c | Tr-s ' | Sort-t "-K 1NR

Regular plus substitution:

#cat/etc/init.d/functions | Tr ' _ ' | Grep-o ' \<[a-za-z]\+\> ' | Sort | uniq-c | Tr-s ' | Sort-t "-k2 NR

Pure is:

#cat/etc/init.d/functions | Grep-o--color ' \<\ ([a-za-z_]\) \+\> ' | grep--color ' \<[#a-za-z]\+\> ' | Sort | uniq-c | Grep-o "[0-9]\+[[:space:]][a-za-z]\+" | Sort-t '-k1-nr

7,/etc/rc.d/init.d/functions or/etc/rc.d/init.d/functions/"Fetch directory Name

#DIR = "/etc/rc.d/init.d/functions" #DIR1 = "/etc/rc.d/init.d/functions/" #echo ${dir} | grep ' \ (/.*/\) ' #echo ${dir1} | Tr '/' | Cut-d '-f1-$[' echo $DIR | tr '/' ' | wc-w ' | TR '/' Echo ${dir} | Grep-o '/.*[^/] ' | grep--color-o '/.*/'

The extension uses the SED command:

echo "/sdsdf/sgdsg/sdgg/fgdfhdfh/" | Sed ' s/[^\/]\+\/\?$//g '













This article is from the "Excalibur Trunks-kun" blog, please be sure to keep this source http://mengzhaofu.blog.51cto.com/10085198/1836489

Linux starter grep Regular expression case

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.