- 删除centos7系统/etc/grub2.cfg文件中所有以空白开头的行行首的空白字符
[root@centos7 ~]# sed "s#^[[:space:]]\+##" /etc/grub2.cfg |tail -10
# 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 ###
- 删除/etc/fstab文件中所有以#开头,后面至少跟一个空白字符的行的行首的#
和空白字符
[root@centos7 ~]# sed "s@^#[[:space:]]\+@@" /etc/fstab
#
/etc/fstab
Created by anaconda on Sat Jun 6 19:41:00 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=2eeea4df-07ba-4bcd-ab95-eb921423df18 / xfs defaults 0 0
UUID=d60ee508-b5cf-49e7-9b04-347992d975ca /boot xfs defaults 0 0
UUID=2ce82ded-6156-422a-95b8-1d0c0e93ab68 /data xfs defaults 0 0
UUID=36755f53-3e04-4e77-8cdf-f306b6875e22 swap swap defaults 0 0
- 在centos6系统/root/install.log每一行行首增加#号
[root@centos6min ~]# sed -r "s@^(.*)@#\1@g" /root/install.log |head
#Installing libgcc-4.4.7-23.el6.x86_64
#warning: libgcc-4.4.7-23.el6.x86_64: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
#Installing setup-2.8.14-23.el6.noarch
#Installing filesystem-2.4.30-3.el6.x86_64
#Installing basesystem-10.0-4.el6.noarch
#Installing ncurses-base-5.7-4.20090207.el6.x86_64
#Installing kernel-firmware-2.6.32-754.el6.noarch
#Installing tzdata-2018e-3.el6.noarch
#Installing glibc-common-2.12-1.212.el6.x86_64
#Installing nss-softokn-freebl-3.14.3-23.3.el6_8.x86_64
- 在/etc/fstab文件中不以#开头的行的行首增加#号
[root@centos6min ~]# sed -r 's/^(^#*)/#/g' /etc/fstab
#
#
# /etc/fstab
# Created by anaconda on Sun Jun 7 22:59:50 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
#
#/dev/mapper/vg_centos6min-lv_root / ext4 defaults 1 1
#UUID=9ef81aab-164a-46e5-a626-aead33d77075 /boot ext4 defaults 1 2
#/dev/mapper/vg_centos6min-lv_home /home ext4 defaults 1 2
#/dev/mapper/vg_centos6min-lv_swap swap swap defaults 0 0
#tmpfs /dev/shm tmpfs defaults 0 0
#devpts /dev/pts devpts gid=5,mode=620 0 0
#sysfs /sys sysfs defaults 0 0
#proc /proc proc defaults 0 0
- 处理/etc/fstab路径,使用sed命令取出其目录名和基名
[root@centos6min ~]# echo "/etc/fstab" | sed -r 's/^(\/.*\/)[^/]+\/?/\1/g'
/etc/
[root@centos6min ~]# echo "/etc/fstab" |sed -r 's@^/.*/@@'
fstab
- 利用sed 取出ifconfig命令中本机的IPv4地址
[root@centos6min ~]# ifconfig eth0 | sed -n '2p' | sed -r 's@^[[:space:]].*inet[[:space:]]@@' | sed -r 's@[[:space:]].*@@'
addr:192.168.177.132
- 统计centos安装光盘中Package目录下的所有rpm文件的以.分隔倒数第二个
字段的重复次数
[root@centos7 cd]# ls /misc/cd/Packages/ | grep "rpm$" | sed -r 's@.*\.(.*)\.rpm@\1@' | sort | uniq -c
1404 noarch
2663 x86_64
- 统计/etc/init.d/functions文件中每个单词的出现次数,并排序(用grep和
sed两种方法分别实现)
[root@centos6min ~]# grep -o "[[:alpha:]]*" /etc/init.d/functions | sort | uniq -c | sort -nr|head
83 if
77 then
75 pid
73 echo
72 fi
61 return
56 dev
55 file
50 n
47 local
[root@centos6min ~]# sed -nr 's/[[:alpha:]]+/\n/g'p /etc/init.d/functions | sort | uniq -c | sort -nr|head
- 将文本文件的n和n+1行合并为一行, n为奇数行
[root@centos6min ~]# sed -n 'N;s/\n//'p /etc/passwd
root:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologinsync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologinuucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologinftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologinvcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologinpostfix:x:89:89::/var/spool/postfix:/sbin/nologin
网友评论