Grep和shell练习

1、显示/etc/passwd文件中以bash结尾的行;

[root@128 ~]# grep bash$ /etc/passwd

2、找出/etc/passwd文件中的三位或四位数;

[root@128 ~]# grep  -wE "[0-9]{3,4}" /etc/passwd

3、找出/etc/grub2.cfg文件中,以至少一个空白字符开头,后面又跟了非空白字符的行;

[root@128 ~]# grep -E "^[[:space:]]+[^[:space:]]" /etc/grub2.cfg 

4、找出"netstat -tan”命令的结果中,以‘LISTEN’后跟0个或多个空白字符结尾的行;

[root@128 ~]# netstat -tan | grep "LISTEN[[:space:]]*$"

5、找出"fdisk -l“命令的结果中,包含以/dev/后跟sd或hd及一个字母的行;

[root@128 ~]# fdisk -l | grep -E "/dev/(sd.|hd.)"

6、找出”ldd /usr/bin/cat“命令的结果中文件路径;

[root@128 ~]# ldd /usr/bin/cat | grep -oE "/[^[:space:]]+"
/lib64/libc.so.6
/lib64/ld-linux-x86-64.so.2

7、找出/proc/meminfo文件中,所有以大写或小写s开头的行;至少用三种方式实现;

[root@128 ~]# grep  "^[sS]" /proc/meminfo
[root@128 ~]# grep -i "^s" /proc/meminfo
[root@128 ~]# grep -E "^(s|S)" /proc/meminfo

 8、显示当前系统上root、centos或spark用户的相关信息;

[root@128 ~]# egrep  "(root|centos|spark)" /etc/passwd

 9、echo输出一个绝对路径,使用egrep取出其基名;

[root@localhost ~]#echo /mnt/sdc/ | grep -E -o "[^/]+/?$" | cut -d"/" -f 1

10、找出ifconfig命令结果中的1-255之间的整数;

[root@128 ~]# ifconfig | egrep -w "[0-9]{1}|[0-9]{2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5]"

11、找出系统中其用户名与shell名相同的用户。

[root@128 ~]# grep -E "^(.*):.*\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

12、显示/etc/rc.d/rc.sysinit文件中以不区分大小的h开头的行;

[root@128 ~]# grep -i ^h /etc/rc.d/rc.sysinit

13、显示/etc/passwd中以sh结尾的行;

[root@128 ~]# grep sh$ /etc/passwd

14、显示/etc/fstab中以#开头,且后面跟一个或多个空白字符,而后又跟了任意非空白字符的行;

[root@128 ~]# grep -nE "^#[[:space:]]+[^[:space:]]" /etc/fstab 

 15、查找/etc/rc.d/rc.local中包含“以to开始并以to结尾”的字串行;

[root@128 ~]# grep -E "(to).*\1$" /etc/rc.d/rc.local 

16、查找/etc/inittab中含有“以s开头,并以d结尾的单词”模式的行;

[root@128 ~]# grep -E "^s.*d$" /etc/inittab

17、查找ifconfig命令结果中的1-255之间的整数;

[root@128 ~]# ifconfig | egrep -w "[0-9]{1}|[0-9]{2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5]"

18、显示/var/log/secure文件中包含“Failed”或“FAILED”的行;

[root@128 ~]# grep -E "(Failed|FAILED)" /var/log/secure

19、在/etc/passwd中取出默认shell为bash的行;

[root@128 ~]# grep bash$ /etc/passwd

20、以长格式列出/etc/目录下以ns开头、.conf结尾的文件信息;

[root@128 ~]# cd /etc/
[root@128 etc]# ll $(ls /etc/ | grep -w "^nf.*\.conf$")
-rw-r--r--. 1 root root 1631 Jun 13 22:10 nfs.conf
-rw-r--r--. 1 root root 3604 Jun 13 22:10 nfsmount.conf

21、高亮显示passwd文件中冒号,及其两侧的字符;

[root@master-dns chap04]# grep -E .?:*:.? /etc/passwd

1、写一个 bash脚本以输出数字 0 到 100 中 7 的倍数(0 7 14 21...)的命令。

#!/bin/bash
for ((i=0;i<=100;i++))
do
        if [ $[$i%7] -eq 0 ]
        then
                echo -n $i 
                echo -n " "
        fi
done

2、写一个 bash脚本以统计一个文本文件 nowcoder.txt中字母数小于8的单词。
示例:
假设 nowcoder.txt 内容如下:
how they are implemented and applied in computer 

#!/bin/bash
read line < nowcoder.txt
for n in $line
do
        if [ $(expr length "$n") -lt 8 ]
        then
                echo $n
        fi
done

[root@128 test01]# bash demo14.sh 
how
they
are
and
applied
in

3、写一个 bash脚本以实现一个需求,去掉输入中含有this的语句,把不含this的语句输出
示例:
假设输入如下:
that is your bag
is this your bag?
to the degree or extent indicated.
there was a court case resulting from this incident
welcome to nowcoder
你的脚本获取以上输入应当输出:
that is your bag
to the degree or extent indicated.
welcome to nowcoder

#!/bin/bash
grep -v "this" aaa.txt

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Fish_1112

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值