Text-processing Exercises

Source: Internet
Author: User
Tags egrep

I. Text-processing Exercises

1. Find all IPv4 address of native in ifconfig command result

[Email protected] zhang]# ifconfig |tr-s "" |head-2 |tail-1| Cut-d ""-f3

2. Find out the maximum percentage value of partition space utilization

[Email protected] zhang]# DF |cut-c46-48 |SORT-NR |head-1

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

[Email protected] zhang]# cat/etc/passwd |sort-t:-k3-n |tail-1 |cut-d:-f1,3,7

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

[Email protected] zhang]# stat/tmp |head-4|tail-1 |cut-d "/"-f1 |cut-d "1"-f2

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

[Email protected] zhang]# netstat-nt |cut-d:-f2 |tr-s "": "|cut-d:-f2 |sort-r |uniq-c


Two. grep exercises

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

[Email protected] zhang]# grep-i "^s"/proc/meminfo or [[email protected] zhang]# grep "^[ss]"/proc/meminfo or [[Email prote CTED] zhang]# egrep "^ (s|s)"/proc/meminfo

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

[[email protected] zhang]# grep "/bin/bash$"/etc/passwd

3. Show user RPC default shell program

[[email protected] zhang]# grep "^rpc\>"/etc/passwd |cut-d:-f7

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

[[email protected] zhang]# 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

[Email protected] zhang]# grep-n "^[[:space:]]\+[^[:space:]]*"/etc/grub2.cfg

6. Find the line ending with ' LISTEN ' followed by 0, 1, or more whitespace characters in the result of the "Netstat-tan" command

[Email protected] zhang]# Netstat-tan |grep-n "\<listen[[:space:]]*$"

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

[[email protected] testdir]# grep "^\ ([[: Alnum:]]\+\):. *\1$"/etc/passwd or [[email protected] testdir]# grep "^\ (\<.*\ >\). *\1$ "/etc/passwd



Three. Egrep Practice


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

[Email protected] zhang]# egrep "(Root|zhang)"/etc/passwd |cut-d:-f3,7

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

[[email protected] zhang]# egrep "^\< ([[: Alpha:]]|_) +\>\ (\). *"/etc/rc.d/init.d/functions

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

[[email protected] zhang]# echo "/etc/rc.d/init.d/functions" |egrep-o "[[: alpha:]]+$] or [[email protected] zhang]# Echo "/etc/rc.d/init.d/functions" |egrep-o "[^/]+$"

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

[Email protected] zhang]# echo "/etc/rc.d/init.d/fun.ctions" |egrep-o "^.*/"

5. Count the number of logins for each remote host IP address logged in as Zhang

[Email protected] zhang]# w|egrep "^zhang" |sort-nr |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

"([0-9]| [1-9] [0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5] "

7. Display all IPV4 addresses in ifconfig command results

[Email protected] packages]# ifconfig |egrep "\< ([0-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \.) {3} ([0-9]| [1-9] [0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \> "



Three. Job

1. Use regular expressions to represent IP addresses

[Email protected] packages]# ifconfig |egrep "\< ([0-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \.) {3} ([0-9]| [1-9] [0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \> "

2. Use regular expression to indicate mobile phone number 11 13 17 15 18

grep "\<1[13578][0-9]{9}\>"


3. Use regular expressions to represent the ID number 18

Egrep "\< ([1[1-5]) | ( 2[1-3]) | (3[1-7]) | (4[1-6]) | (5[0-4]) | (6[1-5]) | (71|81|82)) ([0-9]) {4} (19|20) ([0-9]) {2} ((0[1-9]) | (1[0-2])) (0[1-9]| ([0-9]) | (2[0-9]) | (3[0-1])) ([0-9]) {3} ([0-9]| X) \> "filename

4. Use regular expressions to express mailboxes

[[email protected] testdir]# egrep "\b[[:alnum:]]+ (-|_) *[[:alnum:]]\[email protected] ([[: Alnum:]]+\.) +[[:alnum:]]+ "Mail


Four. Sed exercises

1. Delete all whitespace characters from the beginning of lines in the/etc/grub2.conf file that begin with whitespace

[Email protected] ~]# sed-r ' [email protected]^[[:space:]][email protected]@ '/etc/grub2.cfg

2. Delete all # and white space characters at the beginning of the line beginning with #, followed by at least one white-space character, in the/etc/fstab file

[[Email protected] ~]# sed ' s/^#[[:space:]]\+//'/etc/fstab

3. Add # At the beginning of each line of/etc/passwd

[[Email protected] ~]# sed ' s/^/#/'/etc/passwd

4. Add # to the beginning of the line in the/etc/fstab file that does not begin with #

[[Email protected] ~]# sed ' s/^[^#]/#/'/etc/fstab or [[email protected] ~]# sed-r ' s/^ ([^#].*]/#\1/'/etc/fstab

5, processing/etc/fstab path, use sed command to take out its directory name and base name

echo "/ETC/FST/SD" | Sed-r ' [email protected] (. *)/([^/]+/?) @\[email protected] ' take the base name Echo '/ETC/FST/SD ' | Sed-r ' [email protected] (. */) ([^/]+/?) [Email protected]\[email protected] ' fetch directory name or [[email protected] ~]# echo '/ETC/FSTAB/ASDD ' |sed-r ' [email protected][^/]+\/ [Email protected]@ ' Fetch directory name [[email protected] ~]# echo "/ETC/FSTAB/ASDD" |sed-r ' [email protected]^.*/@@ ' Take base name

6. Use SED to remove the IPv4 address of the ifconfig command

[Email protected] ~]# ifconfig |sed-n ' 2p ' |sed-r ' [email protected]^[[:space:]][email protected]@ ' |sed-r ' [email pro Tected]*[email protected]@ '

7. Statistics of all RPM files in the package directory on the CentOS installation CD. Number of repetitions of the second-to-last field

[email protected] packages]# ls *.rpm |sed-r ' [email protected]*\. (. *) \[email Protected]\[email protected] ' |sort |uniq-c

















This article is from the "zhang1003995416" blog, make sure to keep this source http://1003995416.blog.51cto.com/10482168/1836114

Text-processing 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.