1、显示/etc/passwd
文件中不以/bin/bash结尾的行;
grep -v "/bin/bash$" /etc/passwd
讯享网
2、找出/etc/passwd文件中的两位数或三位数;
讯享网grep -E '\<[0-9]{2,3}\>' /etc/passwd
3、找出/etc/rc.d/rc.sysinit或/etc/grub2.cfg文件中,以至少一个空白字符开头,且后面非空白字符的行;
grep '^[[:space:]]\+[^[:space:]]' /etc/grub2.cfg
4、找出"netstat -tan"命令的结果中以’LISTEN’后跟0、1或多个空白字符结尾的行;
讯享网netstat -tan | grep 'LISTEN[[:space:]]*$'
5、找出/proc/meminfo文件中,所有以大写或小写S开头的行;至少有三种实现方式;
grep -i "^s" /proc/meminfo grep "^[sS]" /proc/meminfo grep -E "^(s|S)" /proc/meminfo
6、显示肖前系统上root、centos或user1用户的相关信息;
讯享网grep -E "^(root|centos|user1)\>" /etc/passwd
checkpid() __kill_pids_term_kill_checkpids() __kill_pids_term_kill() __pids_var_run() __pids_pidof() daemon()
讯享网grep -E -o "[_[:alnum:]]+\(\)" /etc/rc.d/init.d/functions
echo /etc/sysconfig/ | egrep -o "^/.*[^/]" | egrep -o "^/.*/" | egrep -o "^/.*[^/]"
9、找出ifconfig命令结果中的IP地址;
讯享网ifconfig | grep -A 2 ens33 | grep -w inet | grep -Eo 't.*n'| cut -d' ' -f2
10、添加用户bash, testbash, basher以及nologin(其shell为/sbin/nologin);而后找出/etc/passwd文件中用户名同shell名的行;
grep -E "^([^:]+\>).*\1$" /etc/passwd egrep "^(.*):.*\1$" /etc/passwd
试题要求
:按照题目要求写出你的正则表达式,并把输出结果贴出来。
讯享网file.txt文件内容: 48 Dec 3BC1977 LPSX 68.00 LVX2A 138 483 Sept 5AP1996 USP 65.00 LVX2C 189 47 Oct 3ZL1998 LPSX 43.00 KVM9D 512 219 dec 2CC1999 CAD 23.00 PLV2C 68 484 nov 7PL1996 CAD 49.00 PLV2C 234 483 may 5PA1998 USP 37.00 KVM9D 644 216 sept 3ZL1998 USP 86.00 KVM9E 23
4
grep -i '48' file.txt |wc -l grep -c 48 file.txt
2 显示含有“48”字符串的所有行的行号
讯享网 grep -n 48 file.txt grep -n '48' file.txt
grep "48\>" file.txt
4 抽取代码为4 8 4和4 8 3的城市位置
讯享网PS:使用[ ]来指定字符串范围 [root@localhost ~]# grep "48[43]" file.txt 483 Sept 5AP1996 USP 65.00 LVX2C 189 484 nov 7PL1996 CAD 49.00 PLV2C 234 483 may 5PA1998 USP 37.00 KVM9D 644 [root@localhost ~]# grep "48[4|3]" file.txt 483 Sept 5AP1996 USP 65.00 LVX2C 189 484 nov 7PL1996 CAD 49.00 PLV2C 234 483 may 5PA1998 USP 37.00 KVM9D 644
grep -v "^[48]" file.txt grep -c 48 file.txt
讯享网[root@XKWB5705 zhaoyj]# grep -v "^[4|8]" file.txt [root@XKWB5705 zhaoyj]# grep -v "^[48]" file.txt [root@XKWB5705 zhaoyj]# grep -v "^[4,8]" file.txt [root@XKWB5705 zhaoyj]# grep "^[^48]" file.txt
#这个是直接最简单的方法
grep -i "sept" file.txt
讯享网 grep -i "K...D" file.txt
grep "[A-Z]\{2\}..C" file.txt
9 查询所有以5开始以1996或1998结尾的所有记录
讯享网 grep "5..199[6,8]" file.txt grep '5..[1998|1996]' file.txt grep '5..[]' file.txt
10 取出所有三位数字的记录
grep "\<[0-9][0-9][0-9]\>" file.txt grep '\<[0-9]\{3\}\>' file.txt
11 取出所有字母与数字组合的记录
讯享网[root@localhost ~]# grep '[a-zA-Z0-9]' file.txt
12 取出所有非数字的记录
[root@localhost ~]# grep '[^0-9]' file.txt
13 取出所有非字母的记录
讯享网[root@localhost ~]# grep '[0-9]' file.txt

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/55569.html