Script to find the string contain in which file 1000 files.

Hi Greetings
i have 1000 files (xmlfiles) and i need to find which file contain the string over 1000 file
all file end with txt
i tried with

grep -c "string" *.txt 

i am getting an error

-bash: /bin/egrep: Argument list too long

i have put an script like below

#!/bin/bash
for line in `cat flist`
do
     a=" grep -c "1024" $line "
     if [ a = 1 ]
     then
          echo "$line"
     fi
done

i do whats wrong with my code, Kindly guide any one for my reuirement

Regards
Venikathir

You need xargs:

find . -name \*.txt | xargs grep -c "string"

this one is working
its giving all files with 0 and 1
i want to list only if the string contain files is there any other way to

grep -c is count lines, grep -l is list files. See man grep.

your original cmd works fine here:

grep -c "string" *.txt

did test in this way:

echo "one">1.txt
touch {2..1000}.txt

ARG_MAX varies.

Regards,
Alister

This will do it:

#!/usr/bin/ksh
for mFName in *.txt; do
  mRC=$(grep -c 'string' ${mFName})
  if [[ "${mRC}" != "0" ]]; then
    echo "Found string in <${mFName}>"
  fi
done

thnx you all for your kind help
my issue is that string will contain in more then one time

i managed some below

#/bin/bash
echo " Enter the DIR Path"
read dir
echo "File types"
read file
ls $dir | grep $file > flist
echo "enter the string"
read string
for line in `cat flist`
do
#a=" grep -c "1024" $line "
a=`grep -c "$string" $dir/$line`
if [ $a != 0 ]
then
#echo $a
echo "$line "
fi
done

kindly check this and let me know i want to make this more stable because
my txt file contain more then 500 lines (xml lines)

as DGPicket said: Use "grep -l"

find . -name \*.txt | xargs grep -l "string"

You do not want to call grep for every file, but sometimes there are too many files for "*.txt" to be either robust or low latency, so divide the find from the grep using xargs or parallel.

Use "grep -l" since you just want to list any presence not a -c count:

find your_dir -name '*.txt' | xargs grep -l 'your_string'