设为首页 收藏本站
查看: 863|回复: 0

[经验分享] Linux之sed

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-4-28 08:27:35 | 显示全部楼层 |阅读模式
一、sed简介
二、sed语法
    附:正则表达式元字符
三、sed常用编辑命令
四、sed常用选项


一、sed简介
    Linux三大文本处理工具,grep、sed和awk。
    Stream Editor,sed是文本流编辑器,它能对文本进行行编辑,使用它能对数据进行匹配查找后,进行添加、删除、替换等操作。


二、sed语法
    sed [options] /pattern/ /path/to/file1 /path/to/file2 ······    #pattern为匹配模式;处理多个文件时用空格隔开

    地址定界/pattern/:支持正则表达式,默认支持基本正则表达式,使用选项-r支持扩展正则表达式

    1、可以仅仅是模式匹配的行
?
1
2
3
4
[iyunv@TESTHOST scripts]# sed '/^#/p' /scripts/test
# The first line.
# The first line.
The second one.



    2、指定行,如1,7第一行到第7行,3,/^#/第3行到#开头的行
?
1
2
3
4
5
6
[iyunv@TESTHOST scripts]# sed -n '1,3p;5,/^#/p' /etc/fstab    #打印文件中第1行到第3行、第5行到以井号开头的行其内容
                        #第1行
#                    #第2行   
# /etc/fstab         #第3行
#                    #第4行
# Accessible filesystems, by reference, are maintained under '/dev/disk'    #这里第5行和以井号开头的行是同一行



    3、查找替换
    s@要查找的内容@替换为的内容@g:查找的内容可以使用模式匹配,替换为的内容不能使用模式匹配,但可以引用;标志位:g表示全局,i表示忽略大小写
?
1
2
3
4
5
6
7
8
[iyunv@TESTHOST scripts]# which ls
alias ls='ls --color=auto'
    /bin/ls
[iyunv@TESTHOST scripts]# which ls | sed '/^alias/!s#^[[:space:]]##'   
alias ls='ls --color=auto'
/bin/ls
[iyunv@TESTHOST scripts]# which ls | sed '/^alias/d;s#^[[:space:]]##'    #先去掉alias开头的行,然后搜索替换去掉空白字符
/bin/ls




附:正则表达式元字符

    1、基本正则表达式
    .    任意单个字符
    []    范围内
    [^]     范围外,例如非空白字符[^[:space:]]
    [0-9],[[:digi:]]    数字
    [a-z],[[:lower:]]    小写字母
    [A-Z],[[:upper:]]    大写字母
    [a-zA-Z],[[:alpha:]]    字母
    [a-zA-Z0-9],[[:alnum:]]    数字+字母
    [[:space:]]   空白字符
    [[:punct:]]    标点符号
    \{m,n\}    至少m次,最多n次
    \{m\}    精确m次
    \{m,\}    至少m次
    \{0,n\}    最多n次
    *    匹配前面字符任意字符任意次
    \?  匹配前面字符0次或1次
    ^    锚定行首
    $    锚定行尾
    \<,\b    锚定词首
    \>,\b    锚定词尾
    \(\),\1 ,\2,······    分组

    2、扩展正则表达式
    .    任意单个字符
    []    范围内
    [^]     范围外,例如非空白字符[^[:space:]]
    [0-9],[[:digi:]]    数字
    [a-z],[[:lower:]]    小写字母
    [A-Z],[[:upper:]]    大写字母
    [a-zA-Z],[[:alpha:]]    字母
    [a-zA-Z0-9],[[:alnum:]]    数字+字母
    [[:space:]]   空白字符
    [[:punct:]]    标点符号
    {m,n}    至少m次,最多n次
    *    匹配前面字符任意字符任意次
    ?    匹配前面字符0次或1次
    +    匹配前面字符1次或多次
    |    或者
    ( ),\1, \2    分组
    ^    锚定行首
    $    锚定行尾
    \<,\b    锚定词首
    \>,\b    锚定词尾

三、sed常用编辑命令
    p:在屏幕上打印目标文本内容和模式匹配的内容
    d:删除匹配的内容并将内容打印到屏幕
    !d:删除非匹配的内容并将内容打印到屏幕
    a \Text:添加内容到匹配的内容下一行,使用\n能添加下一行
    i \Text:添加内容到匹配的内容前面一行
    c \Text: 将符合条件的内容替换为指定的文本
    r /path/to/file:在匹配内容处加入其他文件的内容
    w /path/to/file:将匹配内容保存至指定文件中
    =:打印匹配到内容的行号
------------------------------------------
    p:在屏幕上打印目标文本内容和模式匹配的内容
?
1
2
3
4
5
6
7
[iyunv@TESTHOST scripts]# cat test
# The first line.
The second one.
[iyunv@TESTHOST scripts]# sed /^#/p /scripts/test    #未使用-n时,不仅打印匹配内容,文件所有内容也会打印到屏幕
# The first line.
# The first line.
The second one.



    d:删除匹配的内容并将内容打印到屏幕
?
1
2
3
4
5
[iyunv@TESTHOST scripts]# cat test
# The first line.
The second one.
[iyunv@TESTHOST scripts]# sed /^#/d /scripts/test    #删除以井号开头的行
The second one.



    !d:删除非匹配的内容并将内容打印到屏幕
?
1
2
3
4
5
[iyunv@TESTHOST scripts]# cat test
# The first line.
The second one.
[iyunv@TESTHOST scripts]# sed '/^#/!d' /scripts/test    #删除非井号开头的行
# The first line.



    a \Text:添加内容到匹配的内容下一行,使用\n能添加下一行
?
1
2
3
4
5
6
7
[iyunv@TESTHOST scripts]# cat test
# The first line.
The second one.
[iyunv@TESTHOST scripts]# sed '/^#/a \next line' /scripts/test    #内容插入到井号开头的行下一行
# The first line.
next line
The second one.



    i \Text:添加内容到匹配的内容前面一行
?
1
2
3
4
5
6
7
8
[iyunv@TESTHOST scripts]# cat test
# The first line.
The second one.
[iyunv@TESTHOST scripts]# sed '/^#/i \previous line' /scripts/test    #内容插入到井号开头行的上一行
previous line
# The first line.
The second one.
You have new mail in /var/spool/mail/root



    r /path/to/somefile:在匹配内容处加入其他文件的内容
?
1
2
3
4
5
6
7
8
9
[iyunv@TESTHOST scripts]# cat test
# The first line.
The second one.
[iyunv@TESTHOST scripts]# sed '/^#/r /etc/issue' /scripts/test    #把issue文件的内容加入匹配内容后
# The first line.
CentOS release 6.5 (Final)    #issue文件中内容
Kernel \r on an \m    #issue文件中内容
                      #issue文件中内容
The second one.



    c \Text:将匹配内容替换为指定的文本
?
1
2
3
4
5
6
7
[iyunv@TESTHOST scripts]# !c
cat test
# The first line.
The second one.
[iyunv@TESTHOST scripts]# sed '/^#/c \Replace line.' /scripts/test    #替换内容
Replace line.
The second one.



    w /path/to/file:将匹配内容保存至指定文件中
?
1
2
3
4
5
6
7
[iyunv@TESTHOST scripts]# ls
test
[iyunv@TESTHOST scripts]# sed '/^#/w /scripts/new' /scripts/test
# The first line.
The second one.
[iyunv@TESTHOST scripts]# cat new    #匹配的内容已经添加到new文件中,经测试,如果文件中存在内容,此命令相当于>输出重定向,而不是>>追加
# The first line.



    =:打印匹配到内容的行号
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[iyunv@TESTHOST scripts]# cat test
# The first line.
# The second one.
# Last line.
111111111111111
222222222222222
333333333333333
# New line.
[iyunv@TESTHOST scripts]# sed /^#/= /scripts/test    #匹配到内容所在行的行号
1
# The first line.
2
# The second one.
3
# Last line.
111111111111111
222222222222222
333333333333333
7
# New line.



四、sed常用选项
    -n:静默模式,仅在屏幕打印出模式匹配的内容
    -e:在一个sed命令中使用多个/pattern/,如sed –e /pattern1/ –e /pattern2/ –e /pattern3/ ······ /path/to/file1 /path/to/file2 ······,例如
    -i:直接修改文件内容
    -r:使用扩展正则表达式元字符
    -f:从文件中读取处理脚本,并执行;sed -f sed_scripts8 /path/to/file1 /path/to/file2 ······
------------------------------------------
    -n:静默模式,仅在屏幕打印出模式匹配的内容
?
1
2
3
4
5
6
7
8
9
10
11
12
[iyunv@TESTHOST scripts]# cat test
# The first line.
# The second line.
# Last line.
[iyunv@TESTHOST scripts]# sed /first/p /scripts/test    #默认除了打印匹配的内容,文件内容也将打印到屏幕
# The first line.
# The first line.
# The second line.
# Last line.
[iyunv@TESTHOST scripts]# sed -n /first/p /scripts/test    #仅打印匹配的内容
# The first line.
[iyunv@TESTHOST scripts]#




    -e:在一个sed命令中使用多个/pattern/
?
1
2
3
4
5
6
7
8
[iyunv@TESTHOST scripts]# which ls
alias ls='ls --color=auto'
    /bin/ls
[iyunv@TESTHOST scripts]# which ls | sed '/^alias/d'
    /bin/ls
[iyunv@TESTHOST scripts]# which ls | sed -e'/^alias/d' -e s#^[[:space:]]##    #使用多个/pattern/
/bin/ls        
[iyunv@TESTHOST scripts]#




    -i:直接修改文件内容
?
1
2
3
4
5
6
7
8
9
[iyunv@TESTHOST scripts]# cat test
# The first line.
The second line.
Last line.
[iyunv@TESTHOST scripts]# sed -i 's/The first line./The new line./' /scripts/test    #将文件中“The first line.”修改为“The new line.”
[iyunv@TESTHOST scripts]# cat test    #已被修改
# The new line.
The second line.
Last line.




    -f:从文件中读取处理脚本,并执行;sed -f sed_scripts8 /path/to/file1 /path/to/file2 ······
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[iyunv@TESTHOST scripts]# ls
new  sed.script1  test
[iyunv@TESTHOST scripts]# cat sed.script1    #创建一个脚本,里面每行是一个匹配模式或命令
/^#/p
/second/w /scripts/new2
[iyunv@TESTHOST scripts]# sed -f /scripts/sed.script1 /scripts/test    #使用脚本文件执行命令
# The new line.
# The new line.
The second line.
Last line.
[iyunv@TESTHOST scripts]# ls
new  new2  sed.script1  test
[iyunv@TESTHOST scripts]# cat new2    #第二条命令执行结果--->成功
The second line.



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-61370-1-1.html 上篇帖子: awk的简单使用方法 下篇帖子: python输出重定向 Linux
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表