Search and Edit Files with Certain Text

Here's a quick way to edit all files whose contents contain a given string:

files=""; for i in `find . -type f -print`; do files="$files `grep -l <text> $i`"; done; vi $files
  1. For all file names in the current directory, including those in subdirectories, assign each in turn to the variable i.
    for i in `find . -type f -print`;
  2. For each of the files we found, run the grep command to search the file provided by the $i variable.
    do files="$files `grep -l <string> $i`";
  3. Edit the found files with the vi editor.
    vi $files

    Move back and forth between files using:

    :n    -- move to the next file
    :rew  -- move back 
    :e #3 -- edit the third file in buffer
    :ar   -- list files status
    

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Search and replace text in html files

The escape characters make the regex hard to read/write (For me that is)
ie to add index.html at the end of each href - replace /" with /index.html"

find . -name "*.html" -print0 | xargs --null perl -pi -e 's/\/\"/\/index.html\"/'

This script contains perl - possibly the head honcho of text manipulation scripting languages - though perl could be replaced with grep, in this instance.

Comment