我正在写脚本
脚本的一个目标是在匹配后,在多个.yml
文件中添加新的 codeline 和 foo 递归地。
# Append new line after match in multiple files with sed
## sed -i -se "s/\foo/bar/g" *.yml
grep -rl foo * .| xargs sed -i -e "s/\foo/a bar/g" *.yml
在与 foo 的每次比赛后我期望,由于/a
, bar 将被添加到新行中.yml
个文件。
我意外出现Sed支出:
sed: can't read *.yml: No such file or directory
详细信息:
答案 0 :(得分:2)
您要为文件名参数提供xargs
。您也不应使用*.yml
作为参数。 Shell会将其扩展到当前目录中的.yml
文件,如果没有,则会出现错误。
如果您希望grep -r
仅检查.yml
个文件,请使用--include
选项。
grep -rl --include='*.yml' foo . | xargs sed -i -e "s/foo/a bar/g"
请注意,s
命令不用于在一行之后添加新行。用于替换同一行。 s/foo/ a bar/g
用foo
替换行中的所有a bar
。如果要追加,请摆脱s
命令。另外,您不要将/g
放在a
命令的末尾。
grep -rl --include='*.yml' foo . | xargs sed -i -e "/foo/a bar"