正則表達式是一個字符串,它可以用來描述幾個字符序列。使用正則表達式是由幾個不同的Unix命令,包括 ed, sed, awk, grep,并且,在較為有限的程度上擴展 vi.
本教程將教你如何使用正則表達式使用 sed.
這里流編輯器sed的代表是面向流的編輯器,它是專門用于執(zhí)行腳本創(chuàng)建。因此,所有的輸入送入通過到stdout,它不會改變輸入文件。
在我們開始之前,讓我們確保你有一個本地副本 /etc/passwd 文件的文本文件,用sed。
正如前面提到的,可以調(diào)用sed的發(fā)送數(shù)據(jù)通過管道如下:
$ cat /etc/passwd | sed Usage: sed [OPTION]... {script-other-script} [input-file]... -n, --quiet, --silent suppress automatic printing of pattern space -e script, --expression=script ...............................
cat命令轉(zhuǎn)儲 /etc/passwd文件的內(nèi)容通過管道進入sed 模式空間sed 。是內(nèi)部工作模式空間緩沖區(qū),sed使用做其工作。
以下是 sed 的一般語法
/pattern/action
在這里,模式是一個正則表達式,動作是下表中給出的命令之一。如果省略模式,執(zhí)行操作的每一行,正如我們上面看到的。
斜線字符(/),環(huán)繞模式是必需的,因為它們被用來作為分隔符。
Range | 描述 |
---|---|
p | Prints the line |
d | Deletes the line |
s/pattern1/pattern2/ | Substitutes the first occurrence of pattern1 with pattern2. |
再次調(diào)用sed ,但這個時候告訴sed使用編輯命令刪除行,由單字母d表示:
$ cat /etc/passwd | sed 'd' $
調(diào)用sed 發(fā)送文件,通過管道,而是可以指示sed來讀取數(shù)據(jù)文件,在下面的例子。
下面的命令做完全一樣的東西,以前的嘗試,沒有 cat 命令:
$ sed -e 'd' /etc/passwd $
SED還了解到一種叫做地址。位址是特定的地點,在一個文件或一個特定的編輯命令應(yīng)適用范圍。當sed遇到?jīng)]有地址,在該文件中的每一行上執(zhí)行其操作。
以下命令將sed 命令你已經(jīng)使用了一個基本的地址:
$ cat /etc/passwd | sed '1d' |more daemon:x:1:1:daemon:/usr/sbin:/bin/sh bin:x:2:2:bin:/bin:/bin/sh sys:x:3:3:sys:/dev:/bin/sh sync:x:4:65534:sync:/bin:/bin/sync games:x:5:60:games:/usr/games:/bin/sh man:x:6:12:man:/var/cache/man:/bin/sh mail:x:8:8:mail:/var/mail:/bin/sh news:x:9:9:news:/var/spool/news:/bin/sh backup:x:34:34:backup:/var/backups:/bin/sh $
請注意,數(shù)字1之前添加刪除編輯命令。這告訴sed執(zhí)行編輯命令的第一行上的文件。在這個例子中,sed將刪除第一行 /etc/password,并打印文件的其余部分。
所以如果你想從文件中刪除多個行?用sed,您可以指定一個地址范圍如下:
$ cat /etc/passwd | sed '1, 5d' |more games:x:5:60:games:/usr/games:/bin/sh man:x:6:12:man:/var/cache/man:/bin/sh mail:x:8:8:mail:/var/mail:/bin/sh news:x:9:9:news:/var/spool/news:/bin/sh backup:x:34:34:backup:/var/backups:/bin/sh $
上面的命令將開始從1至5的所有行。所以,刪除前五行。
試試下面的地址范圍:
Range | 描述 |
---|---|
'4,10d' | Lines starting from 4th till 10th are deleted |
'10,4d' | Only 10th line is deleted, because sed does not work in reverse direction. |
'4,+5d' | This will match line 4 in the file, delete that line, continue to delete the next five lines, and then cease its deletion and print the rest |
'2,5!d' | This will deleted everything except starting from 2nd till 5th line. |
'1~3d' | This deletes the first line, steps over the next three lines, and then deletes the fourth line. Sed continues applying this pattern until the end of the file. |
'2~2d' | This tells sed to delete the second line, step over the next line, delete the next line, and repeat until the end of the file is reached. |
'4,10p' | Lines starting from 4th till 10th are printed |
'4,d' | This would generate syntax error. |
',10d' | This would also generate syntax error. |
注:使用p動作時,你應(yīng)該使用-n選項,以避免重復行式打印。檢查以下兩條命令之間的區(qū)別:
$ cat /etc/passwd | sed -n '1,3p'
檢查上面的命令沒有-n作為如下:
$ cat /etc/passwd | sed '1,3p'
替換命令,用s表示,將您指定的其他任何字符串中指定的任何字符串代替。
用一個字符串代替另一個,你需要有一些方式告訴sed,你的第一個字符串結(jié)束,并開始替換字符串。這是傳統(tǒng)上是由兩個字符串bookending斜線(/)字符。
首次出現(xiàn)一行字符串根字符串a(chǎn)mrood與下面的命令替代。
$ cat /etc/passwd <