Regular Expression in command line with sed and awk

Let’s remember your Personal Love Song for a while. And Replace all “Love” word with “Math” word.

Then, put your Favorite sentence as your comment here.

Regular Expression with sed

It is easy to replace text with text editor. But this is not the case when you need to automate repetitive task with coding. It takes simpler tools than text editor.

Luckily there is Regular Expressions, that has been around for a few decades. Regular Expression is a language for string (text) processing. Search and replace utilize pattern match.

Dear Geeks,

Outside of programming language scope, there are two linux commands helper utilizing regular expression. It is the sed. And the more advance one, the awk.

 $ man sed
 $ man awk
 $ man gawk

Now you can do this with this simple command

 $ sed -E s/love\|Love/Math/ 'Love is.txt'
 $ perl -pe s/love\|Love/Math/ 'Love is.txt'
 $ awk '{gsub(/love|Love/,"Math"); print}' 'Love is.txt'

Now, that you know regular expressions. You can reduce your search-and-replace task in your script to just a very few lines.

Regular Expression It is very helpful. I’ve been regular expression in my script for at least one decade.


Have Fun

How Romantic is Love ?
How Numeric is Math ?
Why Roman Numeral ?