LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-31-2010, 05:09 PM   #1
farsam
LQ Newbie
 
Registered: Aug 2010
Posts: 1

Rep: Reputation: 0
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
 
Old 08-31-2010, 06:10 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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
 
Old 08-31-2010, 06:42 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
find /path -type f -iname "*.exp" -exec sed  -i.bak 's|#!../../../../TCL-Source/work/bin/expect|#!/usr/bin/expect|' "{}" +;
 
Old 08-31-2010, 06:55 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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
 
Old 08-31-2010, 10:27 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
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@' {} \;
 
Old 08-31-2010, 10:39 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Tinkster View Post
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)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find & Replace string in multiple files Rudy Linux - General 14 04-15-2010 08:10 AM
search and replace string having multiple special characters say_hi_ravi Linux - Newbie 4 08-26-2009 07:43 AM
What is the best way to find & replace in a directory and all sub-directories mostanser Linux - Newbie 4 04-06-2009 10:31 PM
how to replace string by other one in a file & without perl ? Xeratul Programming 4 05-25-2007 11:13 PM
trying to search and replace text file for single & multiple line breaks separately brokenfeet Programming 7 08-29-2003 01:56 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration