0

$cat process-file.csv

"key1","key1-ConnString","Server=database.windows.net,1433;Database=sqldb;Persist Security Info=False;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"

process-file.sh

#!/bin/bash
strip_quotes() {
   echo `sed -e 's/^"//' -e 's/"$//' <<<"$1"`
}

while IFS=, read -r key1 keyC secretValue; do
 echo "$(strip_quotes $secretValue)"
done  < process-file.csv
$ ./process-file.sh

Server=database.windows.net,1433;Database=sqldb;Persist

How to preserve spaces in a string that needs the quote removed?

JhnWorks
  • 3
  • 3

1 Answers1

1
  • Take out the backticks. (They shouldn't be used at all in code written for post-1992 shells; $(...) is the modern, POSIX-standardized way to do command substitution).

  • Quote all your expansions. All of them. If you do need $(something), then it needs to be "($something)" with the quotes. That's true of backticks too.

  • Don't run echo $(somecommand) -- just run somecommand. It's faster, and it avoids the bugs that unquoted expansion (as referenced above) creates.

Fixing the above, but keeping your code's general structure, might look like:

#!/bin/bash
strip_quotes() {
   sed -e 's/^"//' -e 's/"$//' <<<"$1"
}

while IFS=, read -r key1 keyC secretValue; do
 strip_quotes "$secretValue"
done  < process-file.csv

...but this is silly (and extremely slow), when you could run just one copy of sed to process the whole input file, instead of starting a new copy of sed for every individual line as your code currently does.

sed -re 's/^[^,]*,[^,]*,"(.*)"/\1/' <process-file.csv
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441