Linux

我怎樣才能從文件中刪除一些東西?

  • September 21, 2010

我想從文件的每一行中刪除一些東西,例如:-

我在文件中有以下路徑:/var/lib/svn/repos/b1me/products/payone/generic/code/core/db/fs-type /var/lib/svn/repos/b1me/products/payone/generic /code/fees/db/fs-type /var/lib/svn/repos/b1me/products/payone/generic/code/merchantserver/db/fs-type

我想做點什麼變成 /var/lib/svn/repos/b1me/products/payone/generic/code/core/ /var/lib/svn/repos/b1me/products/payone/generic/code/fees/ / var/lib/svn/repos/b1me/products/payone/generic/code/merchantserver/

sed -ie 's/db\/fs-type//g' FILENAME
or cat FILENAME | sed -e 's/db\/fs-type//g'

如何:http : //www.grymoire.com/Unix/Sed.html

聽起來您想要做的是簡單的替換,為此,使用帶有簡單正則表達式匹配的 sed 之類的工具是實現目標的經典方法。一個簡單的搜尋就會出現大量連結,但這裡有兩個可以幫助您開始使用:

通常在文件中使用 sed 進行字元串替換: http ://www.cs.hmc.edu/tech_docs/qref/sed.html

一個更具體針對您的情況的範例:http: //www.computing.net/answers/unix/replace-a-complex-string-using-sed/5811.html

這是要解決的最常見的問題之一,解決方案在 Linux 社區中相當普遍。

引用自:https://serverfault.com/questions/183160