Find and replace with sed
To do replacements on the command line using us can use the command:cat file.txt | sed -r "s/find/replace/"
This will replace an occurrences of the word "find" with the word "replace" on each line.
The -r switch allows you to use POSIX extended regular expressions.
To use part of the found pattern in the replacement you can use "\1", "\2" and so on, eg:
sed -r "s/(.)/ i found this: \\1/"
The above example will match any single character and prefix the string " i found this: " in front of each line
If you use the "g" option in the expression eg:
sed -r "s/(.)/ i found this: \\1/g"
This replacement will be performed for every match found on each line.
For BSD or Mac OSX please omit the -r
switch, an example of this would be:
sed "s/hello/world"
Last updated: 11/07/2005