grep spool file error

Hi,
I am trying to do this
For i in *.txt
do
sed 'some pattern checking' $i
done
>file

These text files are spool files.And when i find error in these spool files I want to append the text file contents to error.log file.
Or is there any option that I do this sed for only files that does not contain errors?
Can some one tell me how can i do this please..

this would give you a list of files that contain the pattern you are looking for:

grep -l 'pattern_to_check' * | sort | uniq

so you could do:

for file in `grep -l 'pattern_to_check' * | sort | uniq`
do
    cat $file  >> error.log
done

I don't think you need the sort | uniq because grep will only print each file name once. In any event, the purpose of cat is to concatenate multiple files, so you really don't need the loop either.

cat `grep -l 'pattern_to_check' *.txt` >>error.log

Thanks Yogesh it worked...

Hi,
Please tell me how I can grep files which does not have given pattern.

Thanks

hi ran16,
use -v option in grep
Thanks and Regards,
uttam hoode

If I use like grep -v 'ERROR' *.txt then I get lines also from the text files which do not have ERROR pattern
I want to grep only files that do not have the pattern

I even used 'grep -r exclude' option but some how syntax is wrong I think so.

Please tell me the exact command

grep -v will find any file which contains a line which doesn't match, so that's not what you want.

for f in *.txt; do
  grep 'pattern' "$f" >/dev/null && continue
  echo $f
done |
xargs cat >nonmatching.txt

Just take out the pipeline to xargs at the end if you just want to list the file names.

Era,
my requirement is

for f in 'grep 'pattern' "$f" >/dev/null .....(need syntax)
Do some sed or awk
done

That is I want to do a 'for loop and do some awk' for files
which does not contain the pattern.

The syntax from above should work. The continue will break out of the loop for those files which have a match; if you get past that line, $f contains the name of a file which does not contain a match. (Oops, should properly double quote "$f" in case the file name contains special characters, always.)

for f in *.txt; do
  grep 'pattern' "$f" >/dev/null && continue
  do some sed or awk with "$f"
done

Quite possibly you could "do some sed or awk" with xargs as per what I posted before, for improved efficiency.

Era,
I have tried your code but still awk is done for all the files including the files with pattern
I doubt after awk when i give awk ' ' $i
it is taking file from the *.txt instead of the set we got from grep.

I'm not sure I understand you. Here's a quick demonstration of what you should be seeing.

vnix$ for f in foo food bar barn ; do echo This is $f >$f; done
vnix$ ls
bar  barn  foo  food
vnix$ nl foo
     1  This is foo
vnix$ for f in *; do
>   grep foo "$f" >/dev/null && continue
>   nl "$f"
> done
     1  This is bar
     1  This is barn

As you can see, nl runs on each file which does not match the pattern foo, one at a time in turn. As far as I can understand, this is what you want to do as well (but with awk instead of nl, which I just used as a simple example here).

Era
Thank you for your patience in answering me.
My problem got solved.