如何在发现前pression使用的文件名与SED [英] How to use the name of the file with sed in a find expression

查看:130
本文介绍了如何在发现前pression使用的文件名与SED的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图回答使用bash / Perl的基于每个文件的名称我在一个点结束修改文件中,我不知道如何使用找到 SED 在一起。

Trying to answer Using Bash/Perl to modify files based on each file's name I ended in a point in which I don't know how to use find and sed all together.

让我们说有就是我们要改变一个行的文件有一定的结构,追加的文件的名称。

Let's say there is a certain structure of files in which we want to change a line, appending the name of the file.

如果这是一个正常的循环中,我们会做:

If it was a normal for loop we would do:

for file in dir/*
do
   sed -i "s/text/text plus $file/g" $file
done

但是,假设我们要使用找到从所有子目录更改的文件。在这种情况下,我会用...

But let's say we want to use find to change files from all subdirectories. In this case, I would use...

find . -type f -exec sed -i "s/text/text plus {}/g" {} \;
                                              ^
                                   it does not like this part

但这些 {} SED 是不能接受的,我得到的错误

but these {} within sed are not accepted and I get the error

的sed:-e前pression#1,烧焦20:未知的选项,以单曲

sed: -e expression #1, char 20: unknown option to `s'

我发现了一些类似的问题( 1 ),但在这种情况下,不能概括它足以令它理解为我

I found some similar questions (1) but could not generalize it enough to make it understandable for me in this case.

我相信你们会为这个伟大的解决方案。谢谢!

I am sure you guys will come with a great solution for this. Thanks!

推荐答案

找到将返回的的路径名的(相对或绝对)取决于路径您指定。

find would return pathnames (relative or absolute) depending upon the path you specify.

这将与您指定的分隔符冲突,即 / 。更改 SED 分隔符,你应该是不错的:

This would conflict with the delimiter you've specified, i.e. /. Change the delimiter for sed and you should be good:

find . -type f -exec sed -i "s|text|text plus {}|g" {} \;

编辑:为除去最主要的 ./ 从路径,你可以试试:

find . -type f -exec sh -c '$f={}; f=${f/.\//}; sed -i "s|text|text plus ${f}|g" {}' \;

我敢肯定,更好的解决方案可能存在的...

I'm certain that better solutions might exist ...

这篇关于如何在发现前pression使用的文件名与SED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆