Count the number of occurrences of a string using sed?

前端 未结 6 1820
星月不相逢
星月不相逢 2020-12-28 17:18

I have a file which contains \"title\" written in it many times. How can I find the number of times \"title\" is written in that file using the sed command provided that \"t

6条回答
  •  醉梦人生
    2020-12-28 17:45

    just one gawk command will do. Don't use grep -c because it only counts line with "title" in it, regardless of how many "title"s there are in the line.

    $ more file
    #         title
    #  title
    one
    two
    #title
    title title
    three
    title junk title
    title
    four
    fivetitlesixtitle
    last
    
    $ awk '!/^#.*title/{m=gsub("title","");total+=m}END{print "total: "total}' file
    total: 7
    

    if you just want "title" as the first string, use "==" instead of ~

    awk '$1 == "title"{++c}END{print c}' file
    

提交回复
热议问题