Linux 如何从文件中删除某些内容?

Linux 如何从文件中删除某些内容?,linux,command-line,file,Linux,Command Line,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 我想

我想从文件的每一行删除一些内容,例如:-

我在文件中有以下路径:

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


您可以使用一些编辑器(如nano、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

当我说重写时,我的意思是替换“:-”对于“”…

您可以使用一些编辑器(如nano、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
当我说重写时,我的意思是替换“:-”对于“”…

我可能会使用。经过一些实验,我得到了以下结论

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

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

这似乎是您想要的输出,假设path.txt包含您希望更改的原始路径

如果要在另一个文件中捕获此信息,该命令类似于:

sed 's:/db/fs-type:/ :g' path.txt > new_path.txt
我可能会用。经过一些实验,我得到了以下结论

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

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

这似乎是您想要的输出,假设path.txt包含您希望更改的原始路径

如果要在另一个文件中捕获此信息,该命令类似于:

sed 's:/db/fs-type:/ :g' path.txt > new_path.txt
所以现在要做整个文件

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$ 
so$expand
所以现在要做整个文件

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$ 
so$expand
perl中的一个:)

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

perl中的一个:)


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

那么,您想删除
db/fs type
部分,对吗?是的,我想删除db/fs type部分,所以,您想删除
db/fs type
部分,正确吗?是的,我想删除db/fs类型的部分请您给我一个例子您的sed没有删除文件每一行中所有出现的字符串,添加“g”开关…@eumiro从示例中可以明显看出,他想从行的末尾删除它们。@Mohammed这是一个例子。如果信息在
myfile
中,您将在同一目录中使用上面的
sed
命令,它将生成
myalteredfile
,一份包含您所做更改的副本。我们可以使用cat FILENAME | sed-e's/db\/fs type//g'请您给我一个例子您的sed没有删除文件每一行中所有出现的字符串,添加“g”开关…@eumiro从例子中可以明显看出,他想从行末删除它们。@Mohammed这是一个例子。如果信息在
myfile
中,您将在同一目录中使用上面的
sed
命令,它将生成
myalteredfile
,一份包含您更改的副本。我们可以使用cat FILENAME | sed-e's/db\/fs type//g'