sed用法小結

quote.txt

The honeysuckle hand played all night long for only $90.

It was an evening of splendid music and company.

Too bad the disco floor fell through at 23:10.

The local nurse Miss P.Neave was in attendance.

顯示行

$sed -n '2p' quote.txt

範圍

$sed -n '1,3p' quote.txt

匹配單詞

$sed -n '/The/'p quote.txt

匹配某行單詞

$sed -n '4,/The/'p quote.txt

匹配元字符

$sed -n '/\$/'p quote.txt

顯示整個文件

$sed -n '1,$p' quote.txt    sed -n '1,$'p quote.txt

任意字符

$sed -n '/.*ing/'p quote.txt  任意字符出現一次或N次 以ing結尾

首末行

$sed -n '1p' quote.txt

$sed -n '$p' quote.txt

打印行號

$sed -n '/music/=' quote.txt

2

$sed -n -e '/music/p' -e '/music/=' quote.txt

It was an evening of splendid music and company.

2

附加文件

!/bin/sed -f

/文件中匹配的字符/ a\

插入的字符 +x; *.sed quote.txt

刪除行

$sed '1d' quote.txt 刪除第一行

$sed '1,3d' quote.txt 刪除1到3行

$sed '$d' quote.txt 刪除最後一行

$sed '/Neave/d' quote.txt 刪除帶Neave的行

替換文本

$sed 's/night/NIGHT/' quote.txt  s替換/night/NIGHT/

$sed 's/\$//' quote.txt  $換成空格

$sed 's/The/Wow!/g' quote.txt    g替換所有

$sed 's/splendid/SPLENDID/w sed.out' quote.txt  w sed.out此行寫入文件

$sed -n 's/nurse/"Hello" &/p' quote.txt  

The local "Hello" nurse Miss P.Neave was in attendance.

$sed -n 's/played/from Hockering &/p' quote.txt

將sed結果寫入文件命令

$sed '1,2 w filedt' quote.txt 第一二行寫入文件

$sed '/Neave/ w dht' quote.txt 匹配//的寫入文件

從文件中讀文本

$sed '/company./r sedex.txt' quote.txt

在quote.txt的company後換行,寫入sedex.txt的內容

匹配後退出

$sed '/.a.*/q' quote.txt

任意字符後跟a,再跟任意字符0次或任意次匹配:

Line 1.band  Line 2.bad  Liner3.was  Line 4.was

The honeysuckle hand played all night long for only $90.

vi dos.txt

12332##DISO##45.12^M

00332##LPSO##23.11^M

01299##USPD##34.46^M

用一個空格替換所有的##,刪除最前邊的0,刪除尾部^M

$sed 's/##*/ /g' dos.txt

$sed 's/^0*/ /g' dos.txt

$sed 's/\^M//g' dos.txt

$cat dos.txt |sed 's/##*/ /g'|sed 's/^0*/ /'|sed 's/\^M//g'

處理報文輸出

Database      Size(MB)   Date Created

-------------------------------------

GOSOUTH       2244       12/11/97

TRISUD        5632       8/9/99

(2 rows affected)

1>使用s/-*//g刪除 -----------

2>使用/^$/d刪除空行

3>使用$d刪除最後一行

4>使用1d刪除第一行

5>使用awk {print $1}打印第一列

cat sql.txt | sed 's/--*//g' | sed '/^$/d' | sed '$d' | sed '1d' | awk '{print $1}'

去除行首數字

$vi UNH.txt

12345UND SPLLFC 234344

9999999UND SKKLT 3423

1UND SPLLY 434

$sed 's/^[0-9]*//g' UND.txt

附加文本每一行末尾加上字符串'passwd'

vi ok.txt

AC456

AC492169

AC9967

AC88345

$sed 's/[0-9]*/& Passwd/g' ok.txt

從sed輸出中設置shell變量

$NAME="It's a go situation"

$REPLACE="GO"

$NEW_NAME='echo $NAME | sed "s/go/$REPLACE/g"'

$echo $NEW_NAME

快速一行命令

's/\.$//g'       刪除以.結尾的行

'-e /abcd/d'     刪除包含abcd的行

's/[][][]*/[]/g' 刪除一個以上的空格,用一個空格代替

's/^[][]*//g'    刪除行首空格

's/\.[][]*/[]//g 刪除.後跟2或多個空格,以一個空格代替

's/COL\(...\)//g'刪除COL和它後邊的3個字母的行

's/^\//g'        刪除第一個\

's/[]/[]//g'     刪除所有空格並用tab替代

's/^[]//g'       刪除行首tab鍵

's/[]*//g'       刪除所有tab鍵

插入文本

$echo "Mr Willis"|sed 's/Mr/& Bruce/g'

刪除首字符

$echo "attkla.dc"|sed 's/^./g'

刪除文件擴展名

$echo "attkla.dc"|sed 's/.dc//g'

增加擴展名

$echo "attkla"|sed 's/$/.dc/g'

替換字符系列

$x="Department+payroll%Building G"

+ 換成of %換成located

$echo $x | sed 's/\+/ of /g' | sed 's/\%/ Located at /g '

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章