grep与正则表达式02-相关练习题

(1)、显示/proc/meminfo 文件中以大小s 开头的行

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

(2)、显示/etc/passwd 文件中不以/bin/bash 结尾的行

#grep -v ‘\/bin\/bash$‘ /etc/passwd

(3)、显示/etc/passwd 文件中ID 号最大的用户的用户名及其shell

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

(4)、显示用户rpc 默认的shell 程序

方法1:# grep -w  ‘^rpc‘ /etc/passwd |awk -F: ‘{print $7}‘
方法2:# grep ‘\<rpc\>‘ /etc/passwd |cut -d: -f7

(5)、找出/etc/passwd 中的两位或三位数

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

(6)、显示/etc/grub2.cfg 文件中,至少以一个空白字符开头的且后面存非空白字符的行

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

(7)、找出"netstat -tan"命令的结果中以‘LISTEN‘后跟0个、1个或多个空白字符结尾的行

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

(8)、取出远程连接到本机的IP地址,并排序。

方法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
方法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
 体会这里者tail -n +3 这里表示从第3行开始打印最后的行(包括第3行)
方法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
体会这里的tr -s  表示将相邻两个字段之间的分隔符压缩为1个
 
 例如:
    [[email protected] ~]# cat  test.txt
     1:::::::::::2:::3
    [[email protected] ~]# tr -s ":" <test.txt
     1:2:3

(9)、找出/etc/passwd 文件中用户名和shell名一致的行

方法1: 
[[email protected] ~]# egrep "^(.*):.*\1$" /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
nologin:x:1004:1004::/home/nologin:/sbin/nologin
方法2:
[[email protected] ~]# egrep  ‘(\<[[:alpha:]]*\>).*\1$‘ /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
nologin:x:1004:1004::/home/nologin:/sbin/nologin

(10)、显示当前系统root 、mage 或wang 用户的UID和默认shell

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

(11)、找出/etc/rc.d/init.d/functions 文件中某单词(包括下划线)后面跟一个小括号的行

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

(12)、使用egrep 取出/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/
好好体会一下?的妙用
        
最简单的方法:basename、dirname
[[email protected] ~]# basename /etc/rc.d/init.d/functions
functions
[[email protected] ~]# dirname /etc/rc.d/init.d/functions
/etc/rc.d/init.d

(13)、使用egrep 取出上面路径的目录名

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

(14)、找出ifconfig 命令结果中本机的IPv4 地址

[[email protected] ~]# ifconfig eno16777736|head -2|tail -1|cut -d " " -f10
 172.16.251.126
[[email protected] ~]# ifconfig eno16777736|awk -F" " ‘NR==2{print $2}‘
 172.16.251.126
[[email protected] ~]# ifconfig eno16777736|awk -F "[ ]+" ‘NR==2{print $3}‘
 172.16.251.12

(15)、查出/tmp 权限以数字形式显示

使用stat命令

方法1:
[[email protected] ~]# stat /tmp |head -4|tail -1|egrep -o "[0-9]{4}"
 1777
方法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

未完待续...

时间: 2025-01-06 15:05:49

grep与正则表达式02-相关练习题的相关文章

grep与正则表达式

一.grep简介 二.基本正则表达式 三.egrep扩展正则表达式 四.其他文本及查看工具{wc,cut,sort,uniq,diff,patch} 五.练习用例 一.grep简介 文本处理工具: Linux上文本处理三剑客: grep:文本过滤工具(模式:pattern) grep:基本正则表达式,-E扩展,-F egrep:扩展正则表达式,-G基本,-F fgrep:不支持正则表达式, sed:stream editor,流 编辑器 awk:Linux上实现的为Gawk,GNU/awk,文本

Shell编程基础教程5--文本过滤、正则表达式、相关命令

5.文本过滤.正则表达式.相关命令    5.1.正则表达式(什么是正则表达式?正则表达式怎么进行匹配?常用命令)        简介:            一种用来描述文本模式的特殊语法            由普通字符(例如字符a到z)以及特殊字符(成为元字符,如/.*.?等)组成            匹配的字符串            文本过滤工具在某种情况下都支持正则表达式        基本元字符集及其含义            ^    只匹配行首,例子 ^a 表示匹配以a开头的

RH134-01 配合grep使用正则表达式

第二章.配合grep使用正则表达式 2.1 正则表达式基础 介绍shell中的常用正则表达式 ^   以什么开头             ^# $   以什么结尾              y$ .   匹配任意一个字符 .*匹配0个或若干个字符 h*匹配0个h或若干个h h+匹配1个或更多个h h?匹配0个或1个h h{2}  匹配 hh (两个hh) [abc]匹配a或b或c [a-z]匹配所有的小写字符 [A-Z]匹配大写字母 [a-Z]匹配所有字符 [0-9]匹配所有数字 练习:匹配 I

Linux &nbsp; &nbsp; grep命令,正则表达式

grep命令及正则表达式 grep命令 grep , egrep , fgrep grep,sed,awk 文本处理三剑客 grep: Global search REgular expression and Print out the line;全面查找正则表达式并将匹配到的行显示出来; 正则表达式 正则表达式主要应用对象是文本,因此它在各种文本编辑器场合都有应用;许多程序设计语言都支持利用正则表达式进行字符串操作; 主流的正则引擎又分为三类:DFA;传统型NFA;POSIX NFA; DFA

tr命令的使用及相关练习题

tr命令相关选项: tr-转换或删除字符 常用选项: -c:取字符集的补集 -d:删除匹配的指定字符集中的字符 -s:把连续重复的字符以单独一个字符表示 -t:先删除第一字符集较第二字符集多出的字符 \\:反斜杠 \a:响铃 \b:退格 \n:换行 \r:回车 [:alnum:] :所有的字母和数字                                                 [:alpha:] :所有的字母                                   

grep及正则表达式 含(含断言)

grep和正则表达式 1.grep:Global search REgular expression and Print out the file 作用:文本搜索工具,根据用户指定的模式对目标文件逐行进行匹配,打印匹配到的行 模式:由正则表达式字符及文本字符编写的过滤条件 格式: grep [options] pattern file[...] --color=auto 对匹配到的文本着色显示 -v 显示不能够被pattern匹配的行 -i 忽略file中文件字符的大小写 -n 显示匹配的行号

grep与正则表达式,grep、egrep和fgrep

grep用法详解:grep与正则表达式 首先要记住的是: 正则表达式与通配符不一样,它们表示的含义并不相同!正则表达式只是一种表示法,只要工具支持这种表示法, 那么该工具就可以处理正则表达式的字符串.vim.grep.awk .sed 都支持正则表达式,也正是因为由于它们支持正则,才显得它们强大:1基础正则表达式grep 工具,以前介绍过.grep -[acinv]   '搜索内容串'   filename-a 以文本文件方式搜索-c 计算找到的符合行的次数-i 忽略大小写-n 顺便输出行号-v

grep及正则表达式随笔

grep [acivn] [--color=auto] '查找字符串' filename -a:将binary文件以text文件的方式查找数据 -c:计算找到'查找字符串'的次数 -i:忽略大小写 -v:反向选择 -n:将查找结果列出行号 --color=auto:查找结果中关键字着色 **********grep使用例子********** (1)列出一个目录下目录的名称: ll |grep '^d' |awk '{print $9}' 其中awk中$后面是列的位置 (2)列出一个文件,去除空

grep以及正则表达式

正则表达式是基本的文本处理常识,理解和掌握好grep以及正则表达式对进行文本处理尤为重要 Grep 根据模式(文本字符和正则表达式的元字符组合而成匹配条件)搜索文本,并将符合模式的文本行显示出来. 1.1grep家族: grep:基本正则表达式使用的命令 egrep:扩展正则表达式使用的命令 fgrep(fast grep):不支持正则表达式,在没有正则表达式的字符串中的使用效率非常高,cup资源消耗少 1.2grep命令格式 grep  [options] PATTERN [FILE...]