To find number of occurrences of a word taken as input from command line in unix

后端 未结 3 1648
孤城傲影
孤城傲影 2021-01-27 18:08

For the file file1.txt which contains

Apple fruit Apple tree
Tree AApple AApklle Apple apple
TREE
Apple

I want to find number of o

3条回答
  •  盖世英雄少女心
    2021-01-27 18:34

    With GNU awk for multi-char RS:

    $ awk -v RS='\\' 'END{print (NR ? NR-1 : 0)}' file
    4
    

    or with a shell variable:

    $ tofind='Apple'
    $ awk -v RS='\\<'"$tofind"'\\>' 'END{print (NR ? NR-1 : 0)}' file
    4
    

提交回复
热议问题