Multiple sed operations on find -exec

2019-09-06 19:12发布

问题:

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 "+"

回答1:

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


回答2:

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.



回答3:

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



标签: regex linux sed