gpt4 book ai didi

bash - 替换csv文件的多个值

转载 作者:行者123 更新时间:2023-12-04 18:46:24 28 4
gpt4 key购买 nike

我有一个 csv 文件:

1,1,1,2
2,2,1,2
3,3,1,2
4,4,1,2
5,5,1,2
6,6,1,2
7,7,1,2
8,8,1,2
9,9,1,2
10,10,2,2
11,11,2,2
12,12,2,2
13,13,3,2

我想将每个第三个值替换为:
如果为 1;然后 22
如果 2;然后 35
如果 3;然后 14

这就是我所做的:
awk -F , -v OFS=, '{if ($3=="1") $3="22";if ($3=="2") $3="35";if ($3=="3") $3="14"} {print "\""$1"\""",""\""$2"\""",""\""$3"\""",""\""$4"\""}' /tmp/test.csv

它在 Debian 上运行良好,但在 Ubuntu 上却不行。
问题是什么 ?谢谢

[编辑]
使用我昨天引用的示例,它可以工作,但不适用于这个:
猫 -v 测试.csv
1,1,1,2
2,2,1,2
3,3,1,2
4,4,1,2
5,5,1,2
6,6,1,2
7,7,1,2
8,8,1,2
9,9,1,2
10,10,1,2
11,11,1,2
12,12,1,2
13,13,1,2
14,14,1,2
15,15,1,2
16,16,1,2
17,17,1,2
18,18,1,2
19,19,1,2
20,20,1,2
21,21,1,2
22,22,1,2
23,23,1,2
24,24,1,2
25,25,1,2
26,26,1,2
27,27,1,2
28,28,1,2
29,29,1,2
30,30,1,2
31,31,1,2
32,32,1,2
33,33,1,2
34,34,1,2
35,35,1,2
36,36,1,2
37,37,1,2
38,38,1,2
39,39,1,2
40,40,1,2

现在,命令返回:
awk -F , -v OFS=, '{if ($3=="1") $3="2";if ($3=="2") $3="3";if ($3=="3") $3="5"} {print "\""$1"\""",""\""$2"\""",""\""$3"\""",""\""$4"\""}' toast.csv
"1","1","5","2"
"2","2","5","2"
"3","3","5","2"
"4","4","5","2"
"5","5","5","2"
"6","6","5","2"
"7","7","5","2"
"8","8","5","2"
"9","9","5","2"
"10","10","5","2"
"11","11","5","2"
"12","12","5","2"
"13","13","5","2"
"14","14","5","2"
"15","15","5","2"
"16","16","5","2"
"17","17","5","2"
"18","18","5","2"
"19","19","5","2"
"20","20","5","2"
"21","21","5","2"
"22","22","5","2"
"23","23","5","2"
"24","24","5","2"
"25","25","5","2"
"26","26","5","2"
"27","27","5","2"
"28","28","5","2"
"29","29","5","2"
"30","30","5","2"
"31","31","5","2"
"32","32","5","2"
"33","33","5","2"
"34","34","5","2"
"35","35","5","2"
"36","36","5","2"
"37","37","5","2"
"38","38","5","2"
"39","39","5","2"
"40","40","5","2"

所有第三个值都等于 5 而不是 2。Debian 上的这个例子也有同样的问题。

最佳答案

您发布的代码在任何给定机器上与任何其他机器上的行为都不会有所不同。你说它确实并且最初发布错误的代码是一个红鲱鱼,你只是有错误的代码,仅此而已。

您在最新编辑中添加的代码说:

if ($3=="1") $3="2";if ($3=="2") $3="3";if ($3=="3") $3="5"

因此,假设您从输入文件中的 $3 开始,其值为 1。您的第一个测试/作业是 if ($3=="1") $3="2"所以在代码执行后 $3 的值为 2。现在你的第二个测试/分配是 if ($3=="2") $3="3"好吧,在你的第一个代码段执行后,$3 现在是 2,所以现在它被设置为 3。然后你的下一个测试/作业将它设置为 5。

因此,给定一个 3 美元,即 1,您将 3 美元设置为 2,然后将其设置为 3,然后将其设置为 5 - 最终结果是它始终为 5。加入一些“其他”:
if ($3=="1") $3="2"; else if ($3=="2") $3="3"; else if ($3=="3") $3="5"

但至少更改您的脚本以避免必须单独打印每个字段:
awk -F, -v OFS='","' '{if ($3=="1") $3="2"; else if ($3=="2") $3="3"; else if ($3=="3") $3="5"} {print "\""$0"\""}' toast.csv

并考虑使用更惯用的方法:
$ cat file
9,9,1,2
10,10,2,2
13,13,3,2

$ awk -F, -v OFS='","' 'BEGIN{split("2,3,5",m)} {$3=m[$3]} {print "\""$0"\""}' file
"9","9","2","2"
"10","10","3","2"
"13","13","5","2"

以上假设您的 $3 始终是您显示/测试的值之一。如果没有,则很容易进行调整。

通常将一组任意数字映射到另一组并允许一些不需要映射的输入数据:
$ awk -F, -v OFS='","' 'BEGIN{split("1,2,3",a); split("2,3,5",b); for (i in a) m[a[i]]=b[i]} {$3=($3 in m ? m[$3] : $3)} {print "\""$0"\""}' file
"9","9","2","2"
"10","10","3","2"
"13","13","5","2"

或者,如果您愿意:
$ awk -F, -v OFS='","' 'BEGIN{split("1,2,2,3,3,5",t); for (i=2;i in t;i+=2) m[t[i-1]]=t[i]} {$3=($3 in m ? m[$3] : $3)} {print "\""$0"\""}' file
"9","9","2","2"
"10","10","3","2"
"13","13","5","2"

关于bash - 替换csv文件的多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36843765/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com