Different ways to filter text and replace words in a stream with bash.

Posted: March 31, 2017. At: 10:18 AM. This was 5 months ago. Post ID: 10425

The bash shell offers many different ways to filter text and modify the output of a command. This is useful when a certain outcome is desired and changing text is desired.

This example uses Python to search and replace the word ‘linux’ with ‘GNU/Linux’.

ubuntu ~ $ echo "linux is fun." | python -c 'import sys;print sys.stdin.read().replace("linux","GNU/Linux")'

Another example, this time using Perl.

ubuntu ~ $ echo "linux is fun." | perl -pe 's.linux.GNU/Linux.g'
GNU/Linux is fun.

This final example shows how to use the sed command to filter text.

ubuntu ~ $ echo "linux is fun." | sed "s/linux/GNU\/Linux/gi;"
GNU/Linux is fun.

No comments have been made. Use this form to start the conversation :)

Leave a Reply