The Text Processing tool and regular expression of the exercises

Source: Internet
Author: User
Tags egrep

1. Find all the IPv4 addresses of the ifconfig command results in this machine.

[[email protected] ~]# ifconfig         #centos6下eth0       Link encap:Ethernet  HWaddr 00:0C:29:BA:F9:36             inet addr:192.168.99.99   bcast:192.168.99.255  mask:255.255.255.0           inet6 addr: fe80::20c:29ff:feba:f936/64 scope:link           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1           RX packets:3670 errors:0 dropped:0  overruns:0 frame:0          tx packets:2963  errors:0 dropped:0 overruns:0 carrier:0           collisions:0 txqueuelen:1000           rx bytes:341489  (333.4  kib)   TX bytes:354417  (346.1 kib) lo         Link encap:Local Loopback             inet addr:127.0.0.1  Mask:255.0.0.0           inet6 addr: ::1/128 Scope:Host           UP LOOPBACK RUNNING  MTU:65536  Metric:1           rx packets:8 errors:0 dropped:0 overruns:0 frame:0           tx packets:8 errors:0 dropped:0  overruns:0 carrier:0          collisions:0  txqueuelen:0           rx bytes:600  (600.0 b)   TX bytes:600   (600.0&NBSP;B)           [[email protected] ~ ]# ifconfig         #centos7下eno16777736:  flags=4163<up, Broadcast,running,multicast>  mtu 1500        inet  192.168.99.100  netmask 255.255.255.0  broadcast 192.168.99.255         inet6 fe80::20c:29ff:fe01:b00c  prefixlen 64   scopeid 0x20<link>        ether 00:0c :29:01:b0:0c  txqueuelen 1000   (Ethernet)          RX packets 2804  bytes 242061  (236.3 kib)          rx errors 0  dropped 0  overruns 0  frame 0         TX packets 1855  bytes 201673  (196.9 kib)         tx errors 0  dropped 0 overruns  0  carrier 0  collisions 0lo: flags=73<UP,LOOPBACK,RUNNING>   mtu 65536        inet 127.0.0.1   Netmask 255.0.0.0        inet6 ::1  prefixlen  128  scopeid 0x10

2. Find out the maximum percentage of partition space usage.

[Email protected] ~]# dffilesystem 1k-blocks used Available use% mounted on/dev/sda2 41153856 2146548 36910 156 6%/tmpfs 953140 0 953140 0%/dev/shm/dev/sda1 1998672 40260 1853556 3%/boot[[e Mail protected] ~]# DF | Tr-s ' |cut-d '-f 5|tr-d%|sort-n|tail-1

3, identify the user UID maximum user name, UID and shell type

[Email protected] ~]# Getent passwd | Sort-t:-K 3-n | Cut-d:-F 1,3,7|TAIL-1

4. Find out the permissions of/tmp and display them digitally

[[email protected] ~]# stat /tmp  file:  '/tmp '   Size: 4096           Blocks: 8           IO Block: 4096   directoryDevice: 802h/2050d     Inode: 786433      Links: 7Access:  (1777/DRWXRWXRWT)   Uid:  (    0/    root)    Gid:  (     0/    root) context: system_u:object_r:tmp_t:s0access:  2016-07-11 19:00:12.017347722 +0800modify: 2016-08-08 18:58:01.445117085 +0800change:  2016-08-08 18:58:01.445117085 +0800 Birth: -[[email protected] ~]#  stat /tmp | head -4|tail -1|cut -d  ' ('  -f2|cut -d  '/'  - F1

5. Count the number of connections to each remote host IP currently connected to this machine, and sort by large to small.

[[email protected] ~]# netstat-nt|grep tcp|tr-s ' |cut-d '-F 5|cut-d:-F 1|UNIQ-C|SORT-NR

6. Display the line in the/proc/meminfo file that starts with the size s (requires: use two ways)

[Email protected] ~]# cat/proc/meminfo |grep-i ^s #方法1 [[email protected] ~]# cat/proc/meminfo |grep-e ' ^s|^s ' #方法2 [[email protected] ~]# cat/proc/meminfo |grep ' ^[ss] ' #方法3

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

[Email protected] ~]# CAT/ETC/PASSWD | Grep-v '/bin/bash$ '

8. Show user RPC default shell program

[Email protected] ~]# CAT/ETC/PASSWD | grep ' ^rpc\> ' |cut-d:-F 7

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

[Email protected] ~]# grep-e ' \<[1-9][0-9]\>|\<[1-9][0-9]{2}\> '/etc/passwd

10. 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

[[email protected] ~]# grep ' ^[[:space:]]\+[^[:space:] '/etc/grub2.cfg [[email protected] ~]# grep-e ' ^[[:space:]]+[^[: Space:] '/etc/grub2.cfg


11. Find the line that ends with "LISTEN" followed by any number of whitespace characters in the result of the "Netstat-tan" command

[Email protected] ~]# Netstat-tan | grep ' listen[[:space:]]*$ '

12. 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

[Email protected] home]# cat/etc/passwd |grep ' ^\<\ (. *\) \>.*\b\1\b$ '

13. Display three user root, Mage, Wang's UID and default shell

[[email protected] home]# grep-e ' ^ (root|mage|wang) '/etc/passwd | Cut-d:-f3,7

14. 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

[[email protected] home]# grep ' ^\<[[:alpha:]_]\+\> () '/etc/rc.d/init.d/functions

15. Use Egrep to remove the base name from the/etc/rc.d/init.d/functions

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

16. Use Egrep to remove/etc/rc.d/init.d/functions directory name

[Email protected] home]# echo "/etc/rc.d/init.d/functions" | GREP-E-O ' ^/.*/'

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

[Email protected] ~]# Last | GREP-E-O "^root\>.* (([0-9]|[ 1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]) \.) {3} ([0-9]| [1-9] [0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])) "| Tr-s ' |cut-d '-f3|uniq-c

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

[Email protected]/]# Cat/etc/init.d/functions | Tr-cs ' [: Alpha:] ' \ n ' |sort|uniq-c|sort-t '-k1-nr

19, from the file Haoma the regular expression of the identity card number

[Email protected]/]# grep-e ' [1-9][0-9]{5}1[0-9]{10}[0-9xx]| [1-9] [0-9] {5}200[0-9]{8}[0-9xx]| [1-9] [0-9] {5}201[0-6][0-9]{7}[0-9xx] ' Haoma

20, from the file Haoma use regular expression to represent the mobile phone number

[Email protected]/]# grep-e-o "(\+86)? 1[38][0-9]{9}|14[57][0-9]{8}|15[0-35-9][0-9]{8}|17[0678][0-9]{8}" Haoma

21, from the file Haoma The regular expression represents the mailbox

[[email protected]/]# grep-e-o ' [a-za-z0-9]+[[:alnum:]\.\_-]*@[a-za-z0-9]+[[:alnum:]\.\_-]* ' Haoma

22, from the file Haoma the regular expression of QQ number

[Email protected]/]# grep-e-o ' \b[1-9][0-9]{4,12}\b ' Haoma


This article is from the "Linux Operational Learning path" blog, please be sure to keep this source http://fengliang.blog.51cto.com/3453935/1835907

The Text Processing tool and regular expression of the exercises

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.