码迷,mamicode.com
首页 > 系统相关 > 详细

Linux之sed

时间:2015-04-27 23:58:35      阅读:442      评论:0      收藏:0      [点我收藏+]

标签:linux   sed   正则表达式元字符   

一、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、可以仅仅是模式匹配的行

[root@TESTHOST scripts]# sed ‘/^#/p‘ /scripts/test
# The first line.
# The first line.
 The second one.

    2、指定行,如1,7第一行到第7行,3,/^#/第3行到#开头的行

[root@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表示忽略大小写

[root@TESTHOST scripts]# which ls
alias ls=‘ls --color=auto‘
	/bin/ls
[root@TESTHOST scripts]# which ls | sed ‘/^alias/!s#^[[:space:]]##‘    
alias ls=‘ls --color=auto‘
/bin/ls
[root@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:在屏幕上打印目标文本内容和模式匹配的内容

[root@TESTHOST scripts]# cat test 
# The first line.
 The second one.
[root@TESTHOST scripts]# sed /^#/p /scripts/test    #未使用-n时,不仅打印匹配内容,文件所有内容也会打印到屏幕
# The first line.
# The first line.
 The second one.

    d:删除匹配的内容并将内容打印到屏幕

[root@TESTHOST scripts]# cat test
# The first line.
 The second one.
[root@TESTHOST scripts]# sed /^#/d /scripts/test    #删除以井号开头的行
 The second one.

    !d:删除非匹配的内容并将内容打印到屏幕

[root@TESTHOST scripts]# cat test
# The first line.
 The second one.
[root@TESTHOST scripts]# sed ‘/^#/!d‘ /scripts/test    #删除非井号开头的行
# The first line.

    a \Text:添加内容到匹配的内容下一行,使用\n能添加下一行

[root@TESTHOST scripts]# cat test
# The first line.
 The second one.
[root@TESTHOST scripts]# sed ‘/^#/a \next line‘ /scripts/test    #内容插入到井号开头的行下一行
# The first line.
next line
 The second one.

    i \Text:添加内容到匹配的内容前面一行

[root@TESTHOST scripts]# cat test
# The first line.
 The second one.
[root@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:在匹配内容处加入其他文件的内容

[root@TESTHOST scripts]# cat test
# The first line.
 The second one.
[root@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:将匹配内容替换为指定的文本

[root@TESTHOST scripts]# !c
cat test
# The first line.
 The second one.
[root@TESTHOST scripts]# sed ‘/^#/c \Replace line.‘ /scripts/test    #替换内容
Replace line.
 The second one.

    w /path/to/file:将匹配内容保存至指定文件中

[root@TESTHOST scripts]# ls
test
[root@TESTHOST scripts]# sed ‘/^#/w /scripts/new‘ /scripts/test
# The first line.
 The second one.
[root@TESTHOST scripts]# cat new    #匹配的内容已经添加到new文件中,经测试,如果文件中存在内容,此命令相当于>输出重定向,而不是>>追加
# The first line.

    =:打印匹配到内容的行号

[root@TESTHOST scripts]# cat test
# The first line.
# The second one.
# Last line.
111111111111111
222222222222222
333333333333333
# New line.
[root@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:静默模式,仅在屏幕打印出模式匹配的内容

[root@TESTHOST scripts]# cat test
# The first line.
# The second line.
# Last line.
[root@TESTHOST scripts]# sed /first/p /scripts/test    #默认除了打印匹配的内容,文件内容也将打印到屏幕
# The first line.
# The first line.
# The second line.
# Last line.
[root@TESTHOST scripts]# sed -n /first/p /scripts/test    #仅打印匹配的内容
# The first line.
[root@TESTHOST scripts]#

 

    -e:在一个sed命令中使用多个/pattern/

[root@TESTHOST scripts]# which ls
alias ls=‘ls --color=auto‘
	/bin/ls
[root@TESTHOST scripts]# which ls | sed ‘/^alias/d‘
	/bin/ls
[root@TESTHOST scripts]# which ls | sed -e‘/^alias/d‘ -e s#^[[:space:]]##    #使用多个/pattern/
/bin/ls        
[root@TESTHOST scripts]#

 

    -i:直接修改文件内容

[root@TESTHOST scripts]# cat test
# The first line.
 The second line.
 Last line.
[root@TESTHOST scripts]# sed -i ‘s/The first line./The new line./‘ /scripts/test    #将文件中“The first line.”修改为“The new line.”
[root@TESTHOST scripts]# cat test    #已被修改
# The new line.
 The second line.
 Last line.

 

    -f:从文件中读取处理脚本,并执行;sed -f sed_scripts8 /path/to/file1 /path/to/file2 ······

[root@TESTHOST scripts]# ls
new  sed.script1  test
[root@TESTHOST scripts]# cat sed.script1    #创建一个脚本,里面每行是一个匹配模式或命令 
/^#/p
/second/w /scripts/new2
[root@TESTHOST scripts]# sed -f /scripts/sed.script1 /scripts/test    #使用脚本文件执行命令 
# The new line.
# The new line.
 The second line.
 Last line.
[root@TESTHOST scripts]# ls
new  new2  sed.script1  test
[root@TESTHOST scripts]# cat new2    #第二条命令执行结果--->成功
 The second line.

本文出自 “Arvin Lau” 博客,请务必保留此出处http://64314491.blog.51cto.com/2784219/1639404

Linux之sed

标签:linux   sed   正则表达式元字符   

原文地址:http://64314491.blog.51cto.com/2784219/1639404

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!