其他分享
首页 > 其他分享> > sed练习-课后练习

sed练习-课后练习

作者:互联网

sed练习

1、删除centos7系统/etc/grub2.cfg文件中所有以空白开头的行行首的空白字符

[root@centos8 ~]#tail /etc/grub2.cfg 
# the 'exec tail' line above.
### END /etc/grub.d/40_custom ###

### BEGIN /etc/grub.d/41_custom ###
if [ -f  ${config_directory}/custom.cfg ]; then
  source ${config_directory}/custom.cfg
elif [ -z "${config_directory}" -a -f  $prefix/custom.cfg ]; then
  source $prefix/custom.cfg;
fi
### END /etc/grub.d/41_custom ###
[root@centos8 ~]#sed -r 's/^[[:space:]]+(.*)/\1/' /etc/grub2.cfg |tail
# the 'exec tail' line above.
### END /etc/grub.d/40_custom ###

### BEGIN /etc/grub.d/41_custom ###
if [ -f  ${config_directory}/custom.cfg ]; then
source ${config_directory}/custom.cfg
elif [ -z "${config_directory}" -a -f  $prefix/custom.cfg ]; then
source $prefix/custom.cfg;
fi
### END /etc/grub.d/41_custom ###

2、删除/etc/fstab文件中所有以#开头,后面至少跟一个空白字符的行的行首的#和空白字符

这题目太绕了
我的理解是,对以#[:sapce:]+开头的行处理,并删除#[:sapce:]+

[root@centos8 ~]#cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Wed May 12 09:10:42 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
UUID=eacaaab6-1589-4ef4-aeb8-a5fc1ec176a4 /                       xfs     defaults        0 0
UUID=604e8976-2d99-4603-9cb5-9135c1f4887e /boot                   xfs     defaults        0 0
UUID=e972ee60-7186-4a9f-b6e1-18cbff1bb40b /data                   xfs     defaults        0 0
UUID=64c3ba2f-502f-4fed-b9fa-406beb686f46 none                    swap    defaults        0 0

[root@centos8 ~]#sed -r 's/^#[[:space:]]+(.*)/\1/' /etc/fstab

#
/etc/fstab
Created by anaconda on Wed May 12 09:10:42 2021
#
Accessible filesystems, by reference, are maintained under '/dev/disk/'.
See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
After editing this file, run 'systemctl daemon-reload' to update systemd
units generated from this file.
#
UUID=eacaaab6-1589-4ef4-aeb8-a5fc1ec176a4 /                       xfs     defaults        0 0
UUID=604e8976-2d99-4603-9cb5-9135c1f4887e /boot                   xfs     defaults        0 0
UUID=e972ee60-7186-4a9f-b6e1-18cbff1bb40b /data                   xfs     defaults        0 0
UUID=64c3ba2f-502f-4fed-b9fa-406beb686f46 none                    swap    defaults        0 0

3、在centos6系统/root/install.log每一行行首增加#号

[root@Centos6 ~]#tail /root/install.log
Installing efibootmgr-0.5.4-15.el6.x86_64
Installing sudo-1.8.6p3-29.el6_9.x86_64
Installing audit-2.4.5-6.el6.x86_64
Installing e2fsprogs-1.41.12-24.el6.x86_64
Installing xfsprogs-3.1.1-20.el6.x86_64
Installing acl-2.2.49-7.el6_9.1.x86_64
Installing attr-2.4.44-7.el6.x86_64
Installing bridge-utils-1.2-10.el6.x86_64
Installing rootfiles-8.1-6.1.el6.noarch
*** FINISHED INSTALLING PACKAGES ***[root@Centos6 ~]#
[root@Centos6 ~]#
[root@Centos6 ~]#sed -r 's/^(.*)/#\1/' /root/install.log |tail
#Installing efibootmgr-0.5.4-15.el6.x86_64
#Installing sudo-1.8.6p3-29.el6_9.x86_64
#Installing audit-2.4.5-6.el6.x86_64
#Installing e2fsprogs-1.41.12-24.el6.x86_64
#Installing xfsprogs-3.1.1-20.el6.x86_64
#Installing acl-2.2.49-7.el6_9.1.x86_64
#Installing attr-2.4.44-7.el6.x86_64
#Installing bridge-utils-1.2-10.el6.x86_64
#Installing rootfiles-8.1-6.1.el6.noarch
#*** FINISHED INSTALLING PACKAGES ***[root@Centos6 ~]#

4、在/etc/fstab文件中不以#开头的行的行首增加#号

[root@centos8 ~]#cat -A  /etc/fstab
$
#$
# /etc/fstab$
# Created by anaconda on Wed May 12 09:10:42 2021$
#$
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.$
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.$
#$
# After editing this file, run 'systemctl daemon-reload' to update systemd$
# units generated from this file.$
#$
UUID=eacaaab6-1589-4ef4-aeb8-a5fc1ec176a4 /                       xfs     defaults        0 0$
UUID=604e8976-2d99-4603-9cb5-9135c1f4887e /boot                   xfs     defaults        0 0$
UUID=e972ee60-7186-4a9f-b6e1-18cbff1bb40b /data                   xfs     defaults        0 0$
UUID=64c3ba2f-502f-4fed-b9fa-406beb686f46 none                    swap    defaults        0 0$
[root@centos8 ~]#
[root@centos8 ~]#sed 's/^[^#].*/#&/' /etc/fstab

#
# /etc/fstab
# Created by anaconda on Wed May 12 09:10:42 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
#UUID=eacaaab6-1589-4ef4-aeb8-a5fc1ec176a4 /                       xfs     defaults        0 0
#UUID=604e8976-2d99-4603-9cb5-9135c1f4887e /boot                   xfs     defaults        0 0
#UUID=e972ee60-7186-4a9f-b6e1-18cbff1bb40b /data                   xfs     defaults        0 0
#UUID=64c3ba2f-502f-4fed-b9fa-406beb686f46 none                    swap    defaults        0 0
#第一行是空行,处理后没有加上#
[root@centos8 ~]#sed -e 's/^$/#/' -e 's/^[^#].*/#&/' /etc/fstab
#
#
# /etc/fstab
# Created by anaconda on Wed May 12 09:10:42 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
#UUID=eacaaab6-1589-4ef4-aeb8-a5fc1ec176a4 /                       xfs     defaults        0 0
#UUID=604e8976-2d99-4603-9cb5-9135c1f4887e /boot                   xfs     defaults        0 0
#UUID=e972ee60-7186-4a9f-b6e1-18cbff1bb40b /data                   xfs     defaults        0 0
#UUID=64c3ba2f-502f-4fed-b9fa-406beb686f46 none                    swap    defaults        0 0

5、处理/etc/fstab路径,使用sed命令取出其目录名和基名

基名是什么意思?
基名相当于绝对路径中最后一个子目录或文件的名称,
例如:/etc/sysconfig/network 基名就是network;/etc/sysconfig 基名就是 sysconfig
可以使用basename命令取出绝对路径的基名。例如:basename /etc/sysconfig/network

[root@centos8 ~]#echo /etc/fstab |sed -r 's/^.*(f.*)/\1/'
fstab
[root@centos8 ~]#echo /etc/fstab |sed -r 's/^(.*)\/f.*/\1/'
/etc
[root@centos8 ~]#echo /etc/fstab |sed -r 's@(^/.*)/(.*)@\1@'
/etc
[root@centos8 ~]#echo /etc/fstab |sed -r 's@(^/.*)/(.*)@\2@'
fstab

6、利用sed 取出ifconfig命令中本机的IPv4地址

[root@centos8 ~]#ifconfig |sed -nr '2s/^.*inet ([0-9.]+).*/\1/p'
10.0.0.154

7、统计centos安装光盘中Package目录下的所有rpm文件的以.分隔倒数第二个字段的重复次数

挂载光盘后有两个含有Package的文件夹

[root@centos8 ~]#find /mnt -type d -name 'Packages'
/mnt/AppStream/Packages
/mnt/BaseOS/Packages

处理 /mnt/BaseOS/Packages

[root@centos8 ~]#ll /mnt/BaseOS/Packages |sed -nr '/rpm$/s@.*\.(.*)\.rpm@\1@p' |wc -l
1694
[root@centos8 ~]#ll /mnt/BaseOS/Packages |grep 'rpm$' | wc -l
1694		#确定sed命令处理正确
[root@centos8 ~]#ll /mnt/BaseOS/Packages |sed -nr '/rpm$/s@.*\.(.*)\.rpm@\1@p' |sort -u
i686
noarch
x86_64
#以rpm结尾的行,倒数第二个字段有三种字符
[root@centos8 ~]#ll /mnt/BaseOS/Packages |sed -nr '/rpm$/s@.*\.(.*)\.rpm@\1@p' |sed -n '/^i686/p' |wc -l
396
[root@centos8 ~]#ll /mnt/BaseOS/Packages |sed -nr '/rpm$/s@.*\.(.*)\.rpm@\1@p' |sed -n '/noarch/p' |wc -l
224
[root@centos8 ~]#ll /mnt/BaseOS/Packages |sed -nr '/rpm$/s@.*\.(.*)\.rpm@\1@p' |sed -n '/x86_64/p' |wc -l
1074
[root@centos8 ~]#ll /mnt/BaseOS/Packages |sed -nr '/rpm$/s@.*\.(.*)\.rpm@\1@p' |sort -r |uniq -c
   1074 x86_64
    224 noarch
    396 i686

处理 /mnt/AppStream/Packages

[root@centos8 ~]#ls /mnt/AppStream/Packages |wc -l
5685
[root@centos8 ~]#ls /mnt/AppStream/Packages |grep 'rpm$' |wc -l
5684
[root@centos8 ~]#ls /mnt/AppStream/Packages |grep -v 'rpm$' 
TRANS.TBL
[root@centos8 ~]#ls /mnt/AppStream/Packages |sed -nr '/rpm$/p' |wc -l
5684	#确认sed命令执行正确
[root@centos8 ~]#ls /mnt/AppStream/Packages |sed -nr '/rpm$/s@.*\.(.*)\.rpm$@\1@p' |sort -r |uniq -c
   2631 x86_64
   2134 noarch
    919 i686

8、统计/etc/init.d/functions文件中每个单词的出现次数,并排序(用grep和sed两种方法分别实现)

我擦,,第7题我已经忍了,又来这种题,面试官这么变态吗!!!统计这东西除了折磨人还有啥意义呢?

[root@centos8 script]#grep -Eo "[a-zA-Z]+" /etc/init.d/functions |sort |uniq -c
[root@centos8 script]#cat /etc/init.d/functions |sed -r 's/[^[:alpha:]]+/\n/g' |sed '/^$/d' |sort |uniq -c |sort -nr
#sed '/^$/d' 是删掉空白行

9、将文本文件的n和n+1行合并为一行,n为奇数行(sed高级用法)

[root@centos8 study]#cat sed_advanced 
1abc
2xyz
3mn
4qwer
5centos
6ubantu
7Linux
sed -n 'N;s/\n//p' sed_advanced 
1abc2xyz
3mn4qwer
5centos6ubantu
#读取第7行的时候,没有了n+1行,即没有\n字符,'s/\n//p'没有匹配的东西,p指令就没有打印内容
[root@centos8 study]#echo 123 abc |sed -n 's/\n//p'
[root@centos8 study]#
#这样理解正确吗?

#这样呢?
[root@centos8 study]#sed 'N;s/\n//' sed_advanced 
1abc2xyz
3mn4qwer
5centos6ubantu
7Linux

标签:fstab,rpm,练习,etc,sed,课后练习,root,centos8
来源: https://blog.csdn.net/weixin_41142408/article/details/118304930