Find the answer to your Linux question:
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 ...
  1. #1
    Linux Newbie Sangal-Arun's Avatar
    Join Date
    May 2006
    Location
    Gurgaon, India + Denver Colorado USA
    Posts
    101

    Question 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] $
    Brgds,

    ARUN SANGAL
    SCM: 1- 720 251 9962
    Email: sangal.ak04@gmail.com
    Email: sangal_ak04@yahoo.com

  2. #2
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    You could assign the search expression to a variable, e.g.:
    Code:
    $ EXPR='sangal'
    $ grep "$EXPR" p1 p2

  3. #3
    Linux Newbie Sangal-Arun's Avatar
    Join Date
    May 2006
    Location
    Gurgaon, India + Denver Colorado USA
    Posts
    101
    Quote Originally Posted by anomie View Post
    You could assign the search expression to a variable, e.g.:
    Code:
    $ EXPR='sangal'
    $ grep "$EXPR" p1 p2


    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
    Brgds,

    ARUN SANGAL
    SCM: 1- 720 251 9962
    Email: sangal.ak04@gmail.com
    Email: sangal_ak04@yahoo.com

  4. #4
    Just Joined!
    Join Date
    Apr 2008
    Posts
    19
    I am late

  5. #5
    Linux Newbie Sangal-Arun's Avatar
    Join Date
    May 2006
    Location
    Gurgaon, India + Denver Colorado USA
    Posts
    101

    Cool

    Quote Originally Posted by shorye View Post
    I am late
    No, you are welcome to solve it. Any help!
    Brgds,

    ARUN SANGAL
    SCM: 1- 720 251 9962
    Email: sangal.ak04@gmail.com
    Email: sangal_ak04@yahoo.com

  6. #6
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    Quote Originally Posted by Sangal-Arun
    we are mentioning the value of the keyword to be searched by ourself means..hardcoded at command prompt.
    So don't hardcode it. Assign the value(s) of your operation to the variable. You can use egrep to search for more than one expression.

  7. #7
    Linux Newbie Sangal-Arun's Avatar
    Join Date
    May 2006
    Location
    Gurgaon, India + Denver Colorado USA
    Posts
    101
    Quote Originally Posted by anomie View Post
    So don't hardcode it. Assign the value(s) of your operation to the variable. You can use egrep to search for more than one expression.
    yeah! egrep will help us out....but i thought we could achieve that with fgrep.

    looks like fgrep doesn't support 2 files for having patterns....in one go.
    well, thanks a lot.
    Brgds,

    ARUN SANGAL
    SCM: 1- 720 251 9962
    Email: sangal.ak04@gmail.com
    Email: sangal_ak04@yahoo.com

  8. #8
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.
    Quote Originally Posted by Sangal-Arun View Post
    ... looks like fgrep doesn't support 2 files for having patterns....in one go.
    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:
    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
    Producing:
    Code:
    % ./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
    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, drl
    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 )

  9. #9
    Linux Newbie Sangal-Arun's Avatar
    Join Date
    May 2006
    Location
    Gurgaon, India + Denver Colorado USA
    Posts
    101

    Smile

    Quote Originally Posted by drl View Post
    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:
    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
    Producing:
    Code:
    % ./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
    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, drl

    Here comes DRL...thanks a lot. Found last night that we have to use -f n no. of times ...for n pattern files.

    Brgds,

    ARUN SANGAL
    SCM: 1- 720 251 9962
    Email: sangal.ak04@gmail.com
    Email: sangal_ak04@yahoo.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...