(Up to OJB's Mac Tips List Page)
Unix Search and ReplaceTo Search and Replace from the Command Line
Use "sed", the stream editor. You can use | (pipe) to send standard streams (STDIN and STDOUT specifically) through sed and alter them on the fly. To edit files directly, use the -i (in place) parameter.
sed -i "" -e 's/SEARCH/REPLACE/g' FILE
Where:
SEARCH is what to search for.
REPLACE is what to replace it with.
FILE is the path to the file to be processed.
s indicates the search terms.
g sets search to global (all instances on each line, not just the first).
-i specifies search the file "in place", otherwise uses standard input and output.
-e precedes each search specification; multiple can be used (see below).
Note 1: "" indicates don't add anything to the filename of the output file (just replace the existing file). This seems to be different in different shells, so YMMV.
Note 2: Any character (except "\" and "\n") can be used as the separator ("/" in the example above). Alternatively, use the escape character ("\") to escape "/" in the search or replace strings.
Note 3: the search string can be a regular expression.
Multiple Search and Replace
Use multiple search and replace string, each preceded by "-e":
sed -i "" -e 's/SEARCH1/REPLACE1/g' -e 's/SEARCH2/REPLACE2/g' FILE
Case Insensitive Search
Use the "I" option at the end of the strings:
sed -i "" -e 's/SEARCH/REPLACE/gI' FILE
|