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.
CodePudding user response:
awk -F , -v OFS=, '
$5 == "Female" {$5 = "f"}
$5 == "Male" {$5 = "m"} 1' data.csv
CodePudding user response:
# awk
# without header
awk -F, 'BEGIN{OFS=FS}{$5=="Male" ? $5="m" : $5="f"}1' data.csv > output1.csv
# with header
awk -F, 'BEGIN{OFS=FS} NR!=1{$5=="Male" ? $5="m" : $5="f"}1' data.csv > output1.csv
# 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