views:

84

answers:

5

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

A: 

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 ""...

Fredy87
+4  A: 
sed 's#db/fs-type$##' myfile > myalteredfile
sleepynate
+1, beat me to it by a few seconds
ammoQ
please you can give me example
Osama Ahmad
your sed does not remove all occurences of the string in every line of the file, add the 'g' switch...
eumiro
@eumiro It is obvious from the examples he wants to remove them from the end of the line.
sleepynate
@Mohammed this is an example. If the information were in `myfile`, you'd use the `sed` command as above in the same directory, and it would generate `myalteredfile`, a copy with your changes.
sleepynate
and we can use cat FILENAME | sed -e 's/db\/fs-type//g'
Osama Ahmad
@sleepynate: It seems obvious from the example that the OP wants to remove "db/fs-type" from *everywhere* in the line (there are three in the original and none in the result). Furthermore, your answer removes the text, but the example shows spaces replacing the db/fs-type.
GreenMatt
@GreenMatt: I think you're looking at the previous malformed version of the question. There are three lines in the example, each only containing one instance of the text to be removed, occurring at the end of the line.
sje397
@GreenMatt yea, I think you may be looking at a messed up version of the example. I don't see what you're describing in the current example.
sleepynate
@Mohammad AL-Rawabdeh, @sje397, @sleepynate: I've looked at the original and both edits and copied it would be nice if Mohammad could clarify what is wanted, instead of making us guess.
GreenMatt
@GreenMatt well mate, we're all here to do our best and help each other out. *group high-five*
sleepynate
A: 

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
GreenMatt
A: 
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$ 
DigitalRoss
A: 

One in perl :)

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

Powertieke