Posted: . At: 10:18 AM. This was 7 years 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.


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


The bash shell offers many different ways to filter text and modify the output of a command. This is useful when a the user is creating a complex script and manipulating a text stream is required.

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.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.