The practice collection of regular expressions in Linux

Source: Internet
Author: User
Tags egrep

1. Find the IPV4 address of this machine in Ifconfig "NIC name" command result
Ifconfig | Head-n 2 |tail-1 |tr-s "" |cut-d ""-f3

2. Find out the maximum percentage value of partition space utilization
DF |tr-s "" |cut-d ""-f5

3, identify the user UID maximum user name, UID and shell type
cat/etc/passwd | Cut-d:-f1,3,7| Sort-nt:-k2 |tail-n 1

4. Find out the permissions of/tmp and display them digitally
stat/tmp | Head-n 4 |tail-n 1|cut-c10-13

5. Count the number of connections for each remote host IP currently connected to this machine, and sort from large to small (no image)
Cat/var/log/httpd/access_log |cut-d ""-f1 |sort |uniq-c |sort-n-R |head
6. A line that displays at least one whitespace character in the CentOS7/etc/grub2.cfg file, followed by a non-whitespace character
grep "^[[:blank:]]+"/etc/grub2.cfg |grep-v "^[[:space:] $"

7. Find the line that ends with ' LISTEN ' followed by any number of whitespace characters in the result of the "Netstat-tan" command
Netstat-tan |grep "Listen[[:blank:]
$"

8. Display user name and UID of all system users on CentOS7
CAT/ETC/PASSWD |cut-d:-f1,3 | Egrep-v "[0-9]{4,}"

9, add user Bash, Testbash, basher, SH, nologin (its shell is/sbin/nologin), find the/etc/passwd user name with the shell name of the line
grep "(^. ) \>.\<\1$ "/etc/passwd

10, take advantage of DF and grep, remove the disk partition utilization, and from large to small sort
DF |egrep "\<[0-9] %."-o|sort-nr

11. Display three user root, Mage, Wang Uid and default shell (C7 instead)
CAT/ETC/PASSWD |egrep "^ (root| C7) "|cut-d:-f1,3

12. 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
Egrep ". () "/etc/rc.d/init.d/functions

13. Use Egrep to remove the base name from the/etc/rc.d/init.d/functions (make a little speculative suspicion)
echo/etc/rc.d/init.d/functions | Egrep "[A-z]
$"

14. Use Egrep to remove the directory name of the path above
echo/etc/rc.d/init.d/functions | Egrep "/.*/"

15. Count the number of logins per host IP address that was logged in as root in the last command
last|cut-c23-38 |sort|uniq-c|sort-rn|head-n 1

16. Use extended regular expressions to represent 0-9, 10-99, 100-199, 200-249, 250-255, respectively
echo {1..255} |
Egrep "\<[0-9]\>"

Egrep "\<1[0-9]\>"

Egrep "\<1[0-9][0-9]\>"

Egrep "\<2[0-4][0-9]\>"

Egrep "\<25[0-5]\>"

17. Display all IPV4 addresses in ifconfig command result (write blindly)
Ifconfig |egrep "[0-9]{1,3}. [0-9] {1,3}. [0-9] {1,3}. [0-9] {1,3} "

18, this string: Welcome to Tiantianpaokuzhentmdehaowan each character in Linux to go to the weight and sort, repeat the number of rows to the front
echo Wecomele to Tiantianpaokuzhentmdehaowan Linux |grep-o. |sort |uniq-c |sort-nr

If there is any mistake, please criticize it.

The practice collection of regular expressions in Linux

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.