Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I've got a set of 5000+ files that I need to do a simple search and replace in. I have been doing it in a text editor (EditPlus) by opening 500 files at a time, doing a global search/replace, saving all, closing, etc. But, that's taking literally hours to do and it's boring and tedious and I already have done it once today and need to do it again because all the files got refreshed. Is there a way to do this via the Bash command line? Here's the details:

Find onchange="document.location ='/products/view.html/view/'+this.value"

Replace it with onchange="alert('Not implemented')" style="display: none"

All of the files have a .HTM extension, but they are nested in several sub directories.

EDIT - I'm on a Mac and would prefer a *nix based solution.

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

The base command for you case looks like this:

sed -i 's|oldvalue|newvalue|g' file1 file2 file3

If all your file are in the directory ~/foo/bar/, what you want to do is:

cd '~/foo/bar/'
find . -type f | xargs sed -i "" "s|onchange=\"document.location ='/products/view.html/view/'+this.value\"|onchange=\"alert\('Not implemented'\)\" style=\"display: none\"|g"

EDIT: the glob (* as file name) can be problematic with very very much files and don't go in the child directories. In this case, change the last line.

EDIT 2: with BSD sed (the one used on MacOS X) -i needs an argument. BTW using xargs. Thanks to @Dennis Williamson

share|improve this answer
    
The questioner mentioned a Windows-based text editor, so my guess is that a windows solution is needed. Yours will probably work if cygwin or a similar environment is installed. –  Doug Harris Jan 10 '11 at 19:13
    
@Doug Harris He asked: "Is there a way to do this via the Bash command line?" seams to be a mixed environment. –  shellholic Jan 10 '11 at 19:26
    
Yeah, sorry for the confusion. I'm on a Mac, but I had previously been remoted in to a Windows machine and was using EditPlus. (EditPlus has a superior Find/Replace interface over TextMate) –  Chrisbloom7 Jan 10 '11 at 19:37
    
Question about the above solution: I'm getting an error saying "syntax error near unexpected token `('". I took a guess that it may have been the parentheses and tried escaping them, but that just resulted in what appeared to be a line waiting for input - just a " >" –  Chrisbloom7 Jan 10 '11 at 19:45
    
aha. My mistake. –  Doug Harris Jan 10 '11 at 20:06
show 7 more comments

Since EditPlus is a Windows editor i'm guessing you want a Windows shell solution.

You can get sed variants for Windows, but here's a PowerShell solution that recursively finds all *.HTM files, replaces "this" with "that", then saves the file.

gci -Include *.HTM -Recurse | % {
  (gc $_) -replace "this","that" | sc -path $_ }

Using your strings makes things a bit more tortured, since you have embedded double- and single-quotes, along with regular expression characters. The quotes are escaped with backticks (`) and the regex characters with backslashes (\).

gci -Include temp*.HTM -Recurse | % {
  (gc $_) -replace "onchange=`"document\.location `
  =`'/products/view\.html/view/`'\+this\.value",`
  "onchange=`"alert(`'Not implemented`')`" style=`"display: `
  none`"" | sc -path $_ }

Powershell is included with Windows 7, and can be downloaded for XP and above.

There's a vbscript single-file solution at StackOverflow i posted in response to "Is there any sed like utility for cmd.exe?" That question contains many answers with suggestions for Windows command-line sed solutions.

share|improve this answer
    
Sorry for the confusion. See my comment below - I'm on a Mac and do want to use a Bash or other *nix command line utility. –  Chrisbloom7 Jan 10 '11 at 19:38
    
@Chrisbloom7 - oh gotcha, i saw the EditPlus but missed the Bash. –  bill weaver Jan 10 '11 at 20:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.