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

前端 未结 2 739
夕颜
夕颜 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:43

    I really think the issue is that your files name contains a / that is why sed believes it start the options strings.

    Replace / by @ in you sed command would do the job.

    I try that on Linux BASH and it work perfectly

    find . -type f -exec sed -i -e "s@text@test plus {}@g" {} \;
    
    0 讨论(0)
  • 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 ...

    0 讨论(0)
提交回复
热议问题