Urza9814 Posted May 6, 2007 Report Share Posted May 6, 2007 (edited) Ok, so I hope you can get what I wanna do from this and tell me how to actually do it: [urza9814@localhost CDREADY]$ sed s/ssk/..\/ssk/gw < */*.html bash: */*.html: ambiguous redirect Basically, I have a folder of folders with HTML files, and I need to change wherever it says 'ssk' to '../ssk' for all of the html files in all of the folders. How do I do that? I mean I understand why what I typed doesn't work...but yea. What will? Edited May 6, 2007 by Urza9814 Quote Link to comment Share on other sites More sharing options...
jboy Posted May 7, 2007 Report Share Posted May 7, 2007 Check into the krename utility. http://www.krename.net/ It should be available in the Mandriva repositories. Quote Link to comment Share on other sites More sharing options...
Urza9814 Posted May 7, 2007 Author Report Share Posted May 7, 2007 that's to rename filenames though. I need to search and replace strings inside of the files. Quote Link to comment Share on other sites More sharing options...
neddie Posted May 7, 2007 Report Share Posted May 7, 2007 Have you read the other sed threads here, like for example this one or this one? Just use -i to specify in-place editing of the file. If you want to recurse subdirectories you'll probably need to do a find first, and pipe the result to sed. Quote Link to comment Share on other sites More sharing options...
Urza9814 Posted May 7, 2007 Author Report Share Posted May 7, 2007 Ok, I suppose find should work. I was also confusing /w and -i after reading a page on google as a refresher. Quote Link to comment Share on other sites More sharing options...
iphitus Posted May 7, 2007 Report Share Posted May 7, 2007 assuming the files are in the current directory... for file in *html do sed -i "s@ssk@../ssk@g" $file done or shorter way with find, (but i havnt tested). this one will recurse to deeper directories find . -iname \*.html -exec sed -i "s@ssk@../ssk@g" {}; this one is the same as the last, except it won't recurse deeper, only the current directory: find . -maxdepth 1 -iname \*.html -exec sed -i "s@ssk@../ssk@g" {}; Just as a tip, instead of hideously escaping forward slashes in sed statements, use a different delimiter, I use @ because it stands out, but iirc the rule is, if it isnt in the string, you can use it. Though using alphanumeric things isnt encouraged as it'd be pretty confusing. James Quote Link to comment Share on other sites More sharing options...
Guest blin Posted May 7, 2007 Report Share Posted May 7, 2007 (edited) Good relevant script examples, here are a few minor additions and adjustments. adjusted for command line use, -print addition to list files modified for file in *.html; do sed -i "s@ssk@../ssk@g" $file; done -print adjustments for escape sequences on ";", execdir vs. exec, missing -iname and html ext typos, -print addition With changes... find . -iname \*.html -execdir sed -i "s@ssk@../ssk@g" {} ';' -print this one is the same as the last, except it won't recurse deeper, only the current directory: find . -maxdepth 1 -iname \*.html -execdir sed -i "s@ssk@../ssk@g" {} ';' -print If the execdir command you're using permits, you should use + instead of ';' . It executes the command against a list of names rather than once per filename. Sed and most fileutils will work with lists. No need to escape +. find . -iname \*.html -execdir sed -i "s@ssk@../ssk@g" {} + -print find . -maxdepth 1 -iname \*.html -execdir sed -i "s@ssk@../ssk@g" {} + -print All tested and appear to work on Mandriva 2007.1 x86_64 standard install. You'll want to rewrite the reg expression in sed to be more useful. Possible fixes... add exclude pattern so you don't clobber ../ssk into ../../ssk, this will allow you to run the script twice if needed. adjust regex to include the larger pattern, ssk is probably not the fully defined pattern you're trying to find, "newline" ssk, "whitespace" ssk, html : / /ssk or another more refined regex pattern is needed that won't match sssk, tssk, ../ssk or some other variant of ssk you don't want to change. add line numbers and counts of modified files to output see http://www.regular-expressions.info/examples.html for more ideas Edited May 7, 2007 by blin Quote Link to comment Share on other sites More sharing options...
iphitus Posted May 7, 2007 Report Share Posted May 7, 2007 nice blin! thanks for that! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.