LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to replace a string in multiple file & sub directories (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-replace-a-string-in-multiple-file-and-sub-directories-829612/)

farsam 08-31-2010 05:09 PM

how to replace a string in multiple file & sub directories
 
Need to replace the following string :

#!../../../../TCL-Source/work/bin/expect

by

#!/usr/bin/expect

in multiple .exp files and in sub directories.


Thanks

Tinkster 08-31-2010 06:10 PM

Hi, welcome to LQ!

Something like
Code:

find . -type f -name \*exp | xargs -i sed -i.ori 's@#!../../../../TCL-Source/work/bin/expect@#!/usr/bin/expect@' {}
might do the trick. Untested, but you hopefully get the idea.

What we do is start searching in the current directory, look
for everything that's a) an ordinary file and b) ends in .exp.

We send all the found files through sed and have it in-place
replace the string "#!../../../../TCL-Source/work/bin/expect"
with "#!/usr/bin/expect", and creating a back-up file with
the extension .ori tacked on.



Cheers,
Tink

ghostdog74 08-31-2010 06:42 PM

Code:

find /path -type f -iname "*.exp" -exec sed  -i.bak 's|#!../../../../TCL-Source/work/bin/expect|#!/usr/bin/expect|' "{}" +;

Tinkster 08-31-2010 06:55 PM

Ghostdog, out of curiosity: what benefit do you see
in using the exec statement? In my personal experience
exec adds a bit of latency to execution.


Cheers,
Tink

grail 08-31-2010 10:27 PM

I was thinking maybe just force the change of the first line:
Code:

find /path -type f -iname '*.exp' -exec sed -i.bak '1s@.*@#!/usr/bin/expect@' {} \;

ghostdog74 08-31-2010 10:39 PM

Quote:

Originally Posted by Tinkster (Post 4084134)
Ghostdog, out of curiosity: what benefit do you see
in using the exec statement? In my personal experience
exec adds a bit of latency to execution.


Cheers,
Tink

note that i am not using \; but +; which is GNU find's extension that is supposed to have the effect of "xargs". (though i have not really tested myself) :)


All times are GMT -5. The time now is 12:23 PM.