Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.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 shell:使用grep的列中出现的字符串数_Bash_Shell - Fatal编程技术网

Bash shell:使用grep的列中出现的字符串数

Bash shell:使用grep的列中出现的字符串数,bash,shell,Bash,Shell,我的数据文件是: name,age,favourite_person Adam,19,Helen Keller Alex,18,Joe Biden Kyle,18,George Washington Mary,20,Marie Curie Jade,16,Marie Kondo 我想找出“最喜欢的人”栏(第3栏)中出现“玛丽”的次数。我现在的代码是grep-R“Marie”文件| wc-l,但这会检查整个文件中的单词“Marie”。我只想让它在“最喜欢的人”栏中勾选。在这种情况下,我应该添加什

我的数据文件是:

name,age,favourite_person
Adam,19,Helen Keller
Alex,18,Joe Biden
Kyle,18,George Washington
Mary,20,Marie Curie
Jade,16,Marie Kondo

我想找出“最喜欢的人”栏(第3栏)中出现“玛丽”的次数。我现在的代码是
grep-R“Marie”文件| wc-l
,但这会检查整个文件中的单词“Marie”。我只想让它在“最喜欢的人”栏中勾选。在这种情况下,我应该添加什么?

您可以按如下方式使用awk:

awk'BEGIN{FS=“,”}{if($3~“Marie”){count++}}END{print count}文件
  • BEGIN{FS=“,”}
    设置为字段分隔符
  • {if…}
    部分的内容类似于“如果第三个字段与“Marie”匹配,则递增变量
    count
  • END{print count}
    在末尾打印
    count

  • 您可以使用
    cut
    以及
    grep

    cut-d”、“-f3文件| grep Marie | wc-l

    -d
    表示delimeter,而
    -f3
    仅取第三列


    grep-Marie
    检查Marie是否在第三列,并且
    wc-l
    统计发生次数

    awk
    更合适。或者可能
    cut
    ,例如,`grep-c“Marie”与awk方法相比,此方法返回的计数少9个,我想知道为什么?