WRITELOOP

TESTING SED SUBSTITUTION WITH REGULAR EXPRESSIONS

2020 June 25

I had a situation where I had to make sure a sed expression would apply correctly on a file, but I did not want to change the file, and to also test with various inputs.

To achieve that, I can use echo to simulate the various inputs, and pipe it to sed then. E.g.:

echo 'tag: latest' | sed -e "s/tag\: *.*/tag\: $(cat VERSION | tr -s '\n' ' ')/g"
echo 'tag: ' | sed -e "s/tag\: *.*/tag\: $(cat VERSION | tr -s '\n' ' ')/g"
echo 'tag:' | sed -e "s/tag\: *.*/tag\: $(cat VERSION | tr -s '\n' ' ')/g"
echo 'tag: xxx ' | sed -e "s/tag\: *.*/tag\: $(cat VERSION | tr -s '\n' ' ')/g"
echo 'tag: 0.11 ' | sed -e "s/tag\: *.*/tag\: $(cat VERSION | tr -s '\n' ' ')/g"
NOTE: The original content(s) that inspired this one can be found at:
https://stackoverflow.com/questions/4052253/how-do-you-debug-a-regular-expression-with-sed
All copyright and intellectual property of each one belongs to its' original author.