Count the number of occurrences of a string using sed?

前端 未结 6 1869
星月不相逢
星月不相逢 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:27

    I don't think sed would be appropriate, unless you use it in a pipeline to convert your file so that the word you need appears on separate lines, and then use grep -c to count the occurrences.

    I like Jonathan's idea of using tr to convert spaces to newlines. The beauty of this method is that successive spaces get converted to multiple blank lines but it doesn't matter because grep will be able to count just the lines with the single word 'title'.

提交回复
热议问题