How to replace some text using sed awk, and bash ?

Today we will see how to replace some text from a string using sed, awk, tr and in bash shell.

Suppose you want to remove “~” from a string, it means we can either delete it or replace “~” with a space/null.

Example String:
Welcome ~ To ~ NGELINUX.COM

Output Required:
Welcome To NGELINUX.COM

Ways to achieve this by replacing “~” with “”

1. Using AWK

[root@nglinux ~]#echo "Welcome ~ To ~ NGELINUX.COM" | awk 'gsub("~","")'
Welcome  To  NGELINUX.COM

2. Using SED

[root@nglinux ~]#echo "Welcome ~ To ~ NGELINUX.COM" | sed 's/ ~//g'
Welcome To NGELINUX.COM

3. Using BASH shell inbuilt functionality

[root@nglinux ~]#var1="Welcome ~ To ~ NGELINUX.COM"; echo ${var1//~ /}
Welcome To NGELINUX.COM
[root@nglinux ~]#

4. Using TR to replace/delete text

[root@nglinux ~]#echo "Welcome ~ To ~ NGELINUX.COM" | tr -d "~"
Welcome  To  NGELINUX.COM

Do post your comments or further suggestions in comments below.

Leave a Reply

avatar
  Subscribe  
Notify of