自动化运维
1.命令别名-----alias
unalias 取消别名
[root@VM_0_7_centos ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
2.sed
sed是一款流编辑工具,用来对文本进行过滤和替换操作,特别是当你想要对几十个配置文件做统一修改时。
sed命令的选项(option):
-n :只打印模式匹配的行
-e :直接在命令行模式上进行sed动作编辑,此为默认选项
-f :将sed的动作写在一个文件内,用–f filename 执行filename内的sed动作
-r :支持扩展表达式
-i :直接修改文件内容
[root@VM_0_7_centos ~]# sed '2a jason123' test.txt #在第二行后面追加jason123
[root@VM_0_7_centos ~]# sed '3i jason123' test.txt #在第三行前追加jason123
[root@VM_0_7_centos ~]# sed 's/yes/no/g' test.txt #将文本yes换为no
[root@VM_0_7_centos ~]# sed '3,4d' test.txt 删除第3、4行的内容
[root@VM_0_7_centos ~]# sed '/ONBOOT/a jason123' test.txt #匹配ONBOOT的行,在后面添加jason123
[root@VM_0_7_centos ~]# sed '/^GATEWAY/d' test.txt #匹配GATEWAY开始的行,并删除该行
[root@VM_0_7_centos ~]# sed '/^$/d' test.txt #匹配到空白行后,删除该行
打印文件的奇数行
[root@VM_0_7_centos ~]# sed -n '1~2p' test.txt
删除2-8行之间的所有行
[root@VM_0_7_centos ~]# sed '2,8d' test.txt
Sed脚本指令范例
范例1
样本文件
[root@VM_0_7_centos ~]# cat test.txt
<html>
<title>First Web</title>
<body>Hello the World!<body>
</html>
1.编写Sed脚本,替换与行匹配相同的内容,即将body替换为/body,但仅替换第二个body为/body).
[root@VM_0_7_centos ~]# cat sed.sh
/body/{s//\/body/2}
[root@VM_0_7_centos ~]# sed -f sed.sh test.txt
<html>
<title>First Web</title>
<body>Hello the World!</body>
</html>
范例2
给所有第一个的h1,h2等添加<>;第二个h1,h2</>.
[root@VM_0_7_centos ~]# cat sed.sh
/h[0-9]/{
s//\<&\>/1
s//\<\/&\>/2
}
[root@VM_0_7_centos ~]#
[root@VM_0_7_centos ~]# sed -f sed.sh test.txt
<html>
<title>First Web</title>
<body>
<h1>Hello</h1>
<h2>Hello</h2>
<h3>Hello</h3>
<body>
</html>
[root@VM_0_7_centos ~]# sed '/h[0-9]/s//\<&\>/1;s//\<\/&\>/2' test.txt
<html>
<title>First Web</title>
<body>
<h1>Hello</h1>
<h2>Hello</h2>
<h3>Hello</h3>
<body>
</html>
范例3
删除文件中的空白行
[root@VM_0_7_centos ~]# cat test.txt
OTPROTO=none
DEFROUTE=yes
DEVICE=eth0
GATEWAY=172.17.0.1
HWADDR=52:54:00:2d:1d:06
IPADDR=172.17.0.7
NETMASK=255.255.240.0
NM_CONTROLLED=no
ONBOOT=yes
TYPE=Ethernet
USERCTL=no
[root@VM_0_7_centos ~]# sed '/.*/{/^$/d}' test.txt
OTPROTO=none
DEFROUTE=yes
DEVICE=eth0
GATEWAY=172.17.0.1
HWADDR=52:54:00:2d:1d:06
IPADDR=172.17.0.7
NETMASK=255.255.240.0
NM_CONTROLLED=no
ONBOOT=yes
TYPE=Ethernet
USERCTL=no
[root@VM_0_7_centos ~]# sed '/^$/d' test.txt
OTPROTO=none
DEFROUTE=yes
DEVICE=eth0
GATEWAY=172.17.0.1
HWADDR=52:54:00:2d:1d:06
IPADDR=172.17.0.7
NETMASK=255.255.240.0
NM_CONTROLLED=no
ONBOOT=yes
TYPE=Ethernet
USERCTL=no
范例4
范本
[root@VM_0_7_centos ~]# cat test.txt
OTPROTO=none
DEFROUTE=yes
DEVICE=eth0
GATEWAY=172.17.0.1
HWADDR=52:54:00:2d:1d:06
IPADDR=172.17.0.7
NETMASK=255.255.240.0
NM_CONTROLLED=no
ONBOOT=yes
TYPE=Ethernet
USERCTL=no
4.在Ethernet行后添加一行,内容为IPADDR=192.168.0.1
[root@VM_0_7_centos ~]# sed '/Ethernet/a IPADDR=192.168.0.1' test.txt
OTPROTO=none
DEFROUTE=yes
DEVICE=eth0
GATEWAY=172.17.0.1
HWADDR=52:54:00:2d:1d:06
IPADDR=172.17.0.7
NETMASK=255.255.240.0
NM_CONTROLLED=no
ONBOOT=yes
TYPE=Ethernet
IPADDR=192.168.0.1
USERCTL=no
范例5
5.在匹配Netmask的行前插入内容IPADDR=192.168.0.1
[root@VM_0_7_centos ~]# sed '/NETMASK/i IPADDR=192.168.0.1' test.txt
OTPROTO=none
DEFROUTE=yes
DEVICE=eth0
GATEWAY=172.17.0.1
HWADDR=52:54:00:2d:1d:06
IPADDR=172.17.0.7
IPADDR=192.168.0.1
NETMASK=255.255.240.0
NM_CONTROLLED=no
ONBOOT=yes
TYPE=Ethernet
USERCTL=no
范例6
6.将包含ONBOOT行的内容更改为ONBOOT=no
[root@VM_0_7_centos ~]# sed '/ONBOOT/c ONBOOT=no' test.txt
OTPROTO=none
DEFROUTE=yes
DEVICE=eth0
GATEWAY=172.17.0.1
HWADDR=52:54:00:2d:1d:06
IPADDR=172.17.0.7
NETMASK=255.255.240.0
NM_CONTROLLED=no
ONBOOT=no
TYPE=Ethernet
USERCTL=no
范例7
7.显示非打印字符,一般与-n一起使用,l 显示非打印字符
[root@VM_0_7_centos ~]# sed -n '1,2l' test.txt
OTPROTO=none$
DEFROUTE=yes$
范本8
8.将小写转换为大写
文本
[root@VM_0_7_centos ~]# cat test.txt
OTPROTO=none
DEFROUTE=yes
DEVICE=eth0
GATEWAY=172.17.0.1
IPADDR=172.17.0.7
netmask=255.255.240.0
大小写转换
[root@VM_0_7_centos ~]# sed 's/[a-z]/\u&/g' test.txt
OTPROTO=NONE
DEFROUTE=YES
DEVICE=ETH0
GATEWAY=172.17.0.1
IPADDR=172.17.0.7
NETMASK=255.255.240.0
[root@VM_0_7_centos ~]# sed 's/[A-Z]/\l&/g' test.txt
otproto=none
defroute=yes
device=eth0
gateway=172.17.0.1
ipaddr=172.17.0.7
netmask=255.255.240.0
范本9
打印(p), 显示第一行,第二行内容
[root@VM_0_7_centos ~]# sed -n '1,2p' test.txt
OTPROTO=none
DEFROUTE=yes
AWK
后续会慢慢补上。。。。