e21e1 发表于 2015-4-28 08:27:35

Linux之sed

一、sed简介
二、sed语法
    附:正则表达式元字符
三、sed常用编辑命令
四、sed常用选项


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


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

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

    1、可以仅仅是模式匹配的行
?

1
2
3
4
# 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
# 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
# which ls
alias ls='ls --color=auto'
    /bin/ls
# which ls | sed '/^alias/!s#^[[:space:]]##'   
alias ls='ls --color=auto'
/bin/ls
# which ls | sed '/^alias/d;s#^[[:space:]]##'    #先去掉alias开头的行,然后搜索替换去掉空白字符
/bin/ls





附:正则表达式元字符

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

    2、扩展正则表达式
    .    任意单个字符
    []    范围内
    [^]   范围外,例如非空白字符[^[:space:]]
    ,[[:digi:]]    数字
    ,[[:lower:]]    小写字母
    ,[[:upper:]]    大写字母
    ,[[:alpha:]]    字母
    ,[[: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
# cat test
# The first line.
The second one.
# sed /^#/p /scripts/test    #未使用-n时,不仅打印匹配内容,文件所有内容也会打印到屏幕
# The first line.
# The first line.
The second one.




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

1
2
3
4
5
# cat test
# The first line.
The second one.
# sed /^#/d /scripts/test    #删除以井号开头的行
The second one.




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

1
2
3
4
5
# cat test
# The first line.
The second one.
# sed '/^#/!d' /scripts/test    #删除非井号开头的行
# The first line.




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

1
2
3
4
5
6
7
# cat test
# The first line.
The second one.
# sed '/^#/a \next line' /scripts/test    #内容插入到井号开头的行下一行
# The first line.
next line
The second one.




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

1
2
3
4
5
6
7
8
# cat test
# The first line.
The second one.
# 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
# cat test
# The first line.
The second one.
# 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
# !c
cat test
# The first line.
The second one.
# sed '/^#/c \Replace line.' /scripts/test    #替换内容
Replace line.
The second one.




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

1
2
3
4
5
6
7
# ls
test
# sed '/^#/w /scripts/new' /scripts/test
# The first line.
The second one.
# 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
# cat test
# The first line.
# The second one.
# Last line.
111111111111111
222222222222222
333333333333333
# New line.
# 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
# cat test
# The first line.
# The second line.
# Last line.
# sed /first/p /scripts/test    #默认除了打印匹配的内容,文件内容也将打印到屏幕
# The first line.
# The first line.
# The second line.
# Last line.
# sed -n /first/p /scripts/test    #仅打印匹配的内容
# The first line.
#





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

1
2
3
4
5
6
7
8
# which ls
alias ls='ls --color=auto'
    /bin/ls
# which ls | sed '/^alias/d'
    /bin/ls
# which ls | sed -e'/^alias/d' -e s#^[[:space:]]##    #使用多个/pattern/
/bin/ls      
#





    -i:直接修改文件内容
?

1
2
3
4
5
6
7
8
9
# cat test
# The first line.
The second line.
Last line.
# sed -i 's/The first line./The new line./' /scripts/test    #将文件中“The first line.”修改为“The new line.”
# 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
# ls
newsed.script1test
# cat sed.script1    #创建一个脚本,里面每行是一个匹配模式或命令
/^#/p
/second/w /scripts/new2
# sed -f /scripts/sed.script1 /scripts/test    #使用脚本文件执行命令
# The new line.
# The new line.
The second line.
Last line.
# ls
newnew2sed.script1test
# cat new2    #第二条命令执行结果--->成功
The second line.



页: [1]
查看完整版本: Linux之sed