1. 文本处理

  sed编辑器根据sed命令处理数据流中的数据;在流编辑器将所有命令与一行数据匹配完后,它会读取下一行数据并重复以下过程:

    (1) 一次从输入中读取一行数据

    (2) 根据所提供的编辑器命令匹配数据

    (3) 按照命令修改流中的数据

    (4) 将新的数据输出到STDOUT

  格式:

    sed options script-command input-file

  选项:

-e script

在script中指定命令

-f file

从file文件中读取命令

-n

不产生命令输出,使用print命令完成输出

-i

将修改应用到文件

=

打印行号,行号由换行符决定

l

打印数据流中的文本和不可打印的ASCII字符

 命令:

s

替换行

sed '[address]/s/pattern/replacement/flags'

d

删除行

sed '[address]command'

i/a

插入/追加行

sed '[address]command\new line'

c

修改行

sed '[address]command\new line'

y

转换单字符

sed '[address]y/inchars/outchars/'

w

写入行

sed '[address]w test.file' date.txt

r

读取行

sed '[address]r test.file' date.txt

  定义编辑器命令:

    # echo "this is a test" | sed 's/test/big test/'

    this is a big test

  使用多个编辑器命令:

    # echo "this is a test" | sed 's/test/big test/;s/this/here/'

    here is a big test

  从文件中读取编辑器命令:

    #sed -f script1.sed data1.txt

  Tips:sed编辑器不会修改文本文件的数据,只会将修改后的数据发送到STDOUT;若想修改,可使用-i选项

2. sed编辑器基础 

【行寻址】

  将命令作用于特定行和某些行

  格式1:

    [address] command

  格式2:

    address {

      command1

      command2

      command3

    }

  数字方式的行寻址:

    最后一行的行号可使用$代替

    # sed '2s/dog/cat/' data.txt

    # sed '2,3s/dog/cat/' data.txt

    # sed '2,$s/dog/cat/' data.txt

  文本模式过滤器:

  /pattern/command

    # sed -n '/kim/s/bash/cash/p' /etc/passwd

    kim:x:1001:1001::/home/kim:/bin/cash

    Tips:使用正则表达式,可创建高级文本模式匹配表达式来匹配各种数据

  命令组合:

    可用花括号{}将多条命令组合在一起,行寻址方式如格式2

    # sed '/two/{s/quick/slow/;s/dog/cat/}' data.txt

【替换】

  格式:

    sed '[address]/s/pattern/replacement/flags'

  替换选项:

    s命令(substitude)可在行中替换文本,默认仅替换第一处

  替换标记:

  s/pattern/replacement/flags

    (1) 数字,表明替换第几处

    (2) g,替换所有

    (3) p,原先行的内容要打印出来(先打印修改的行,结合-n选项可实现只输出修改的行)

    (4) w file,替换结果写入文件

      # cat data.txt

      This is a test line.

      This is a different line.

      # sed 's/test/trail/w file.txt' data.txt

      This is a trail line.

      This is a different line.

      # cat file.txt

      This is a trail line.

  替换字符:

    sed允许选择其它字符来作为替换命令中的字符串分隔符

      # sed -n 's!/bin/bash!/bin/csh!p' /etc/passwd

      root:x:0:0:root:/root:/bin/csh

      fonsview:x:1000:1000::/home/fonsview:/bin/csh

    Tips:  替换命令结合正则表达式中的回溯引用,可实现删除一行中的特定部分

      # echo "math: a+b=c" | sed -e '/math/s/\(.*=\).*/\1ab/'

      math: a+b=ab

【删除行】

  格式:

    sed '[address]command'

  删除文本流中的特定行,可以用删除命令d

    # sed '/two/,/three/d' data.txt

    The quick brown fox jumps over the lazy dog one     

    The quick brown fox jumps over the lazy dog four

【插入和追加行】

  格式:

    sed '[address]command\new line'

    (1) 插入insert会在指定行前增加新行,命令i

    (2) 追加append会在指定行后增加新行,命令a

      # sed '1i\hello,\world!' data.txt

      # sed '/two/i\hello,world!' data.txt  

【修改行】

  修改(change)命令允许修改数据流中整行文本的内容

  格式:

    sed '[address]command\new line'

    # sed '1c\dog' data.txt

    # sed '/two/c\dog\cat' data.txt

【转换】

  格式:

    sed '[address]y/inchars/outchars/'

  转换(transform)命令(y)是唯一可以处理单个字符的sed命令,对inchars和outchars进行一对一映射

    # sed 'y/1234/abcd/' data.txt    

    The quick brown fox jumps over the lazy dog a one     

    The quick brown fox jumps over the lazy dog b two

    The quick brown fox jumps over the lazy dog c three

    The quick brown fox jumps over the lazy dog d four

【写入文本】

  格式:

    sed '[address]w test.file' date.txt

    # sed -n '/one/,/three/w text.txt' data.txt

    # cat text.txt

    The quick brown fox jumps over the lazy dog 1 one     

    The quick brown fox jumps over the lazy dog 2 two

    The quick brown fox jumps over the lazy dog 3 three

【读取数据】

  读取(read)命令允许你将一个独立文件中的数据插入到数据流中,指定的地址行之后

  格式:

    sed '[address]r test.file' date.txt

    # sed '2r text.txt' data.txt

    The quick brown fox jumps over the lazy dog 1 one     

    The quick brown fox jumps over the lazy dog 2 two

    hello,world!

    The quick brown fox jumps over the lazy dog 3 three

    The quick brown fox jumps over the lazy dog 4 four

最新文章

  1. java HashMap那点事
  2. android ANR问题
  3. 关于Android Studio里的Gradle,你所需要知道的都在这里了
  4. hdu 5154 Harry and Magical Computer
  5. ganglia对于tomcat进程的res内存监控扩展
  6. ABAP 日期函数
  7. ehcache集群的配置
  8. FastJson中@JSONField注解使用
  9. mysql自动备份删除5天前的备份
  10. 淘宝tairKV分布式
  11. [HNOI2002]跳蚤
  12. Spring py登陆模块(包含 记录登陆时间,记录ip,增加积分)
  13. selenium-弹窗操作(八)
  14. consul分布式集群搭建
  15. 后台调用前台js方法
  16. jsoi2018 R1R2
  17. Android Studio 上传aar(Library)到JCenter
  18. 【Algorithm】冒泡排序
  19. excel自定义函数添加和使用方法
  20. Scala实战高手****第11课:Scala面向接口彻底实战和Spark源码鉴赏

热门文章

  1. bootstrap-table的简单使用
  2. for in和for of的区别
  3. 微信小程序转发事件
  4. body测试onclick等鼠标事件无效果详解
  5. Image Processing and Analysis_8_Edge Detection:A Computational Approach to Edge Detection——1986
  6. shiro系列一、认识shiro
  7. SVN建立分支和合并代码
  8. Python&Selenium 关键字驱动测试框架之数据文件解析
  9. 定时备份etc目录
  10. python_lambda表达式(匿名函数)