Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8639373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:59:07+00:00 2026-06-12T10:59:07+00:00

I have a bash file processing some CSVs. Some of the input CSVs are

  • 0

I have a bash file processing some CSVs. Some of the input CSVs are not formatted properly, so I want to fix them with sed. The quotes are escaped like \" and not like "", so I call sed to change this. In the command line this works perfectly:

sed -i 's/\\"/""/gi' input.csv

But inside a bash script this seems to do nothing. I guess it has something to do with quotes and escape sequences, but what is the solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T10:59:08+00:00Added an answer on June 12, 2026 at 10:59 am

    you need to escape the escape character \ for that to work:

    $ echo 'bla;\"bli bli\";otherbla' | sed -e 's/\\\"/""/g'
    bla;""bli bli"";otherbla
    

    for bash scripts, you need to make sure that the line you read from the CSV file is properly quoted when passing it to sed. Can you provide an example of the CSV file as well as how you read from the file?

    Using cat file | while read, here is an example of the problem:

    $ cat test.csv
    bla;\"bli bli\";otherbla
    ble;""bli bli"";otherbla
    bli;\"blo\";otherbla
    
    $ cat test.sh
    #!/bin/bash
    
    cat test.csv | while read line;
    do echo "$line" | sed -e 's/\\\"/""/g'
    done
    
    $ ./test.sh
    bla;"bli bli";otherbla
    ble;""bli bli"";otherbla
    bli;"blo";otherbla
    

    One solution is to not use echo in the script but use sed directly on the file and storing the resulting csv in a new file:

    $ sed -e 's/\\\"/""/ig' test.csv > test-tmp.csv
    $ cat test-tmp.csv
    bla;""bli bli"";otherbla
    ble;""bli bli"";otherbla
    bli;""blo"";otherbla
    

    Then, as pointed into the comments, to avoid clobbering and wrong replacements of quoted fields finishing by \, we can use 2 sed expressions, and include the field separator to ensure we replace only the \" preceding or following the field separator (in my example, the field separator is ;) but this one doesn’t take into account fields single quoted with a \ as last character in the field such as the blo line:

    $ cat test.csv
    bla;\"bli bli\";otherbla
    ble;""bli bli"";otherbla
    bli;\"blo\";otherbla
    blo;"bli bli\";otherbla
    blu;""bli bli\"";otherbla
    
    $ sed -e 's/;\\\"/;""/ig' -e 's/\\\";/"";/ig' test.csv
    bla;""bli bli"";otherbla
    ble;""bli bli"";otherbla
    bli;""blo"";otherbla
    blo;"bli bli"";otherbla
    blu;""bli bli\"";otherbla
    

    If you have several sed command, you can put the in a script, it works the same way:

    $ cat s.sed 
    s/\\\"/""/g
    

    Using it:

    $ echo 'bla;\"bli bli\";otherbla' | sed -f s.sed 
    bla;""bli bli"";otherbla
    
    sed -f s.sed test.csv > test-tmp.csv
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bash file that does some file manipulation. I don't want to
I have an xml file that I want to configure using a bash script.
I have some status text displayed in a BASH script, e.g.: Removed file sandwich.txt.
I have a bash file to download a file but i do not know
I have a script that calls a bash script that does some processing, but
I have multiple bash file. I want to write a master bash file which
Possible Duplicate: Problem executing bash file I have created a bash file script and
I have a bash script that sources contents from another file. The contents of
I have a ruby bash script to download a zip file and output a
Textmate does not source my .profile file. I don't have a custom bash_init.rb, nor

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.