简体   繁体   English

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

[英]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 / 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 -类型/ 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/ / 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 /产品/ payone /通用/代码/ 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: 您可以使用某些编辑器(例如nano,vi,vim等)重写文件,也可以按照此网站的说明进行操作:

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

Or replacing a string with a simple grep, like this: 或使用简单的grep替换字符串,如下所示:

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

When I say rewrite, I mean replace ":-" for ""... 当我说重写时,我的意思是将“:-”替换为“ ...”。

I would probably use sed . 我可能会使用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/ / 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. 假设path.txt包含您要更改的原始路径,则这似乎是您想要的输出。

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

一分钱一分:)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM