Find the answer to your Linux question:
Results 1 to 7 of 7
declare -a phrasses phrasses[0]="phrasse 1" phrasses[1]="phrasse 2 something'something\"something\" https://www.domain.com/');" phrasses[2]="phrasse 3" wheretosearch=/home outputfile=/tmp/find_malicious_output.txt for (( i=0;i<${#phrasses[*]};i++ )); do phrasse=${phrasses[$i]} echo "$phrasse" find $wheretosearch -type f -size -800k -mmin -1440 -exec ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux User postcd's Avatar
    Join Date
    Apr 2011
    Posts
    310

    Bash: find command search several phrasses in one file


    declare -a phrasses
    phrasses[0]="phrasse 1"
    phrasses[1]="phrasse 2 something'something\"something\" https://www.domain.com/');"
    phrasses[2]="phrasse 3"

    wheretosearch=/home
    outputfile=/tmp/find_malicious_output.txt

    for (( i=0;i<${#phrasses[*]};i++ )); do
    phrasse=${phrasses[$i]}
    echo "$phrasse"
    find $wheretosearch -type f -size -800k -mmin -1440 -exec grep -l "$phrasse" {} \; >> $outputfile
    Please how can i make this to search one file only once and all phrasses in it?
    I mean i have around 30 phrasses and i dont want to do search 30 times, but only 1 time and search in file, you know to save io resources and time
    "Avoid the Gates of Hell. Use Linux affordable VPS."

  2. #2
    Just Joined! mrbruno's Avatar
    Join Date
    Jan 2013
    Location
    /MilkyWay/Sol/Earth/USA/NC/Raleigh
    Posts
    63
    Quote Originally Posted by postcd View Post
    Please how can i make this to search one file only once and all phrasses in it?
    I mean i have around 30 phrasses and i dont want to do search 30 times, but only 1 time and search in file, you know to save io resources and time
    It sounds like you might want to use the alternation regular expression metacharacter but it's not clear to me whether or not your phrases already make use of such metacharacters. I see the vertical bar in phrasses[1] so maybe you're already using it and I'm not sure what your intention is.

    Anyway, you might be able to combine all the phrases into one massive regular expression:

    Code:
    regexpr=${phrasses[0]}
    for (( i=1;i<${#phrasses[*]};i++ ))
    do
      regexpr="$regexpr\|${phrasses[$i]}"
    done
    
    find $wheretosearch -type f -size -800k -mmin -1440 -exec grep -l "$regexpr" {} \; >> $outputfile

  3. #3
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,283
    Hi.

    The command grep can handle a file of regular expressions, and, as in many *nix commands, it can process a list of files. I would take advantage of both those features. For example:
    Code:
    #!/usr/bin/env bash
    
    # @(#) s2	Demonstrate approaches for multiple patterns, multiple files.
    
    # Utility functions: print-as-echo, print-line-with-visual-space, debug.
    # export PATH="/usr/local/bin:/usr/bin:/bin"
    LC_ALL=C ; LANG=C ; export LC_ALL LANG
    pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
    pl() { pe;pe "-----" ;pe "$*"; }
    db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
    db() { : ; }
    C=$HOME/bin/context && [ -f $C ] && . $C grep xargs find
    
    # Create a pattern file.
    echo "prize" > pattern
    echo "bingo" >> pattern
    
    pl " Content of pattern file:"
    cat pattern
    n=1000
    
    # Create n files, the last two to contain "prize" and "bingo
    (( first=n-2 ))
    for i in $( eval echo {1..$first} )
    do
      # echo Creating file $i
      touch t$i
    done
    pe "prize" > t$(( n-1 ))
    pe "bingo" > t$n
    
    extant=$( ls -1 t* | wc -l )
    pl " Case 1: individual calls, $extant files:"
    time {
    for i in t*
    do
      grep -l -f pattern $i
    done
    }
    
    pl " Case 2: single call, $extant files:"
    time {
    grep -l -f pattern $( find . -type f -name 't*' )
    }
    
    ngroup=20
    (( calls=$extant/$ngroup ))
    pl " Case 3: xargs call, $ngroup in a group, $extant files ($calls calls):"
    time {
    find . -type f -name 't*' |
    xargs --max-lines=$ngroup grep -l -f pattern
    }
    
    # Remove files.
    # ls t*
    rm -f t*
    
    exit 0
    producing:
    Code:
    $ ./s2
    
    Environment: LC_ALL = C, LANG = C
    (Versions displayed with local utility "version")
    OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
    Distribution        : Debian 5.0.8 (lenny, workstation) 
    bash GNU bash 3.2.39
    grep GNU grep 2.5.3
    xargs (GNU findutils) 4.4.0
    find (GNU findutils) 4.4.0
    
    -----
     Content of pattern file:
    prize
    bingo
    
    -----
     Case 1: individual calls, 1000 files:
    t1000
    t999
    
    real	0m1.512s
    user	0m0.456s
    sys	0m1.020s
    
    -----
     Case 2: single call, 1000 files:
    ./t999
    ./t1000
    
    real	0m0.020s
    user	0m0.000s
    sys	0m0.020s
    
    -----
     Case 3: xargs call, 20 in a group, 1000 files (50 calls):
    ./t999
    ./t1000
    
    real	0m0.108s
    user	0m0.032s
    sys	0m0.052s
    There is some system overhead in executing grep for each individual file, as noted in Case 1. That can be compared to grep processing the entire list of files as in Case 2.

    However, there might be situations where the list of files is too long. In Case 3, the use of command xargs is shown, collecting ngroup of filenames and running as many executions of grep as necessary to exhaust the full list.

    See man pages for details.

    Best wishes ... 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 )

  4. #4
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    224
    hi drl,

    you don't seem to realize how difficult it is to focus on the one-liners you provide amongst the bunch of crap you wrap it in

  5. #5
    Linux User
    Join Date
    Dec 2011
    Location
    Turtle Island West
    Posts
    344
    Why can you not use 'grep -f'?

  6. #6
    Linux User postcd's Avatar
    Join Date
    Apr 2011
    Posts
    310
    Quote Originally Posted by Miven View Post
    Why can you not use 'grep -f'?
    im not sure, if i can use it, please mention how the command should look like in my case (in my first post i quoted phrasses that i lookup in files. would be gratefull
    "Avoid the Gates of Hell. Use Linux affordable VPS."

  7. #7
    Linux User
    Join Date
    Dec 2011
    Location
    Turtle Island West
    Posts
    344
    Quote Originally Posted by postcd View Post
    im not sure, if i can use it, please mention how the command should look like in my case (in my first post i quoted phrasses that i lookup in files. would be gratefull
    Sure. You put your patterns in a text file, one per line. Let's call it 'patterns.txt'. Then, if the file you want to search is 'phrases.txt', you type"
    Code:
    grep -f patterns.txt phrases.txt
    It works for me anyway.

    Peace and Cheer.

Posting Permissions

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