LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-25-2016, 09:17 AM   #1
bbraml
LQ Newbie
 
Registered: Sep 2011
Posts: 9

Rep: Reputation: Disabled
using grep and passing terms from a file


I have a file with 4000+ terms I need to search for in my log files. How can I pass each of these terms to grep for my search? would it be something like
Quote:
grep -Rf filewithkeywords.txt /var/log/*
to search all log files recursively under /var/log?

Last edited by bbraml; 07-25-2016 at 09:28 AM. Reason: added content
 
Old 07-25-2016, 09:27 AM   #2
24x7servermanagement
Member
 
Registered: Jul 2016
Location: India
Distribution: CentOS, Redhat, Ubuntu and Debian
Posts: 49

Rep: Reputation: Disabled
Hi,

You can do one thing, copy those 4000+ terms in a text file (/root/a.txt) and use for command to search a term in the log file.

For ex :-

for i in `cat /root/a.txt` ; do grep $i /var/log/messages; done
 
Old 07-25-2016, 11:35 AM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,926

Rep: Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690
Quote:
Originally Posted by 24x7servermanagement View Post
Hi,
You can do one thing, copy those 4000+ terms in a text file (/root/a.txt) and use for command to search a term in the log file.
For ex :-
for i in `cat /root/a.txt` ; do grep $i /var/log/messages; done
Wrong. Did you bother to read the man page on the grep command?? If so, did you happen to see the -f flag, which lets you use a file as the input pattern?? And you're the Chief Technical Office somewhere?

OP: Your solution would work just fine, since you're doing it recursively, with the -f flag.

Last edited by TB0ne; 07-25-2016 at 11:36 AM.
 
Old 07-25-2016, 11:44 AM   #4
bbraml
LQ Newbie
 
Registered: Sep 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
Thanks TB0ne I thought I had it wrong.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
grep to file outputs more than grep to screen? tcpman Linux - Server 4 06-07-2013 05:46 AM
Script by passing a FILE without executing it...!!! NetRock Programming 5 03-21-2010 08:43 AM
Passing args in terminal window is OK. but isn't when passing in GNOME launcher why? majrys1962 Debian 0 11-18-2008 07:00 PM
grep output on stdout and grep output to file don't match xnomad Linux - General 3 01-13-2007 05:56 AM
C program FILE passing exvor Programming 3 09-19-2006 04:52 AM


All times are GMT -5. The time now is 04:38 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration