我有文件数据.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

我try 了以下代码,但不起作用:

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

推荐答案

你真的很接近.只有OFS人失踪.为了避免转义引号,可以创建如下脚本

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

给予许可

chmod +x myscript

然后运行它

./myscript data.csv

Linux相关问答推荐

Bash:将多行转换为单行的命令

重命名具有相同前缀的文件对中最旧的文件

我需要从 Ubuntu 中删除 .txt 文件中以白色间距分隔的行的白色间距

将 Visual Studio C++ 项目迁移到 Linux 和 CMake

函数在 shell 脚本中抛出错误语法错误:} unexpected

为什么在 Linux 中使用 select

将 awk 输出保存到变量

如何在 Linux 中使用命令行将 PDF 转换为 JPG?

通过 Linux FrameBuffer 将像素绘制到屏幕上

何时判断 EINTR 并重复函数调用?

仅在不存在时添加换行符

如何告诉 valgrind 对Forking 进程进行 memcheck?

bash 中的线程?

docker compose:加载共享库时出错:libz.so.1:无法从共享对象映射段:不允许操作

如何将路径名中的..转换为 bash 脚本中的绝对名称?

Anaconda 安装后,conda 命令失败并显示ImportError: no module named conda.cli

在linux上上几个目录

使用 Bash 查找和复制文件

id_rsa.pub 文件 SSH 错误:格式无效

未找到版本CXXABI_1.3.8(...要求)