【发布时间】:2020-07-10 22:48:16
【问题描述】:
我有什么:
file.csv
car, speed, gas, color
2cv, 120, 8 , green
vw, 80, , yellow
Jaguar, 250, 15 , red
Benz, , , silver
我发现了什么:
此脚本按列号准确返回我需要的内容:
#!/bin/bash
awk -F', *' -v col=3 '
FNR>1 {
if ($col)
maxc=FNR
}
END {
print maxc
}
' file.csv
read -p "For End press Enter or Ctl + C"
我得到了我需要的输出(最后一行的编号):
* for "col=1" ("car" column), the answer: 5
* for "col=2" ("speed" column), the answer: 4
* for "col=3" ("gas" column), the answer: 4
* for "col=4" ("color" column), the answer: 5
我在寻找什么:
- 我正在寻找一种方法来获得相同的结果,而不是通过“vol=volumnumber p.e. vol=3”,而是通过“vol=columnheadlinevalue p.e. vol=gas”。
可以,需要额外的喜欢:
col_name=gas # selected column headline
col=get column number from $col_name # not working part
【问题讨论】:
-
预期输出是什么?请阅读MCVE
-
在问题中需要澄清几点:(a) 列标题是否包含在计数中? (b) 不同列的行数何时会不同?在示例中,“汽车”、“速度”和“颜色”的计数似乎都是 4。在典型的“CSV”文件中,通常会考虑填充每一行中的每一列,即使它用空字符串填充。
-
我已经清楚地修改了我的问题,因此它现在可能除了我想要的解释之外几乎没有其他解释的余地。