LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   cat and grep (https://www.linuxquestions.org/questions/linux-general-1/cat-and-grep-635512/)

dipuasks 04-15-2008 10:35 AM

cat and grep
 
Hello Friends,

I want to cat and grep some files and redirect the output to an output file.
The problem is I am having lots of files to check, so I have put all the file paths in a text file paths.txt.
I want cat to pick up paths one by one from paths.txt and check, not sure how to pick up one by one path from the file. I don't have much scripting knowledge. Please help me in this case.

Thanks & Regards,
Dipu

lcole 04-15-2008 11:06 AM

Try something like this

for i in `cat ~/list_of _files`; do grep filter-text $i ; done

custangro 04-15-2008 11:34 AM

Quote:

Originally Posted by lcole (Post 3122026)
Try something like this

for i in `cat ~/list_of _files`; do grep filter-text $i ; done

Another way you can do it...

Code:

for i in $(< /path/to/file)
do
  grep filter-text $i
done


"cat" Forks another process which slows your script down a bit.

Remember...

Quote:

Never write it in 'C' if you can do it in 'awk';
Never do it in 'awk' if 'sed' can handle it;
Never use 'sed' when 'tr' can do the job;
Never invoke 'tr' when 'cat' is sufficient;
Avoid using 'cat' whenever possible.
--Taylor's Laws of Programming
-C

unSpawn 04-15-2008 11:58 AM

Quote:

Originally Posted by custangro (Post 3122052)
Another way you can do it...

W/o need for a "for" loop shouldn't "grep -e filter-text -r $(< /path/to/file)" work?

custangro 04-15-2008 12:54 PM

Quote:

Originally Posted by unSpawn (Post 3122071)
W/o need for a "for" loop shouldn't "grep -e filter-text -r $(< /path/to/file)" work?

That also works...

That's what I like about *nix systems...so many ways to do the same thing :)

-C

dipuasks 04-16-2008 07:52 AM

Amazing response guys!! Thanks a lot. I got to learn bash scripting now.

I tried both the ways and all worked fine. But all what I am getting is a list of filter-text(the keyword i searched), so I am unable to determine which file contains the filter-text and which doesn't. Kindly help me with that.

Here's why I want so- I need to grep all dns zone files on dns server to check which one contains "spf" record and which doesn't.

Thanks and Regards,
Dipu

unSpawn 04-16-2008 09:38 AM

Quote:

Originally Posted by dipuasks (Post 3122966)
to check which one contains "spf" record and which doesn't.

'man grep', see "-l" or "-H".

custangro 04-16-2008 10:00 AM

Maybe something like this....?

Code:

#/bin/bash
#
#
set -A zonefiles $(ls -1 /var/named/chroot/etc/zonefiledir)

for zonefile in ${zonefiles[*]}
do
  if [ $(grep -c spf ${zonefile}) -ge 1 ] ;then
    echo "${zonefile} has an SPF record"
  else
    :
  fi
done
#
#

There is probably a better way...but this is where I would start...

-C

unSpawn 04-16-2008 10:20 AM

In your case of looping over files you just the exit value and one instance per file suffices: "for item in list; do grep -i spf -m1 -q item && echo SPF record". OTOH w/o looping "grep -i spf -l -r /path/to/dir" returns all filenames containing the case-insensitive term "spf" just fine.

custangro 04-16-2008 10:50 AM

Quote:

Originally Posted by unSpawn (Post 3123102)
grep -i spf -l -r /path/to/dir

This works in Linux but not Solaris...but it would be cool if it did :)

-C

unSpawn 04-16-2008 11:11 AM

Quote:

Originally Posted by custangro (Post 3123130)
This works in Linux but not Solaris...

Even though we're in the Linux General forum, thanks for the reminder. Most of the times I forget compatibility unless I had recent beef with it (aka RKH).

dipuasks 04-21-2008 03:39 AM

Quote:

Originally Posted by custangro (Post 3123087)
Maybe something like this....?

Code:

#/bin/bash
#
#
set -A zonefiles $(ls -1 /var/named/chroot/etc/zonefiledir)

for zonefile in ${zonefiles[*]}
do
  if [ $(grep -c spf ${zonefile}) -ge 1 ] ;then
    echo "${zonefile} has an SPF record"
  else
    :
  fi
done
#
#


Hi,

I got errors with this script. The error is like:
filename : line 7 : spf: integer expression expected.
filename : line 7 : [: -ge: unary operator expected

However my requirement is fulfilled now!! I used the below command (as I needed the files which does not contain spf)

grep -r -L spf /path/to/directory

Thanks a lot to everyone.


All times are GMT -5. The time now is 10:28 AM.