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

Posted: March 31, 2017. At: 10:18 AM. This was 10 months ago. Post ID: 10425
Page permalink.
WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters. These cookies expire two weeks after they are set.

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