Grep command

I am trying to search a perticular pattern in all the files in a directory except one file.

i have used exclude grep to do that:

grep -c -E '�|�' /interfaces/qi/archive/20130919/*.dat

is returning total 73 rows in the result
while

grep -c -E '�|�' /interfaces/qi/archive/20130919/*[!MERCH_10.dat].dat

is not listing all the files except Merch_10 file. (35 files it listed)

Can you please help me on this.

Question:
If i am doing wrong in above command then how can i grep a pattern in a directory except one file (i dont want to chcek this pattern in one file).
I cannot specify all the files as there are 173 files present in directory.

Hi,

Please use code tags always for commands. Also kindly let us know the proper Output which you want to get.

Thanks,
R. Singh

Try this:

 find . \( -name "*.dat" -a ! -name MERCH_10.dat \) | xargs grep "string"

Here . ( dot ) is current directory for example.

Hi,

Thanks for your reply,
As i mentioned in folder there are 73 dat files but in result it is displaying only 35 files. rest of the files it is not checking. There is no unusual thing i noticed.

---------- Post updated at 05:19 AM ---------- Previous update was at 05:07 AM ----------

Hi,

Thanks for the code, is working as expected.
do you have any idea why the above mentioned command is not working as that one looks easier comapre to this.

grep has following options:

--exclude=GLOB ,
--exclude-from=FILE
--exclude-dir=DIR 
grep 'string' --exclude=MERCH_10.dat *.dat

I am not sure your command can give expected result.

Hi,

Thanks for your help and correcting me as i was not applying the command in proper way. the command is working fine.
That is my concerns also as it is returing some results and skipping some. Dont know on what basis it is missing some files there is no pattern (as date/filename etc)

Neither do we, you have given no detailed information. Show exactly what you did, exactly what happened, and exactly what you expected to happen.

In bash (alas you didn't mention your system data) you could also use "extended pattern matching":

shopt -s extglob
grep -c -E '�|�' /interfaces/qi/archive/20130919/!(MERCH_10*)

---------- Post updated at 02:04 AM ---------- Previous update was at 02:02 AM ----------

Thanks this command is now working as expected. but the problem is that now it checking for other files except MERCH_10, i need to check in All .dat files except MERCH_10.dat.

because if it checks for the all files then it can create problem if i placed some other extension having pound symbol my job will fail for unnecessary reason

---------- Post updated at 02:11 AM ---------- Previous update was at 02:04 AM ----------

Please find the difference in all command using exclude :[in this command Merch_10 file doesnot contains pound symbol, i want the 73 output using all the commands.]

grep -c -E '�|�' /interfaces/qi/archive/20130919/*[!MERCH_10.dat].dat | wc -l
35
grep -c -E '�|�' /interfaces/qi/archive/20130919/*.dat | wc -l 
73
grep -c -E '�|�' /interfaces/qi/archive/20130919/!(MERCH_10*) | wc -l
151

I think that told clearly the difference in two commands. can you please tell me what extra information you need on this. My target is to achieve the count 73 when merch_10 file does have pound symbol.