开发者学堂课程【Linux企业运维实战 - 入门及常用命令:文本三剑客sed高级用法和企业级实战】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/550/detail/7620
文本三剑客sed高级用法和企业级实战
内容介绍:
一、sed 工具
二、sed 示例
三、高级编辑命令
一、sed工具
1、用法:
sed
[
option]...'script'
inputfile…
2、常用选项:
-n:不输出模式空间内容到屏幕,即不自动打印
-e:多点编辑
-f:/PATH/SCRIPT_FILE:从指定文件中读取编辑脚本
-r:支持使用扩展正则表达式
-i.bak:备份文件并原处编辑
3、script:
‘地址命令’
4、地址定界:
(1)不给地址:对全文进行处理
(2)单地址:
#:指定的行,$:最后一行
/pattern/:被此处模式所能够匹配到的每一行
(3)地址范围:
#
,
#
#
,
+#
/pat1/
,
/pat2/
#./pat1/
4)~:步进
1~2奇数行
2~2偶数行
5、编辑命令:
d:删除模式空间匹配的行,并立即启用下一轮循环
p:打印当前模式空间内容,追加到默认输出之后
a [\]text:在指定行后面追加文本
支持使用\n实现多行追加
i [\]text:在行前面插入文本
c [\]text:替换行为单行或多行文本
w /path/somefile:保存模式匹配的行至指定文件
r /path/somefile:读取指定文件的文本至模式空间中
匹配到的行后
=: 为模式空间中的行打印行号
! : 模式空间中匹配行取反处理
示例:
[root@centos7 ~]#sed ‘2,$a=====’ f1
作用:追加别名命令、模式匹配…
在包含alias后面追加别名定义:
[root@centos7 ~]#alias p=poweroff’ .bashrc
[root@centos7 ~]#cat .bahsrc
# .bashrc
[root@centos7 ~]#Cat .bashrc
并没有真的改文件,只是把选项改变了,为避免文件改错了,一般都会加.bashrc后缀,表示备份
备份文件:
[root@centos7 ~]#sed -I.bak ‘/aliases/alias p=poweroff’ .bashrc
[root@centos7 ~]#Cat .bashrc
加别名:
[root@centos7 ~]#set -i bak2 ‘/aliases/alias cdnet=“cd /etc/sysconfig/network-network-scripts/”’ .bashrc
-a特点:加空格加不进去
[root@centos7 ~]#sed ‘2,5a”
====‘ f1
加斜线,实现多行追加
[root@centos7 ~]#sed ‘5a\
=====‘ f1
i在前面加,a在后面加
[root@centos7 ~]#sed ‘2,5i\
====‘ f1相当于用新的代替
c为代替,二行到五行代替
[root@centos7 ~]#sed ‘2,5c\
=====’ f1
用w把二行到五行符合条件的内容存到新文件里
[root@centos7 ~]#
sed
‘2,5w f2’ f1
用r 读入内容放到二行到五行中:
[root@centos7 ~]#sed ‘2,5r /etc/issue’ f1
[root@centos7 ~]#Cat .bashrc
[root@centos7 ~]#Cat > sed.txt
[root@centos7 ~]#Sed ‘/Source/r sed.txt’ .bashrc
[root@centos7 ~]#cat sed.txt
[root@centos7 ~]#Reset.sh
加别名,一条一条加很麻烦,可以将别名放到一个文件里,r读取即可
在source行前插入,r表示在后面追加
[root@centos7 ~]#sed ‘/source/r sed,txt’ .bashrc
6、s ///:查找替换,支持使用其他分隔符,s@@@,s###
7、替换标记:
g:行内全局替换
p:显示替换成功的行
w /PATH/TO/SOMEFILE :将替换成功的行保存至文件中
示例:
搜索root,替换成administer,如果没有写地址,就代表全文件搜索替代,用g实现全文替代
[root@centos7 ~]#sed
‘s
/root/administrator/‘ /etc/passwd
写正则表达式、正向引用:
[root@centos7 ~]#sed -r ‘s/(root)/\1er/g’ /etc/passwd^C
[root@centos7 ~]#sed -r ‘s/(root)/admin\1/g’ /etc/passwd
整行加字符串:root替换成rooter,扩展的不用加斜线/
[root@centos7 ~]#ssed -r ‘s/()/admin/\1erg’ /etc/passwd
每个整行的结尾都加字符串:
[root@centos7 ~]#sed -r ‘s/(.*)/\imagedu/‘ /etc/paswd
每个整行的结尾都加字符串:
[root@centos7 ~]#sed -r ‘s/(.*)magedu\1/‘ /etc/passwd
bash 前加变为s 并 bash:
[root@centos7 ~]#sed -r s@/bin/bash$@/sbin/bash@‘ /etc/passwd
Or 分组写
[root@centos7 ~]#sed
-r ‘$@/(bin/bash)$@/s\1@’ /etc/passwd
在quiet后面加一个字符串表示xyz:
[root@centos7 ~]#sed -r ‘/GRUB_CMDLINE_LINUX/s/(.*)//’ /etc/default/grub
不用正则表达式也可以
[root@centos7 ~]#sed -r ‘/GRUB_CMDLINE_LINUX/s/“$/xyz”/’ /etc/default/grub
在整行结尾插入:
[root@centos7 ~]#sed -r ‘/GRUB_CONLINE_LINUX/$/\1 xyz”/‘ /etc/default/grub
简便方法:
[root@centos7 ~]#sed -r ‘/GRUB_CMDLINE_LINUX/s/“$/ xyz” /etc/default/grub
用sed命令取ip地址
[root@centos7 ~]#
ifconfig
s ens33| sed -n ‘2p’
[root@centos7 ~]#ifconfig ens33| sed -n ‘2p’ | sad -r ‘$@(.*inept )(.*)( net.*)@\2@‘
浓缩命令:
[root@centos7 ~]#Ifconfig ens33| sed -r ‘2!d;s@(.*inet)(.*)( net.*)’
简单版:
[root@centos7 ~]#ifconfig ens33|sed -n ‘2p’ |sed ‘s/.*inet //’ -e ‘s/ net ask.*//’
整合:
ifconfig ens33| sed -n ‘2p’ | sed -r s@.inet(.*)net.*@\1@’
ifconfig ens33| sed -n ‘2p’ | sed -r s@.inet (.*)net.*@\1@’
ifconfig ens33| sed -n ‘2p’ | sed -r s@.inet (.*) net.*@\1@’
ifconfig ens33| sed -n ‘2p’ | sed -r s@.inet (.*inet )(.*)( net.*)@\1@’
ifconfig ens33| sed -r ‘2!d:s@(.*inet )(.*)( net.*)@\2@’
ifconfig ens33| sed -n ‘2p’ | sed s/:*inet //‘ |sed ’ s/ netmask.*//’
ifconfig ens33| sed -n ‘2p’ | sed -e s/:*inet //‘ -e ’ s/ netmask.*//
示例:
批量删除注释中的符号或者注释:
装载 http 安装包
[root@centos6 ~]#rpm -ivh /misc/cd/Packages/https-2.2.15-59.e16.centos.x86_64.rpm
(1)删除注释中的 #
[root@centos6 profile.d]#sed ‘/^#NamevirtualHost/s/#//’
/etc/https/conf/https.conf
(2)删除全部内容
[root@centos6 profile.d]#sed ‘/^#
/etc/https/conf/https.conf
Or
[root@centos6 profile.d]#sed -e ‘/^#
查看文件夹或文件的基名:
[root@centos6 profile.d]#
echo “/etc
/sysconfig/network/
”
|sed -r
‘(.*/)(.*?)\1/’
/etc/sysconfig/network/
[root@centos6 profile.d]#
echo “/etc
/sysconfig/network/
”
|sed -r
‘(.*/)(.*?$)\1/’
/etc/sysconfig/network/
[root@centos6 profile.d]#
echo “/etc
/sysconfig/network/
”
|sed -r
‘(.*/)([^/]+\/?$)\1/’
/etc/sysconfig/
#
virtualHost: If you want to maintain multiple domains/ hostnames on your
#
machine you can setup virtualhost containers for them. Most contigurations
#
use only name-based virtual hosts so the server doesnt need to worry about
#
IP addresses. This is indicated by the asterisks in the directives below.
#
Please see the documentation at
#
< URL: http: / / httpd. apache. org/docs/2.2/vhosts/>
#
for further details before you try to setup virtual hosts
#
You may use the command line option ' -sto verify your virtual host
#
contiguration
#
Use name-based virtual hosting
# NamevirtualHost : 80
#
NOTE: NamevirtualHost cannot be used without a port specifier
#
(e. g. : 80) if mod_ ssl is being used, due to the nature of the
#
ssL protocol
#
Almost any Apache directive may go into a virtualHost container
#
The
f
irst virtualHost section is used for requests without a known
#
server name
Virtual
HOST *:80
>
#
SERVERADMIN WEBMASTER@DUMMY-HOST.EXAMPLE.COM
#
DOCUMENTROOT /www/Docs/Dummy-host.EXAMPLE.COM
#
SERVERNAME dummy-hostexamLE.com
# ErrorLog 1ogs/dummy-host.exampLE.COM-ERROR-LOG
二、sed示例
sed
'2p'
/etc/passwd
sed
-n ‘2p’ /
etc/passwd
sed -n ‘1,4p’
/
etc/passwd
sed
-n
/root/p
’ /
etc/passwd
sed
-n
‘
2
,
/root
/
p
’
/etc/passwd 从2行开始
sed -n ‘
/^$/=”
file显示空行行号
sed -n -e
'/^$/p'
-e ‘/^$/=’ file
sed
'/root/a\superman"/etc/passwd行后
sed
'/root/i\superman" /etc/passwd行前
sed
'/root/c\superman' /etc/passwd代替行
sed
"/^$/d’
file
sed
‘
1,10d’
file
nl
/etc/passwd l
sed
‘
2,5d
’
nl
/etc/passwd |
sed '2
a te
a'
sed 's/test/mytest/g' example
sed
-n 's/root/&superman/p /etc/passwd单词后
sed
-n 's/root/superman&/p /etc/passwd单词前
sed
-e 's/dog/cat/"
-e
's/hi/lo/'
pets
sed
-i.bak 's/dog/cat/g' pets
三、高级编辑命令
P:打印模式空间开端至\n内容,并追加到默认输出之前
h:把模式空间中的内容覆盖至保持空间中
H:把模式空间中的内容追加至保持空间中
9:从保持空间取出数据覆盖至模式空间
G:从保持空间取出内容追加至模式空间
x:把模式空间中的内容与保持空间中的内容进行互换
n:读取匹配到的行的下一行覆盖至模式空间
N:读取匹配到的行的下一行追加至模式空间
d:删除模式空间中的行
D:如果模式空间包含换行符,则删除直到第一个换行符的模式空间中的文本,并不会读取新的输入行,而使用合成的模式空间重新启动循环。
如果模式空间不包含换行符,则会像发出d命令那样启动正常的新循环