0

I want to replace some values with bash in a csv file like this:

age,sex,bmi,smoker,region,charges
19,female,27.9,yes,southwest,16884.924
23,male,29.83,no,northeast,1725.5523

Basically, I need to replace the binary fields with 0 or 1 values. My code is the following:

filename="$1"
IFS=,
while read -ra values; do
 
  case "${values[1]}" in
  male)    values[1]=0 ;;
  female)  values[1]=1 ;;
  esac
  
  case "${values[4]}" in
  yes) values[4]=1 ;;
  no)  values[4]=0 ;;
  esac
    
done
} < $filename

I am executing the script with the calling:

./b.sh insurance.csv

I don't know what I have wrong but the csv does not update with the new values.

CPG
  • 35
  • 3
  • Please add sample input **with correct field separator** (no descriptions, no images, no links) and your desired output **with correct field separator** for that sample input to your question (no comment). – Cyrus Jun 05 '22 at 16:57
  • Please add your shebang to your code. – Cyrus Jun 05 '22 at 16:59
  • 1
    `IFS=,` says to treat the comma as the input field delimiter but there are no commas in the sample input file you've provided hence the entire line is read into `values[0]`; it's not clear (to us) where there's a disconnect ... is your input file a real `.csv` (ie, fields are comma separated) or are the fields really delimited by a variable number of spaces (as in the provided sample); please update the question to clarify the contents of the actual file (eg, `head -5 filename`) – markp-fuso Jun 05 '22 at 17:16

1 Answers1

0

You are just reading and discarding the values in the file, not saving them.

Anyway, a much better solution is to rewrite this as an Awk script.

awk -F, 'FNR > 1 { $2 = ($2 == "female")
  $4 = ($4 == "yes) }1' "$1" >"$1.tmp" &&
mv "$1.tmp" "$1"

The static temp file is a security problem; use mktemp for any production script. The same mechanism would work for your original attempt, too; print to a new file, then move the file back to replace the original.

See also Bash while read loop extremely slow compared to cat, why? as well as When to wrap quotes around a shell variable?

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • GNU Awk has the capability to write changes back to the same file; see the `-i inplace` option. – tripleee Jun 05 '22 at 17:04
  • This assumes your file is really a CSV file (comma-separated values); if your real file uses a different format, maybe try a different value for the `-F` option, or in the worst case a different tool. – tripleee Jun 05 '22 at 17:07