10.文件处理之sed-案例分析

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

[root@localhost ~]#sed -r 's@^[[:space:]]+(.*)@\1@' /etc/grub2.cfg 
#
# DO NOT EDIT THIS FILE
#
# It is automatically generated by grub2-mkconfig using templates
# from /etc/grub.d and settings from /etc/default/grub
#

### BEGIN /etc/grub.d/00_header ###
set pager=1

if [ -s $prefix/grubenv ]; then
load_env
fi
if [ "${next_entry}" ] ; then
set default="${next_entry}"
set next_entry=
save_env next_entry
set boot_once=true
else
set default="${saved_entry}"
fi

if [ x"${feature_menuentry_id}" = xy ]; then
menuentry_id_option="--id"
else
menuentry_id_option=""
fi

export menuentry_id_option

if [ "${prev_saved_entry}" ]; then
set saved_entry="${prev_saved_entry}"
save_env saved_entry
set prev_saved_entry=
save_env prev_saved_entry
set boot_once=true
fi

function savedefault {
if [ -z "${boot_once}" ]; then
saved_entry="${chosen}"
save_env saved_entry
fi
}

function load_video {
if [ x$feature_all_video_module = xy ]; then
insmod all_video
else
insmod efi_gop
insmod efi_uga
insmod ieee1275_fb
insmod vbe
insmod vga
insmod video_bochs
insmod video_cirrus
fi
}

terminal_output console
if [ x$feature_timeout_style = xy ] ; then
set timeout_style=menu
set timeout=5
# Fallback normal timeout code in case the timeout_style feature is
# unavailable.
else
set timeout=5
fi
### END /etc/grub.d/00_header ###

### BEGIN /etc/grub.d/00_tuned ###
set tuned_params=""
set tuned_initrd=""
### END /etc/grub.d/00_tuned ###

### BEGIN /etc/grub.d/01_users ###
if [ -f ${prefix}/user.cfg ]; then
source ${prefix}/user.cfg
if [ -n "${GRUB2_PASSWORD}" ]; then
set superusers="root"
export superusers
password_pbkdf2 root ${GRUB2_PASSWORD}
fi
fi
### END /etc/grub.d/01_users ###

### BEGIN /etc/grub.d/10_linux ###
menuentry 'CentOS Linux (3.10.0-957.el7.x86_64) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-957.el7.x86_64-advanced-71b66acd-688f-45df-8d45-1d3cf606820f' {
load_video
set gfxpayload=keep
insmod gzio
insmod part_msdos
insmod xfs
set root='hd0,msdos1'
if [ x$feature_platform_search_hint = xy ]; then
search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1'  1890737d-134e-49aa-a764-569a2bd251d8
else
search --no-floppy --fs-uuid --set=root 1890737d-134e-49aa-a764-569a2bd251d8
fi
linux16 /vmlinuz-3.10.0-957.el7.x86_64 root=UUID=71b66acd-688f-45df-8d45-1d3cf606820f ro rhgb quiet LANG=en_US.UTF-8
initrd16 /initramfs-3.10.0-957.el7.x86_64.img
}
menuentry 'CentOS Linux (0-rescue-ce95461e1b17474b8955b77bd84c1ce8) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-0-rescue-ce95461e1b17474b8955b77bd84c1ce8-advanced-71b66acd-688f-45df-8d45-1d3cf606820f' {
load_video
insmod gzio
insmod part_msdos
insmod xfs
set root='hd0,msdos1'
if [ x$feature_platform_search_hint = xy ]; then
search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1'  1890737d-134e-49aa-a764-569a2bd251d8
else
search --no-floppy --fs-uuid --set=root 1890737d-134e-49aa-a764-569a2bd251d8
fi
linux16 /vmlinuz-0-rescue-ce95461e1b17474b8955b77bd84c1ce8 root=UUID=71b66acd-688f-45df-8d45-1d3cf606820f ro rhgb quiet
initrd16 /initramfs-0-rescue-ce95461e1b17474b8955b77bd84c1ce8.img
}

### END /etc/grub.d/10_linux ###

### BEGIN /etc/grub.d/20_linux_xen ###
### END /etc/grub.d/20_linux_xen ###

### BEGIN /etc/grub.d/20_ppc_terminfo ###
### END /etc/grub.d/20_ppc_terminfo ###

### BEGIN /etc/grub.d/30_os-prober ###
### END /etc/grub.d/30_os-prober ###

### BEGIN /etc/grub.d/40_custom ###
# This file provides an easy way to add custom menu entries.  Simply type the
# menu entries you want to add after this comment.  Be careful not to change
# 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文件中所有以#开头,后面至少跟一个空白字符的行的行首的#和空白字符

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

#
/etc/fstab
Created by anaconda on Mon Aug  3 17:02:43 2020
#
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
#
UUID=71b66acd-688f-45df-8d45-1d3cf606820f /                       xfs     defaults        0 0
UUID=1890737d-134e-49aa-a764-569a2bd251d8 /boot                   xfs     defaults        0 0
UUID=82c012a2-3f30-4649-ac90-e9896c20f829 /data                   xfs     defaults        0 0
UUID=87716ee2-fda6-41e2-9d46-4d42bd7bb257 swap                    swap    defaults        0 0

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

[root@centos6 ~]#sed -ri 's@(^.*)@#\1@' /root/install.log

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

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

#
# /etc/fstab
# Created by anaconda on Mon Aug  3 17:02:43 2020
#
# 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
#
#UUID=71b66acd-688f-45df-8d45-1d3cf606820f /                       xfs     defaults        0 0
#UUID=1890737d-134e-49aa-a764-569a2bd251d8 /boot                   xfs     defaults        0 0
#UUID=82c012a2-3f30-4649-ac90-e9896c20f829 /data                   xfs     defaults        0 0
#UUID=87716ee2-fda6-41e2-9d46-4d42bd7bb257 swap                    swap    defaults        0 0

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

[root@localhost ~]#echo "etc/fstab"|sed -r 's@(.*)\/.+@\1@'
etc
[root@localhost ~]#echo "etc/fstab"|sed -r 's@.*\/(.+)@\1@'
fstab

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

[root@localhost /data]#ifconfig ens33|sed -nr '2s/(^[^0-9]+)([0-9.]+)( .*$)/\2/p'
10.50.100.12
[root@localhost /data]#ifconfig ens33|sed -nr '2s/^[^0-9]+([0-9.]+) .*$/\1/p'
10.50.100.12

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

[root@localhost ~]#ls /mnt/cdrom/Packages/*.rpm |sed -r 's@.*\.(.*)\.rpm$@\1@'|sort |uniq -c
   1385 noarch
   2636 x86_64

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

[root@localhost ~]#grep -Eo "[[:alpha:]]+" /etc/init.d/functions|sort|uniq -c
[root@localhost ~]#cat /etc/init.d/functions |sed -r 's@[^[:alpha:]]+@\n@g'|sort |uniq -c|sort -n

9.将文本文件的n和n+1行合并为一行, n为奇数行

[root@localhost ~]#seq 1 10 |sed 'N;s/\n/ /'
1 2
3 4
5 6
7 8
9 10
posted @ 2020-08-22 15:42  人生值得  阅读(321)  评论(0编辑  收藏  举报