Multiple sed operations on find -exec

2019-09-06 18:21发布

I am trying to execute multiple sed operations on the find -exec operation. My code looks like this:

find . -name '*.html.haml' -exec sed -i '' 's/restaurant_id/company_id/g' && sed -i '' 's/restaurants/companies/g' && sed -i '' 's/restaurant/company/g' && sed -i '' 's/Restaurants/Companies/g' && sed -i '' 's/Restaurant/Company/g' "{}" \;

This seems not to work. How could I do that?

Error:

find: -exec: no terminating ";" or "+"

标签: regex linux sed
3条回答
Viruses.
2楼-- · 2019-09-06 19:07

your shell presumably splits this command to find . -name '*.html.haml' -exec sed -i '' 's/restaurant_id/company_id/g' and && and sed -i ...

try quoting the exec command find . -name '*.html.haml' -exec "sed -i '' 's/restaurant_id/company_id/g' && sed -i ..."

another approach is an extern sed-script you can call with a single command from exec

查看更多
We Are One
3楼-- · 2019-09-06 19:19

As others have said, the problem is that the && is interpereted to execute the sed command after the find command is finished, instead of passing one string of commands to be executed on each file.

The easiest way to achieve your desired result is to combine this all into one sed command with semicolons. Like so:

$ find . -name '*.html.haml' -exec sed -i 's/restaurant_id/company_id/g;s/restaurants/companies/g;s/restaurant/company/g;s/Restaurants/Companies/g;s/Restaurant/Company/g' "{}" \;
查看更多
SAY GOODBYE
4楼-- · 2019-09-06 19:19

The &&s will be interpreted by the shell. Quote them all to prevent this '&&'

You'll need to add "{}" to each sed command.

Alternatively you could use the -e option of sed to only execute sed once with multiple scripts.

查看更多
登录 后发表回答