Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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.
#/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...
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.
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).
#/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)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.