zoukankan      html  css  js  c++  java
  • 【sed】基本用法

     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 ew line'

    c

    修改行

    sed '[address]command ew 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 ew line'

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

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

          # sed '1ihello,world!' data.txt

          # sed '/two/ihello,world!' data.txt  

    【修改行】

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

      格式:

        sed '[address]command ew line'

        # sed '1cdog' data.txt

        # sed '/two/cdogcat' 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

  • 相关阅读:
    HDU 1114 Piggy-Bank
    HDU 2955 Robberies
    NTOJ 290 动物统计(加强版)
    POJ 3624 Charm Bracelet
    HDU 2602 Bone Collector
    POJ 1523 SPF(无向图割顶)
    HDU 5311 Hidden String
    HDU 1421 搬寝室
    HDU 1058 Humble Numbers
    POJ 3259 Wormholes(spfa判负环)
  • 原文地址:https://www.cnblogs.com/MrReboot/p/10925739.html
Copyright © 2011-2022 走看看