文章目錄
- shell中的文本處理指令:sed指令
-
- 1.什麼是sed
- 2.sed指令選項
- 3.幾種調用sed的方式
-
- 1.在指令行調用sed
-
- 1.單句替換
- 2.對于整個檔案進行替換
- 3.-e:在一個指令行調用多個編輯器,同時進行多個替換
- 2.-f:在檔案中調用編輯器
- 4.s 替換指令
-
- 腳本替換中的幾個替換标記
-
- 1.g 替換文本中每處比對模式
- 2.num(例如2)
- 3.參數p和-n:隻輸出被指令修改過的行
- 4.w 将輸出儲存在檔案中(隻包含操作替換的行)
- 5.替換字元
-
- 方法1:用\轉義符号進行轉義
- 方法2:将替換指令中的/改為!
- 方法3:将替換指令中的/改為#
- 6.行尋址
-
- 1.指定替換的行
- 2.利用正規表達式
- 3.利用組合指令
- 7.d 删除行
- 8.a和i 插入資料
-
- i 在指定行前插入新的行
- a 在指定行後追加新的行
- 對檔案的指定行插入
- $a 将新行插入在多行資料流末尾
- 在第一行之前插入多行資料
- 9.c 修改
-
- 1.修改指定行
- 2.修改含有指定資訊的行
- 3.修改多行
- 10.y 替換指令
- 11.列印行 p
- 12.列印行号 ‘=’
- 13.sed和檔案一起工作
-
- w 向檔案中寫入指定行
- r 從檔案中讀取
shell中的文本處理指令:sed指令
1.什麼是sed

sed的優點:sed速度快。在啟動vim的時間,sed已經完成了需要執行的操作
2.sed指令選項
3.幾種調用sed的方式
1.在指令行調用sed
1.單句替換
s/要替換的部分/替換成的部分
echo "This is a test" | sed 's/test/testing/' #s/要替換的部分/替換成的部分
2.對于整個檔案進行替換
sed 's/dog/fox/' test1
sed指令隻是改變了輸出内容,沒有改變檔案内容
3.-e:在一個指令行調用多個編輯器,同時進行多個替換
sed -e 's/dog/tiger/;s/cat/bird/' test1 #中間用分号割開
#或者可以:
sed -e '
> s/white/red/
> s/dog/tiger/
> s/cat/bird/' test1 #這種方式不需要分号
2.-f:在檔案中調用編輯器
sed -f file test1 #-f 指令檔案 要替換的檔案
4.s 替換指令
腳本替換中的幾個替換标記
不加任何參數時預設替換每句中出現第一次的關鍵字
sed 's/test/trial/' test2
1.g 替換文本中每處比對模式
替換所有比對到的關鍵字
sed 's/test/trial/g' test2
2.num(例如2)
加具體的num參數,替換每句中出現第n次的關鍵字:
sed 's/test/trial/2' test2
3.參數p和-n:隻輸出被指令修改過的行
p顯示指定句,通常和-n搭配使用;
-n:預設不顯示所有内容
sed -n 's/test/trial/p' test3 #隻顯示修改部分
sed -n 's/number/num/' test4 #使用-n而不使用p參數時,不會有顯示
sed -n '/number3/p' test4 #顯示比對到的含有number3的行
sed -n '2,4p' test4 #顯示2到4行
4.w 将輸出儲存在檔案中(隻包含操作替換的行)
sed 's/test/trial/w haha' test3 #将修改的句子寫入haha檔案
5.替換字元
方法1:用\轉義符号進行轉義
sed 's/bin\/bash/bin\/csh/' passwd
方法2:将替換指令中的/改為!
sed 's!bin/bash!bin/csh!' passwd
方法3:将替換指令中的/改為#
sed 's#bin/bash#bin/csh#' passwd
6.行尋址
如果想讓指令隻作用在指定的行
1.指定替換的行
sed '2s/dog/tiger/' test1 #指定替換第二行
sed '2,4s/dog/tiger/' test1 #指定替換第2到4行
2.利用正規表達式
sed '2,$s/dog/tiger/' test1 #指定替換第二行到最後
3.利用組合指令
[[email protected] sed]# sed '2{ #多個要求替換第二行
> s/dog/tiger/
> s/white/red/
> s/cat/bird/
> }' test1
7.d 删除行
沒有改變源檔案的内容,隻是删除了輸出内容
sed 'd' test4 #删除全部
sed '2d' test4 #删除第二行
sed '4d' test4 #删除第四行
sed '2,4d' test4 #删除第2到4行
sed '2,$d' test4 #删除第二行到結束的行
sed '/number 1/d' test4 #删除字元所在的行
sed '/number* 1/d' test4 #與正規表達式結合
8.a和i 插入資料
i和a:插入和追加
i 在指定行前插入新的行
echo 'test 2' | sed 'i\test line1' #i表示插入到前面
a 在指定行後追加新的行
echo 'test 2' | sed 'a\test line1' #a表示追加到末尾
對檔案的指定行插入
sed '3i\test 1' test4 #插入到第三行前面
sed '3a\test 1' test4 #追加到第三行後面
$a 将新行插入在多行資料流末尾
sed '$a\This is an inserted line.\nThis is another inserted line.' test4 #\n是換行。不加則兩句話寫在一行
sed '$a\This is an inserted line.This is another inserted line.' test4
在第一行之前插入多行資料
[[email protected] sed]# sed '1i \
> new line1.\
> new line2.\
> new line3.' test4
9.c 修改
1.修改指定行
sed '3c\new line3.' test4
2.修改含有指定資訊的行
sed '/number 1/c\new line3.' test4
3.修改多行
sed '2,4c\new line.' test4
注意:此處的修改多行為同時修改,而不是逐行修改
10.y 替換指令
唯一一個可以處理單個字元的sed指令
sed 'y/123/789/' test4
當一行中包含多個要修改的單個字元時,預設全部修改:
echo "This 1 is a test of 1 try." | sed 'y/123/789/'
11.列印行 p
echo "this is a test"|sed 'p'
sed -n '/number 3/p' test4 #禁止其他行,隻列印比對到的行
sed -n '2,4p' test4
sed '2,4p' test4
12.列印行号 ‘=’
sed '=' test4
13.sed和檔案一起工作
w 向檔案中寫入指定行
sed '1,3w test' test4
r 從檔案中讀取
将一個獨立的檔案插入到一個資料流中。
sed '3r test5' test4 #将test5添加到test4中第三行後面
#添加到包含指定資訊行的後面
sed '/number2/r test5' test4