61

I have a file called file.txt. It has a number of double quotes throughout it. I want to remove all of them.

I have tried sed 's/"//g' file.txt

I have tried sed -s "s/^\(\(\"\(.*\)\"\)\|\('\(.*\)'\)\)\$/\\3\\5/g" file.txt

Neither have worked.

How can I just remove all of the double quotes in the file?

1
  • 1
    Your first attempt should work on a plain ASCII file. Maybe the file you're working on contains Unicode " instead?
    – holygeek
    Commented Oct 3, 2011 at 13:48

6 Answers 6

139

You just need to escape the quote in your first example:

$ sed 's/\"//g' file.txt
3
  • 3
    I ran this, on the screen it flew by the thousands of entries and the quotes were gone. Within the file.txt though, the quotes were still there.
    – MRTim2day
    Commented Oct 3, 2011 at 13:54
  • 13
    Yes, sed directs output to standard out by default and does not touch the input file. If you want the output saved to a file, do sed 's/\"//g' file.txt > file_new.txt and then when you are happy with file_new.txt, delete the old file and rename the new one.
    – Vicky
    Commented Oct 3, 2011 at 13:57
  • 13
    Or if your sed supports the -i option, that should let you replace the file with the edited file as well.
    – tripleee
    Commented Oct 3, 2011 at 14:20
37

Are you sure you need to use sed? How about:

tr -d "\""
1
  • 1
    What if all the kinds of quotes needed to be removed? What if it needed to work with unicode also? Commented Apr 7, 2017 at 18:24
16

For replacing in place you can also do:

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

or in Linux

sed -i 's/\"//g' file.txt
1
  • why does the first one (unix/mac os?) need the empty single quotes as first arg?
    – Noon Time
    Commented Aug 18, 2020 at 18:40
7

Try this:

sed -i -e 's/\"//g' file.txt
1
  • Hi Amita. Thanks for your answer. But please add an explanation. How does your solution work?. How to Answer. Thanks.
    – Elletlar
    Commented Jul 27, 2018 at 10:38
5

Additional comment. Yes this works:

    sed 's/\"//g' infile.txt  > outfile.txt

(however with batch gnu sed, will just print to screen)

In batch scripting (GNU SED), this was needed:

    sed 's/\x22//g' infile.txt  > outfile.txt
2

Try prepending the doublequote with a backslash in your expresssion:

sed 's/\"//g' [file name]
3
  • 1
    I ran this, on the screen it flew by the thousands of entries and the quotes were gone. Within the file.txt though, the quotes were still there.
    – MRTim2day
    Commented Oct 3, 2011 at 13:55
  • I run this with csv file and all data is gone. I tired couple times with the same result.
    – Katarzyna
    Commented Apr 21, 2017 at 22:17
  • use sed -i 's/\"//g' [file name] Commented Jul 10, 2018 at 5:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.