Hendrik
Hendrik

Reputation: 4929

Multiple sed operations on find -exec

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

Upvotes: 0

Views: 2449

Answers (4)

Richard Tingstad
Richard Tingstad

Reputation: 425

It's also possible to have multiple ecexs, like:

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

(though in this specific case a single sed is better, like accepted answer)

Upvotes: 0

David Cain
David Cain

Reputation: 17333

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

Upvotes: 3

qbert220
qbert220

Reputation: 11556

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.

Upvotes: 0

Hachi
Hachi

Reputation: 3289

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

Upvotes: -1

Related Questions