使用sed對檔案進行操作

來源:互聯網
上載者:User


使用sed對檔案進行操作 [plain] 一.附加文本    www.2cto.com  使用a\在指定行後面附加1行或多行;若不指定放置的位置,則預設放到每一行的後面。  附加文本時,不允許指定範圍,只允許一個地址模式。  附加格式:[address] a\  text\  text\  ...  text  注意:1.a\通知sed對a\後面的內容進行附加操作。2.每行後面都有"\",當sed執行到\時,將建立一個新行,並將內容添加進去。  3.最後一行不能有"\"。例子:如果將附加的第一行最後的"\"去掉,那麼運行指令碼將會報錯。pg append.sed #!bin/sed -f   # append text  /company/ a\  Then suddenly it happened.  _yeeXun.  執行之後:  sed -f append.sed quote.txt  sed: Unrecognized command: _yeeXun.    修改之後  pg append.sed  #!bin/sed -f    # append text  /company/ a\  Then suddenly it happened.\  _yeeXun.  執行指令碼:  sed -f append.sed quote.txt  The honeysuckle band played all night long for noly $90.  It was an evening of splendid music and company.  Then suddenly it happened.  _yeeXun.  Too bad the disco floor fell through at 23:10.  The local nurse Miss P.Neave was in attendance.    因為sed不與原檔案打交道,編輯的只是其一個拷貝,所以我們附加的資料不會寫到檔案中;  當執行上面的命令之後,我們查看下quote.txt檔案:  pg quote.txt  The honeysuckle band played all night long for noly $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.   二.修改文本  使用c\修改指定行,格式:  [address[,address]] c\  text\  text\  ...  text  address:行位置,可以使用Regex定位,也可以直接指定行號。  text:為替換的文本,注意最後一行沒有"\"。  例子  替換第三行資料:  pg change.sed  # change date  /bad/ c\  [Two good.]\  next goods.  運行指令碼:  sed -f change.sed quote.txt  The honeysuckle band played all night long for noly $90.  It was an evening of splendid music and company.  [Two good.]  next goods.  The local nurse Miss P.Neave was in attendance.   www.2cto.com  三.添加文本  在指定行前面添加資料。  [address] i\  text\  text\  ...  text  address:行位置,可以使用Regex定位,也可以直接指定行號。  text:為替換的文本,注意最後一行沒有"\"。  例如:  pg insert.sed  # insert data  /.*ing/ i\  [the insert date].  運行指令碼:  sed -f insert.sed quote.txt  The honeysuckle band played all night long for noly $90.  [the insert date].  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.    四.刪除文本  刪除檔案中的內容,刪除1行或者連續的多行,或者匹配某個模式的所有行格式:  [address[,address]] d  檔案資訊:  pg quote.txt  The honeysuckle band played all night long for noly $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 '1d' quote.txt  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 '1,3d' quote.txt  The local nurse Miss P.Neave was in attendance.    刪除最後一行:  sed '$d' quote.txt  The honeysuckle band played all night long for noly $90.  It was an evening of splendid music and company.  Too bad the disco floor fell through at 23:10.    使用Regex刪除合格所有行。  刪除包含以字母a開頭,以字母d結尾,總長為3的行,符合的有第一行(band),第二行(and)。  sed '/a.d/d' quote.txt  Too bad the disco floor fell through at 23:10.  The local nurse Miss P.Neave was in attendance.    五.替換文本,格式:  [address[,address]] s/pattern-to-find/replacement-pattern/[g p w n]  address:可選項  g:預設情況下,只替換首次出現模式,使用g替換全部所有出現的模式;  p:預設sed將所有被替換行寫入標準輸出,加p將使-n選項無效;-n不列印輸出結果。  w:檔案名稱,使用此選項將輸出定向到一個檔案。      www.2cto.com  將小寫floor替換為大寫的FLOOR:  sed 's/floor/FLOOR/' quote.txt  The honeysuckle band played all night long for noly $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 '/$.*/p' quote.txt  The honeysuckle band played all night long for noly $90.  刪除$(使用空替代)符號後的行:  sed -n 's/\$//p' quote.txt  The honeysuckle band played all night long for noly 90.    將首次出現的"The"單詞替換為"THE",並輸出到另一個檔案中  若sed.out存在則向裡面寫資料,所不存在則建立此檔案並寫資料:  sed 's/The/THE/w sed.out' quote.txt  THE honeysuckle band played all night long for noly $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.out:  pg sed.out  THE honeysuckle band played all night long for noly $90.  THE local nurse Miss P.Neave was in attendance.  --the end-- 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.