Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Bash中使用grep_Bash_Csv - Fatal编程技术网

在Bash中使用grep

在Bash中使用grep,bash,csv,Bash,Csv,我试图浏览一个CSV文件(技术上是一个.gz文件)并阅读第二列。遇到问题,但我在使用第一个答案时遇到了一些困难 我使用的命令是grep${VALUE}inputfile.csv | cut-d,-f${INDEX} 对于我的shell脚本,我认为应该是grep${apples}inputfile.csv | cut-d,-f${2} 然而,我得到了一个错误 cut: option requires an argument -- 'f' Try 'cut --help' for more info

我试图浏览一个CSV文件(技术上是一个.gz文件)并阅读第二列。遇到问题,但我在使用第一个答案时遇到了一些困难

我使用的命令是
grep${VALUE}inputfile.csv | cut-d,-f${INDEX}

对于我的shell脚本,我认为应该是
grep${apples}inputfile.csv | cut-d,-f${2}

然而,我得到了一个错误

cut: option requires an argument -- 'f'
Try 'cut --help' for more information
The delimiter must be a single character
Try 'cut --help' for more information
命令中的逗号似乎不正确,因此我将其编辑为

grep${apples}inputfile.csv | cut-d-f${INDEX}

这让我犯了一个错误

cut: option requires an argument -- 'f'
Try 'cut --help' for more information
The delimiter must be a single character
Try 'cut --help' for more information
在苹果之后抓取字段的正确语法是什么?CSV文件头如下所示-非常简单:


水果|水果计数

您链接的问题的答案使用了变量来显示要使用的值的位置,您不需要
${}
语法:

grep apples inputfile.csv | cut -d, -f2

您链接的问题的答案使用了变量来显示要使用的值的位置,您不需要
${}
语法:

grep apples inputfile.csv | cut -d, -f2

可以用此awk脚本等效地替换

awk -F, '/apples/{print $2}' inputfile.csv

可以用此awk脚本等效地替换

awk -F, '/apples/{print $2}' inputfile.csv

VALUE
可以包含空格,因此
的“$VALUE”
更好(或者
的“${VALUE}”
如果您愿意–我不喜欢)。我不喜欢它,但希望它与引用的问题和答案保持一致。我已更新为将
VALUE
用引号括起来。是否在
“${VALUE}”周围加上双引号以搜索精确的字符串?@simplycoding,它适用于
VALUE=“apples and pears”
的情况。如果没有引号,
grep
将看到四个参数,而不是两个。您不需要
导出
这些变量
可能包含空格,因此
“$VALUE”
更好(或者
“${VALUE}”
如果您愿意–我不喜欢)。我不喜欢它,但希望与参考问题和答案保持一致。我已更新为将
VALUE
用引号括起来。是否在
“${VALUE}”周围加上双引号以搜索精确的字符串?@simplycoding,它适用于
VALUE=“apples and pears”
的情况。如果没有引号,
grep
将看到四个参数,而不是两个。您不需要
导出这些变量