In GNU sedI can display the result of the successful substitution of the search pattern. A simple example:
echo -e "nginx.service\nmariadb.service\nphp-fpm.service" > something.conf;
sed -ri 's|(mariadb)(\.service)|postgresql-9.4\2|w sed-output.log' something.conf;
[[ -s sed-output.log ]] && echo "Pattern found and modified. $(cat sed-output.log)" || echo "Pattern not found.";
Since it sedhas a limitation when working with multi-lines, I switched to perl.
echo -e "nginx.service\nmariadb.service\nphp-fpm.service" > something.conf;
perl -i -pe 's|(mariadb)(\.service)|postgresql-9.4\2|' something.conf;
The above code did the same thing as sed, but how can I get modified content(" postgresql-9.4.service") to a file or print?
Basically, what I would like to achieve, after running the script, it tells me if it successfully (and what actually replaces), and if not, I will show a message saying that it could not be found and replaced.
Edit: It is
emphasized that I want to get (only-the-) modified content, which indicates the success of my script. Because with perl -i -pe 's/pattern/replace/' fileI could not know whether it will return true or false. Of course, I can just do grep -E "/pettern/"to find out, but that is not a question.
source
share