How to escape all double quotes inside a file in Bash shell linux ?

In this post, we will check a quick tip how to escape all double or single quotes in a file in Linux.

The post is very useful sometimes when we have to add some character before/after any specific character.

Lets see how to escape all double quotes in a file in bash shell linux

Escaping all double quotes(“) in a file

### Lets see the original file.
[root@nglinux ~]# cat file4
hello "good" ***
how  "good" ***
 are  "good" ***
 you "good" ***
[root@nglinux ~]# 

### Now lets escape all double quotes.
[root@nglinux ~]# cat file4 | sed 's/"/\\"/g'
hello \"good\" ***
how  \"good\" ***
 are  \"good\" ***
 you \"good\" ***
[root@nglinux ~]# 

In the above example, we can see how we have replaced double quotes ” with \”, and one extra slash \ is used to escape this in sed command.

These small tips are very useful when we need to replace some text in a file with another text.

Do post your comments/suggestions below.

Leave a Reply

avatar
  Subscribe  
Notify of