My first post is an example on how to call an external function in sed. Here is my example:
Code: Select all
echo a | sed -ne 's/\(.*\)/echo a\1/' -e 'e' -e 'p'
Code: Select all
echo a | sed -ne '
s/\(.*\)/echo a\1/ #Replace "a" with echo aa
e #Execute the output of the last command
p #Print the result
* The 's' denotes string substitution.
* The brackests "\(...\)" capture the text which matches the regular expression inside the brackets. In our case the regular expression is .* which means match any string (in our case 'a'). The value of the match can be retrieved with the back reference "\1". The backslash in front of each bracket isn't necessary if you use extended regular expressions. However, with extended regular expressions more escaping of special characters may be required.
Next we Execute the external command which is the output of our last expression. In our case we are executing the external command echo aa. The "e" character means execute the external command.
Finally we print the result. The 'p' command is used to print the result.
The output is "aa"