Results 1 to 9 of 9
Hi All,
Can someone pls tell whether we can use 2 files for -f parameter in grep or fgrep command simultaneously?
I want to search keywords (any keyword stored in ...
- 04-15-2008 #1
fgrep -f : want to use 2 files at the same time
Hi All,
Can someone pls tell whether we can use 2 files for -f parameter in grep or fgrep command simultaneously?
I want to search keywords (any keyword stored in 2 different files) from the output that i'm getting some stdout.
i.e. echo "somevalue"|fgrep -f file1,file2
or echo "somevalue"|fgrep -f file1 file2
Any helps! (And while p[12] is not working with fgrep command though it is working fine with ls command). Thanks-
Example below:
HTML Code:[/E*Fare/Users/qabuild/aksutil/dir] $ cat p1 arun ARUN [/E*Fare/Users/qabuild/aksutil/dir] $ cat p2 sangal SANGAl SANGAL [/E*Fare/Users/qabuild/aksutil/dir] $ echo "arun"|grep -f p1 arun [/E*Fare/Users/qabuild/aksutil/dir] $ echo "arun"|grep -f p2 [/E*Fare/Users/qabuild/aksutil/dir] $ echo "sangal"|grep -f p1 [/E*Fare/Users/qabuild/aksutil/dir] $ echo "sangal"|grep -f p2 sangal [/E*Fare/Users/qabuild/aksutil/dir] $ ls -ltr p[12] -rw-rw-r-- 1 qabuild DENCCEFS 10 Apr 15 18:35 p1 -rw-rw-r-- 1 qabuild DENCCEFS 21 Apr 15 18:35 p2 [/E*Fare/Users/qabuild/aksutil/dir] $ echo "sangal"|grep -f p[12] [/E*Fare/Users/qabuild/aksutil/dir] $ echo "sangal"|grep -f p[21] [/E*Fare/Users/qabuild/aksutil/dir] $
- 04-15-2008 #2
- 04-15-2008 #3
That's right Anomie. But, this can be achieved simply by
grep "keyword" p[12]
grep "keyword" p* etc way, because we are mentioning the value of the keyword to be searched by ourself means..hardcoded at command prompt.
Now, what if you want to check sangal, Anomie, Pumba,...1000 keywords in in files starting with p (let's take we have only 2 files p1 and p2). But the pattern that we want to search should come from another file or stdout and not from us (i.e. we don't want to mention those 1000...n keywords at command prompt).
Thanks for your cover. still digging....
arun
- 04-15-2008 #4Just Joined!
- Join Date
- Apr 2008
- Posts
- 19
I am late
- 04-15-2008 #5
- 04-15-2008 #6
- 04-15-2008 #7
- 04-16-2008 #8Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
If I understand your question, you have more than one pattern file that you would like to have considered in one call to grep.
Apologies for the long post, but this seems to work for me:
Producing:Code:#!/usr/bin/env sh # @(#) s1 Demonstrate selection of multiple pattern files, grep. # ____ # / # | Infrastructure BEGIN echo set -o nounset debug=":" debug="echo" ## The shebang using "env" line is designed for portability. For # higher security, use: # # #!/bin/sh - ## Use local command version for the commands in this demonstration. set +o nounset echo "(Versions displayed with local utility \"version\")" version >/dev/null 2>&1 && version =o $(_eat $0 $1) grep set -o nounset echo FILE=${1-data1} echo " Input file $FILE:" cat $FILE PAT1=a echo echo " Pattern file $PAT1:" cat $PAT1 PAT2=b echo echo " Pattern file $PAT2:" cat $PAT2 # | Infrastructure END # \ # --- echo echo " Results from processing:" grep -f $PAT1 -f $PAT2 $FILE exit 0
If your version of grep does not allow multiple selections, then you could consider creating a temporary file with the contents of all the pattern files, and then using that single file as the pattern source ... cheers, drlCode:% ./s1 (Versions displayed with local utility "version") Linux 2.6.11-x1 GNU bash, version 2.05b.0(1)-release (i386-pc-linux-gnu) grep (GNU grep) 2.5.1 Input file data1: Now is the time for all good men to come to the aid of their country. In Xanadu did Kubla Khan A stately pleasure-dome decree: Where Alph, the sacred river, ran Through caverns measureless to man Down to a sunless sea. Pattern file a: all decree Pattern file b: country cavern Results from processing: for all good men of their country. A stately pleasure-dome decree: Through caverns measureless to man
Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 04-16-2008 #9


Reply With Quote
