根据另一列的值创建一个新列

Create a new column based on the values of other one

我有文件 data.csv,其中包含有关一家商店的客户体验和商店总消费的信息。 每个客户根据他们的客户体验给予积分,因此数据集如下所示:

Ranking     Total Spent 
9.5         1245
5           500.58
7.8         1000.69
3           200
6.2         412.45

我想创建一个名为“体验”的新列,其值取决于“排名”列。例如:

ranking >= 8 the new column value will be the string "Very satisfied" 
ranking >= 6 && ranking < 8 the new column value will be "Satisfied"
ranking >= 5 && ranking < 6 the new column value will be "Neutral"
ranking >= 3 && ranking < 5 the new column value will be "Bad"
ranking >= 0 && ranking < 3 the new column value will be "Horrible"

所以期望的输出是:

Ranking     Total Spent      Experience
9.5         1245             Very satisfied
5           500.58           Neutral
7.8         1000.69          Satisfied
3           200              Bad
6.2         412.45           Satisfied

我尝试使用以下代码但无法正常工作:

awk -F,'NR==1{="Experience";print;next} 
>=8 {print [=13=], "Very satisfied";next}
>=6 && <8 {print [=13=], "Satisfied";next}
>=5 && <6 {print [=13=], "Neutral";next}
>=3 && <5 {print [=13=], "Bad";next}
>=0 && <3 {print [=13=], "Horrible";next}' data.csv

你真的很亲近。只是 OFS 不见了。为避免必须转义引号,您可以创建这样的脚本

#! /usr/bin/awk -f
NR==1 {FS=","; OFS="\t"; ="Experience";print;next}
>=8 {print [=10=], "Very satisfied";next}
>=6 && <8 {print [=10=], "Satisfied";next}
>=5 && <6 {print [=10=], "Neutral";next}
>=3 && <5 {print [=10=], "Bad";next}
>=0 && <3 {print [=10=], "Horrible";next}

授予权限

chmod +x myscript

和运行它

./myscript data.csv