How to use the name of the file with sed in a find expression

前端 未结 2 747
夕颜
夕颜 2020-12-21 17:30

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 t

2条回答
  •  自闭症患者
    2020-12-21 17:50

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

    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" {} \;
    

    EDIT: For removing the leading ./ from the paths, you can try:

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

提交回复
热议问题