Let's say we have below file:
texst1 test1 test2 test1 test3 test1 test4 xtexst1 test5x test1
We can read the file, and use
grepl to search for "x".
grepl returns TRUE for matches, then we can
sum to get the count.
myFile <- readLines("file1.txt") sum(grepl(pattern = "x", x = myFile)) #  3
Shell / BASH:
cat test echo on hello world hello world alo mundo alo mundo hola mundo hola mundo echo off
Then, load R:
R system("grep -e 'hello' test | wc -l") 2 system("grep -e 'echo' test | wc -l") 2 system("grep -e 'echo' test") echo on echo off system("grep -e 'mundo' test | wc -l") 4 system("grep -e 'mundo' test") alo mundo alo mundo hola mundo hola mundo
system() allows you to invoke operating system commands.
You can also read the file into R but it would help if it were TSV or CSV, an there run the
grep() R command.