sed 指令

直接取代 -2008/04/21
sed 3cxxx file 只是會顯示結果是如何給你看

如果這麼作 sed -i 3cxxx file,file 裡的第三行馬上會被更改

在 file裡的第三行後面加入一行xxx
sed 3axxx file


在 file裡的第三行前面加入一行xxx
sed 3ixxx file


將 file裡的第三行修改為 xxx
sed 3cxxx file


將 file裡的 old-string改為 new-string
sed s/old-string/new-string/g file


ref: http://phorum.study-area.org/viewtopic.php?p=223722#223722



將 zone.ssorc.tw檔案裡面的某一行是 "IN A"的全部字元,改成 "IN A 10.1.1.123"
sed -i "s/IN A.*/IN A 10.1.1.123/g" zone.ssorc.tw

如果是 perl

perl -pi -e 's/IN A.*/IN A 10.1.1.123/g' zone.ssorc.tw


取括號裡面的值
more test.txt

(aaa)
(bbb)
(ccc)

cat test.txt | sed 's/((.*))/1/'

圖 sed-1.jpg

attachments/200709/9374293338.jpg
aaa
bbb
ccc

   如果是有中括號的話
      test.txt

(aaa)
[bbb]
(ccc)

cat test.txt | sed -e 's/((.*))/1/' -e 's/[(.*)]/1/'

圖 sed-2.jpg

attachments/200709/3054665627.jpg
aaa
bbb
ccc

取某字串後的值
a="123 aaa bbb ccc"
echo $a | sed 's/^[0-9]{1,10}//'

另一種玩法
echo $a | sed 's/.*123//'

取某行到某行之間
   file.txt內容為

111111111111
222222222222
333333333333
444444444444
555555555555
666666666666
777777777777
888888888888
999999999999
000000000000

   作法: 取當中字串333與888之間的值
      cat file.txt | sed -ne '/33333/,/88888/p'

ref:
http://linux.tnc.edu.tw/techdoc/shell/x737.html
http://phi.sinica.edu.tw/aspac/reports/96/96005/

特殊符號
vi txt1.txt

xxx^@

file txt1.txt 為 ASCII text, with no line terminators

使用 sed -e 'l' 得知其 ASCII 碼
[root@test bin]# sed -e 'l' 321
xxx00$
xxx[root@test bin]#

可行作法一
sed -e 's/.*/& /' txt1.txt | awk '{print $1}' > txt2.txt

作法二
tr -d "00$" < txt1.txt > txt2.txt

作法三
tr -s "[00]" "[ ]" < txt1.txt  > txt2.txt

圖 tr.jpg

attachments/200710/1525475336.jpg
標籤: 指令
評論: 0 | 引用: 0 | 閱讀: 4120 | 列印 | 文件 | 轉發

發表評論
暱 稱: 密 碼:
網 址: E - mail:
驗證碼: 驗證碼圖片 選 項:
頭 像:
內 容:
  • 粗體
  • 斜體
  • 底線
  • 插入圖片
  • 超連結
  • 電子郵件
  • 插入引用
  • 表情符號