Monday, December 06, 2010

SED – Search and replace string:


If you want to change all occurrences of developer to administrator in the oracle.txt file in the grep example, enter this:

Content of oracle.txt

Oracle database developer
SQL database developer
MYSQL database developer

sed 's/developer/administrator/g' oracle.txt

See the changes below after running SED.

oracle database administrator
SQL database administrator
MYSQL database administrator

In the quoted string, the "s" means substitute, and the "g" means make a global change. You can also leave off the "g" (to change only the first occurrence on each line) or specify a number instead (to change the first n occurrences on each line).
Searching and replacing several strings on a file:
example:

sed 's/-//g' test.txt |sed 's/"//g' | sed 's/;//g' | sed 's/\[//g'
Take care of any escape character using additional ‘\’ while searching for ‘[‘

No comments: