2

I want to make it easier to run this kind of command:

find . -type f -exec sed -i 's|wpp-splash|wpp_splash|g' {} \;

so I created a function in my .bashrc to shorten it:

function sedall() { find . -type f -exec sed -i 's|$1|g' {} \; }

this way I can do

sedall wpp-splash|wpp_splash

But there is a syntax error. I am not sure what it is, but that bash function is resulting in "unexpected end of file". I wonder if it is something with the } characters? I tried escaping them like \{\} but that did not solve the problem.

Any help, please?

2 Answers 2

6

Many problems there.

  • Variables are not expanded inside single-quotes.
  • { command ; } requires the terminating semicolon (or a newline).
  • sedall wpp-splash|wpp_splash That is understood as a pipeline because you did not protect the pipe character with quotes.

I would suggest this:

sedall(){
    [ "$#" = 2 ] || { echo Two arguments needed; return 9; }
    find . -type f -exec sed -i "s|$1|$2|g" {} \;
}

It needs two arguments instead of one, and it checks if those two arguments were given before execution.

$ cat a b
XABCX
YABCY
$ sedall ABC DEF
$ cat a b
XDEFX
YDEFY
2
  • Also a micro improvement can be to feed multiple found files to sed via the + keyword.As -i will treat each file individually. Jun 29, 2020 at 23:19
  • Also care has to be taken that $1 abd $2 are properly escaped to be fit to be used on the LHS RHS of the s/// command respectively. Jun 29, 2020 at 23:24
2
function sedall() { find . -type f -exec sed -i "s|$1|g" {} \; ; }
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .