How to Use sed to Remove all Double Quotes from File


Often you may want to use sed to remove all double quotes from a file.

You can use the following basic syntax to do so:

sed -i 's/\"//g' team_info.txt

This particular example removes all double quotes from the file named team_info.txt.

Here is what the various sed commands do:

  • -i specifies that the file should be modified in-place
  • s specifies that we would like to substitute one pattern with another pattern
  • \” is used to specify that we would like to substitue the double quotes (we use a backslash as an escape character)
  • // is used to specify that we would like to substitute the double quotes with nothing
  • g specifies that we would like to make the substitution globally, i.e. all occurrences should be replaced

If you’d like to store the results to a new file then you can use the following syntax:

sed 's/\"//g' team_info.txt > team_info_new.txt

The following example shows how to use this syntax in practice.

Note: The d operator is used to delete lines. By using !d we specify that we’d like to delete all lines that do not match a particular pattern.

Example: Use sed to Remove All Double Quotes from File

Suppose we have a text file named team_info.txt that contains information about various basketball players:

Suppose that we would like to remove all double quotes from the file.

We can use the following syntax to do so:

sed -i 's/\"//g' team_info.txt

The following screenshot shows how to use this syntax in practice:

sed remove double quotes from file

Notice that when we use the cat command again to view the contents of the file that all double quotes have been removed from the file.

Note that we could also use the following syntax to remove all double quotes and then save the results to a new file:

sed 's/\"//g' team_info.txt > team_info_new.txt

The following screenshot shows how to use this syntax in practice:

sed remove double quotes and save to new file

Notice that when we use the cat command to view the contents of the file named team_info_new.txt that it contains all of the lines from the original file with all of the double quotes removed.

Related Tutorials

The following tutorials explain how to perform other common tasks in sed:

How to Use sed to Delete Specific Line Numbers from File
How to Use sed to Replace All Occurrences of Pattern
How to Use sed to Replace All Text After Match
How to Use sed to Extract All Lines Containing a Pattern

Leave a Reply