grep and Regular Expressions 02-related exercises

Source: Internet
Author: User
Tags egrep

(1), display lines in the/proc/meminfo file that begin with size S

# grep-i ' ^s '/proc/meminfo


(2), display lines in the/etc/passwd file that do not end in/bin/bash

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

(3), display the user name of the user with the largest ID number in the/etc/passwd file and its shell

Method 1:# cat/etc/passwd|cut-d:-f3|sort-n|tail-1|xargs getent passwd|cut-d:-f1,7nfsnobody:/sbin/nologin Method 2: # Sort-t: -k3-n/etc/passwd|tail-1|cut-d:-f1,7nfsnobody:/sbin/nologin

(4), display the user RPC default shell program

Method 1:# grep-w ' ^rpc '/etc/passwd |awk-f: ' {print $7} ' method 2:# grep ' \<rpc\> '/etc/passwd |cut-d:-f7

(5), find out the number of two or three digits in the/etc/passwd

# grep-o ' [0-9]\{2,3\}* '/etc/passwd

(6), a line that displays at least one whitespace character in a/etc/grub2.cfg file, followed by a non-whitespace character

# egrep ' ^[[:space:]]+[^[:space:]] '/etc/grub2.cfg

(7), find the result of the "Netstat-tan" command with ' LISTEN ' followed by 0, 1 or more whitespace characters end of the line

# netstat-tan|grep "listen[[:space:]]*$"


(8), remove the remote connection to the native IP address, and sort.

 method 1:[[email protected] ~]# netstat -tnp|awk -f   "[ :]+"   ' nr>2{print $6} ' |uniq -c|sort -r 2 172.16.251.121 1  172.16.250.14 Method 2:[[email protected] ~]# netstat -tnp|tail -n +3|awk - f  "[ :]+"   ' {print $6} ' |uniq -c|sort -r 2 172.16.251.121 1  172.16.250.14  experience here tail -n +3  This means that the last line (including line 3rd) is printed starting from line 3rd 
Method 3:[[email protected] ~]# netstat-tnp|tail-n +3|cut-d:-f2|tr-s "" |cut-d ""-f2|uniq-c|sort-r 2 172.16.251.121 1 172.16.250.14 experience here the tr-s means that the delimiter between the adjacent two fields is compressed to 1 for example: [[email protected] ~]# cat test.txt 1:::::::::::2:::3 [[EMA Il protected] ~]# tr-s ":" <test.txt 1:2:3


(9), find the user name and shell name consistent line in the/etc/passwd file

Method 1: [[email protected] ~]# egrep "^ (. *):. *\1$"/etc/passwdsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/ Sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltnologin:x:1004:1004::/home/nologin:/sbin/nologin Method 2:[[email Protected] ~]# egrep ' (\<[[:alpha:]]*\>). *\1$ '/etc/passwdsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0: Shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltnologin:x:1004:1004::/home/nologin:/sbin/nologin


(10), displays the UID of the current system root, mage, or Wang user and Default Shell

# egrep "^root\>|^mage\>|^wang\>"/etc/passwd|cut-d:-f3,7


(11), find a word in the/etc/rc.d/init.d/functions file ( including underscores) followed by a parenthesis line

# egrep ' [[: Alpha:]0-9_]+\ (\) '/etc/rc.d/init.d/functions


(12), use Egrep to remove its base name in/etc/rc.d/init.d/functions

[[email protected] ~]# echo "/etc/rc.d/init.d/functions" |egrep-o ' [[: Alpha:].] +/?$ ' Functions[[email protected] ~]# echo "/etc/rc.d/init.d/" |egrep-o ' [[: Alpha:].] +/?$ ' init.d/have a good experience? The simplest method: BaseName, Dirname[[email protected] ~]# basename/etc/rc.d/init.d/functionsfunctions[[ Email protected] ~]# DIRNAME/ETC/RC.D/INIT.D/FUNCTIONS/ETC/RC.D/INIT.D


(13), use Egrep to remove the directory name of the above path

# echo "/etc/rc.d/init.d/functions" |egrep-o "^.*/"

(14), find the IPV4 address of the ifconfig command result in the machine

[[email protected] ~]# ifconfig eno16777736|head-2|tail-1|cut-d ""-f10 172.16.251.126[[email protected] ~]# ifconfig Eno16777736|awk-f "" ' Nr==2{print $ {} ' 172.16.251.126[[email protected] ~]# ifconfig eno16777736|awk-f "[]+" ' Nr==2{pri NT $172.16.251.12} '

(15), Detect/TMP permissions to display in digital form

Using the Stat command

Method 1:[[email protected] ~]# stat/tmp |head-4|tail-1|egrep-o "[0-9]{4}" 1777 method 2:[[email protected] ~]# stat/tmp |head- 4|tail-1 Access: (1777/DRWXRWXRWT) Uid: (0/root) Gid: (0/root) [[email protected] ~]# stat/tmp |head- 4|tail-1|cut-d/-f1|cut-d "("-f2 1777

Not to be continued ...


This article from "Hello,linux" blog, reproduced please contact the author!

grep and Regular Expressions 02-related 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.