Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

unix search/replace across files

Status
Not open for further replies.

jdulberg

Technical User
Jun 19, 2001
22
CA
I have a heap of files in a heap of directories that I need to remove a single line of text from. So far the only thing that I can find is the following but its not quite what I'm looking for.

Code:
perl -pi.bak -e s/oldstring/newstring/g filename(s)

I need to traverse the entire directory structure on my system looking at /home/vhosts/xyz.com/index.shtml for a string <!--#exec cgi=&quot;/cgi-bin/count.cgi&quot; --> and delete that entire line. All the files will be called index.shtml but xyz.com will be different in every instance.

Is such a command possible or do I have to do it manually?

Any suggestions are greatly appreciated! :)

Thanks
Jason
 
Thanks for your reply... I suppose next time I should look in the faq as well as doing a forum search. I tried the code from just below my test domain however it gave me a rather long error.

Code:
[vhosts]$ find domain.com -name &quot;index.shtml&quot; | xargs perl -pi.bak -e 's/<!--#exec cgi=&quot;/cgi-bin/count.cgi&quot; -->//g'

Bareword found where operator expected at -e line 1, near &quot;s/<!--#exec cgi=&quot;/cgi-bin/ax&quot;
syntax error at -e line 1, near &quot;s/<!--#exec cgi=&quot;/cgi-bin/ax&quot;
String found where operator expected at -e line 1, at end of line
        (Missing semicolon on previous line?)
Can't find string terminator '&quot;' anywhere before EOF at -e line 1.
xargs: perl: exited with status 255; aborting

Any ideas what went wrong??

Jason
 
Your search string contains '/' characters which are confusing your 's/search/replace/g' call. In perl, you can use other characters instead of '/' in the call, for example:

Code:
[vhosts]$ find domain.com -name &quot;index.shtml&quot; | xargs perl -pi.bak -e 's@<!--#exec cgi=&quot;/cgi-bin/count.cgi&quot; -->@@g'

In fact for the example you have, where you wish to completely skip a line, try the following instead:
Code:
find . -name &quot;*.shtml&quot; | xargs perl -ni.bak -e 'print unless m[<!--#exec cgi=&quot;/cgi-bin/count.cgi&quot; -->]'

Note the -p argument has been changed to -n, which still loops through all the lines in your file, but does not print unless instructed in the -e argument.

Hope this helps :)
 
Toolkit, your second bit of code worked out perfectly... perhaps too perfectly. I think it would have been best to leave out the -ni.bak as there are now tons of index.shtml.bak files throughout the directory tree. How would I go about removing these files automatically?

Thanks again

Jason
 
find /directory -name index.shtml.bak | xargs rm

-jim
 
Thanks for your response J1mbo... Your command worked great!

I'm beginning to see how the format for these kinds of commands work.

Jason
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top