正则:
1
、显示
/etc/rc.d/rc.sysinit
文件中以不区分大小的
h
开头的行;
grep -i '^h' 1.txt
2
、显示
/etc/passwd
中以
sh
结尾的行
;
grep "sh$" /etc/passwd
3
、显示
/etc/fstab
中以
#
开头,且后面跟一个或多个空白字符,而后又跟了任意非空白字符的行;
grep -E "^\#[[:space:]]\+[^[:space:]]\+" /etc/fstab
4
、查找
/etc/rc.d/rc.local
中包含
“
以
to
开始并以
to
结尾
”
的字串行;
grep "\(to\).*\1" /etc/rc.d/rc.local
5
、查找
/etc/inittab
中含有
“
以
s
开头,并以
d
结尾的单词
”
模式的行;
grep "\<s[a-Z]*d\>" /etc/inittab
6
、查找
ifconfig
命令结果中的
1-255
之间的整数;
ifconfig|egrep "[1-9]|[1-9][0-9][0-9]|2[0-4][1-9]|25[0-5]"
7
、显示
/var/log/secure
文件中包含
“Failed”
或
“FAILED”
的行;
egrep "(Faild|FAILD|Accepted)" /var/log/secure
8
、在
/etc/passwd
中取出默认
shell
为
bash
的行;
grep "bash$" /etc/passwd
9
、以长格式列出
/etc/
目录下以
ns
开头、
.conf
结尾的文件信息;
ls -l /etc |grep "\<ns.*\.conf\>$"
10
、高亮显示
passwd
文件中冒号,及其两侧的字符;
egrep --color ".(:.){1,}|.:+." /etc/passwd
sed练习
1
、删除
/etc/grub2.conf
文件中所有以空白开头的行行首的空白字符
sed -n 'd^$ '
2
、删除
/etc/fstab
文件中所有以
#
开头,后面至少跟一个空白字符的行的行首的
#
和空白字符
# sed 's/^#[[:space:]]\+//' 1.txt
3
、在
/root/install.log
每一行行首增加
#
号
sed 's/^.*$/#&/' 1.txt
4
、在
/etc/fstab
文件中不以
#
开头的行的行首增加
#
号
sed -r 's/^[^\#]/#&/' /etc/fstab
5
、利用
sed
取出
ifconfig
命令中本机的
IPv4
地址
ifconfig |sed -n 2p|sed -r 's/.*inet[[:space:]]*//'|sed -r 's/[[:space:]]*netmask.*//'
6
、关闭本机
SELinux
的功能
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
7
、在
/etc/hosts
配置文件中添加内容
sed -i '1 i my name is xxx' 3.txt