Osama Ahmad
Osama Ahmad

Reputation: 815

how i can delete something from file?

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/

Upvotes: 0

Views: 245

Answers (5)

Powertieke
Powertieke

Reputation: 2408

One in perl :)

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

Upvotes: 0

GreenMatt
GreenMatt

Reputation: 18580

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

Upvotes: 0

DigitalRoss
DigitalRoss

Reputation: 146043

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$ 

Upvotes: 0

sleepynate
sleepynate

Reputation: 8036

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

Upvotes: 4

ferran87
ferran87

Reputation: 645

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

Upvotes: 0

Related Questions