2010-09-21 52 views
0

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

我在文件中的以下路徑:

的/ var/lib中/ SVN /回購/ b1me /產品/ payone /通用/ 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 /通用/代碼/ merchantserver/DB/FS型

我想要做的事,成爲

/var/lib/svn/repos/b1me/products/payone/generic/code/core/var/lib/svn/repos/b1me/products/payone/generic/code/fees// var/lib/SVN /回購/ b1me /產品/ payone /通用/代碼/ merchantserver/

+1

所以,你想刪除'db/fs-type'部分,對嗎? – Piskvor 2010-09-21 13:23:51

+0

是的,我想刪除db/fs型部分 – 2010-09-21 13:31:05

回答

4
sed 's#db/fs-type$##' myfile > myalteredfile 
+0

+1,打我幾秒鐘 – 2010-09-21 13:26:38

+0

請你可以給我舉例 – 2010-09-21 13:26:50

+1

你的sed不會刪除文件中每一行的字符串的所有出現,添加'g'開關... – eumiro 2010-09-21 13:31:29

0

你可以用一些編輯器(如納米,VI,VIM等)的文件,重寫或你可以按照這個網站的說明:

http://www.liamdelahunty.com/tips/linux_search_and_replace_multiple_files.php 

或者用一個簡單的grep替換字符串,像這樣:

http://www.debianadmin.com/howto-replace-multiple-file-text-string-in-linux.html 

當我說改寫,我的意思是將「 - 」爲「」 ......

0

我可能會使用sed。一些實驗後,我得到了下面的

sed 's:/db/fs-type:/ :g' path.txt 

生產

的/ var/lib中/ SVN /回購/ b1me /產品/ payone /通用/代碼/核心/的/ var/lib中/ SVN /回購/ b1me /產品/ payone /通用/代碼/費用/的/ var/lib中/ SVN /回購/ b1me/

這似乎是你想要的輸出,假設path.txt包含了你想要的原始路徑改變。

如果你想在另一個文件中捕捉到這一點,該命令會是這樣的:

sed 's:/db/fs-type:/ :g' path.txt > new_path.txt 
0
so ross$ a=/x/y/db/fs-type 
so ross$ b=${a%/db/fs-type} 
so ross$ echo $b 
/x/y 

所以現在要做的整個文件...

so ross$ expand < dbf.sh 
#!/bin/sh 
t=/db/fs-type 
while read f1 f2 f3; do 
    echo ${f1%$t} ${f2%$t} ${f3%$t} 
done 
so ross$ cat dbf 
/a/b/db/fs-type /c/d/db/fs-type /e/f/db/fs-type 
/a/b/db/fs-type /c/d/db/fs-type /e/f/db/fs-type 
/a/b/db/fs-type /c/d/db/fs-type /e/f/db/fs-type 
/a/b/db/fs-type /c/d/db/fs-type /e/f/db/fs-type 
so ross$ sh dbf.sh < dbf 
/a/b /c/d /e/f 
/a/b /c/d /e/f 
/a/b /c/d /e/f 
/a/b /c/d /e/f 
so ross$ 
0

在一個perl :)

cat oldfile.txt | perl -nle "s/db\/fs-type//g; print;" > newfile.txt

相關問題