Question Locked

Change values from a dataset variable in Bash

0 helpful
2 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hi
I am new in Bash and I am trying to change the values of a column from the file data.csv coma delimitted.

In the dataset I have the variable sex with only 2 possible values 'Female' and 'Male' and I would like to transform Male into 'm' and Female into 'f'. I have tried this:

#!/bin/bash
sex=$(cut -d , -f 5 data.csv) #I select column 5 related with the variable sex
for i in $sex; do
if [[$i='Female']]; then
$i='f'
fin
done
The code is wrong and I do not know how to modify it.

Besides, I would like to update my data.csv with the new values in sex.

2 replies

by techguy98

# bash
while read line
do
line=${line/,Male,/,m,}
line=${line/,Female,/,f,}
echo $line
done < data.csv > output2.csv

# sed
sed 's/,Male,/,m,/; s/,Female,/,f,/' data.csv > output3.csv

Regards,
TG

This discussion is closed to new replies.