You could try to use fread
from data.table
package with cmd
option. From documentation:
A shell command that pre-processes the file; e.g. fread(cmd=paste("grep",word,"filename"). See Details.
Shell commands:
fread accepts shell commands for convenience. The input command is run and its output written to a file in tmpdir (link{tempdir}() by default) to which fread is applied "as normal". The details are platform dependent -- system is used on UNIX environments, shell otherwise; see system.
So if you run something like
library(data.table)
t <- fread(......., cmd=paste("grep","' A '","filename"), .....)
then it filters lines which contains A
(A surrounded by spaces) and then apply fread
to the result.