【sed】基本用法

 1. 文本處理

  sed編輯器根據sed命令處理數據流中的數據;在流編輯器將全部命令與一行數據匹配完後,它會讀取下一行數據並重復如下過程:正則表達式

    (1) 一次從輸入中讀取一行數據bash

    (2) 根據所提供的編輯器命令匹配數據app

    (3) 按照命令修改流中的數據編輯器

    (4) 將新的數據輸出到STDOUTui

  格式:this

    sed options script-command input-filespa

  選項:orm

-e scriptthree

在script中指定命令ip

-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

相關文章
相關標籤/搜索