开发者

how i can delete something from file?

开发者 https://www.devze.com 2023-01-17 12:16 出处:网络
i want to delete something from each line of file for example :- i have the following path in file : /var/lib/svn/repos/b1me/products/payone/generic/code/core/db/fs-type /var/lib/svn/repos/b1me/pr

i want to delete something from each line of file for example :-

i have the following path in file :

/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/merchants开发者_开发百科erver/db/fs-type

i want to do something to become

/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 's#db/fs-type$##' myfile > myalteredfile


You can rewrite the file with some editor(like nano, vi, vim, etc.) or you can follow the instructions of this website:

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

Or replacing a string with a simple grep, like this:

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

When I say rewrite, I mean replace ":-" for ""...


so ross$ a=/x/y/db/fs-type
so ross$ b=${a%/db/fs-type}
so ross$ echo $b
/x/y

So now to do the whole file...

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$ 


I would probably use sed. After some experimentation I got the following

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

to produce

/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/

which seems to be the output you want, assuming path.txt contains the original path you wish to make changes to.

If you want to capture this in another file, the command would be something like:

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


One in perl :)

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

0

精彩评论

暂无评论...
验证码 换一张
取 消