LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-15-2008, 10:35 AM   #1
dipuasks
Member
 
Registered: Oct 2005
Location: India
Distribution: Redhat 7-9,Fedora Core 3 - 9, RHEL 4 -5, CentOS 4 - 5, Ubuntu 7.10 - 12.10, Mandirva 2008 -2009
Posts: 133

Rep: Reputation: 16
Cool 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
 
Old 04-15-2008, 11:06 AM   #2
lcole
Member
 
Registered: Aug 2003
Location: Oregon
Distribution: Ubuntu
Posts: 51

Rep: Reputation: 18
Try something like this

for i in `cat ~/list_of _files`; do grep filter-text $i ; done
 
Old 04-15-2008, 11:34 AM   #3
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979
Blog Entries: 1

Rep: Reputation: 209Reputation: 209Reputation: 209
Quote:
Originally Posted by lcole View Post
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
 
Old 04-15-2008, 11:58 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by custangro View Post
Another way you can do it...
W/o need for a "for" loop shouldn't "grep -e filter-text -r $(< /path/to/file)" work?
 
Old 04-15-2008, 12:54 PM   #5
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979
Blog Entries: 1

Rep: Reputation: 209Reputation: 209Reputation: 209
Quote:
Originally Posted by unSpawn View Post
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
 
Old 04-16-2008, 07:52 AM   #6
dipuasks
Member
 
Registered: Oct 2005
Location: India
Distribution: Redhat 7-9,Fedora Core 3 - 9, RHEL 4 -5, CentOS 4 - 5, Ubuntu 7.10 - 12.10, Mandirva 2008 -2009
Posts: 133

Original Poster
Rep: Reputation: 16
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
 
Old 04-16-2008, 09:38 AM   #7
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by dipuasks View Post
to check which one contains "spf" record and which doesn't.
'man grep', see "-l" or "-H".
 
Old 04-16-2008, 10:00 AM   #8
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979
Blog Entries: 1

Rep: Reputation: 209Reputation: 209Reputation: 209
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
 
Old 04-16-2008, 10:20 AM   #9
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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.
 
Old 04-16-2008, 10:50 AM   #10
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979
Blog Entries: 1

Rep: Reputation: 209Reputation: 209Reputation: 209
Quote:
Originally Posted by unSpawn View Post
grep -i spf -l -r /path/to/dir
This works in Linux but not Solaris...but it would be cool if it did

-C
 
Old 04-16-2008, 11:11 AM   #11
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by custangro View Post
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).
 
Old 04-21-2008, 03:39 AM   #12
dipuasks
Member
 
Registered: Oct 2005
Location: India
Distribution: Redhat 7-9,Fedora Core 3 - 9, RHEL 4 -5, CentOS 4 - 5, Ubuntu 7.10 - 12.10, Mandirva 2008 -2009
Posts: 133

Original Poster
Rep: Reputation: 16
Thumbs up

Quote:
Originally Posted by custangro View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
cat to grep to textfile? jabfinger Linux - General 3 09-05-2006 03:25 PM
cat [file] | grep --- trouble bob_man_uk Linux - General 12 03-10-2006 06:05 AM
Question About Cat And Grep George2 Linux - Software 10 03-09-2006 03:33 AM
bash: how to edit cat <filename> | grep <keyword> feature? sirpelidor Linux - Software 2 06-20-2005 02:00 PM
output to a file - cat? grep? Godsmacker777 Linux - Newbie 6 12-08-2004 10:06 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration