[英]how i can delete something from 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 -类型/ 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 /产品/ payone /通用/代码/ merchantserver /
sed 's#db/fs-type$##' myfile > myalteredfile
您可以使用某些编辑器(例如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 。 经过一些实验,我得到了以下内容
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$ a=/x/y/db/fs-type
so ross$ b=${a%/db/fs-type}
so ross$ echo $b
/x/y
所以现在要做整个文件...
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.