1 Reply Latest reply: May 22, 2013 4:20 PM by Catch_22 RSS

    Help with a simple script

    993195
      Hello,

      I am trying to search through a large group of files for a list of strings. I have my my list called list.txt which is just a carriage return separated list of 800 strings. I want to search a group of files for the string, and if it finds it, output the line to a file. What it is doing is creating the logfile even if it doesn't find the string. Since my list is 800 items and it's searching thousands of files I don't want all the logfiles generated if it's not finding the match. Here is what I have and it is working.
      #!/bin/sh
      for list in `cat list.txt`
      do
              grep $list /u01/h/nomadd/archive/xml_archive/P3I_214A_20130522* > /u04/temp/xml/${list}_findings3.log 2>&1
              find /u04/temp/xml/ -name '*.log' -size 0 -print0 | xargs -0 rm
      done
      What I would like to do is get rid of the find and remove and not generate the log file if there is nothing in it. The find is really slowing down the processing.

      Thanks
        • 1. Re: Help with a simple script
          Catch_22
          How about the following example. You can easily customize it to your needs.

          1. Create some sample files:
          $ mkdir sample1
          $ cd sample1
          $ for (( i=1; i<=10; i++ )); do echo -e "${i}1\n${i}2" > file$i.txt; done
          $ echo -e "11\n32\n51\n72\n91" > search.inf
          $ ls
          file10.txt  file2.txt  file4.txt  file6.txt  file8.txt  search.inf
          file1.txt   file3.txt  file5.txt  file7.txt  file9.txt  
          2. Create a script, e.g. "search" with following content:
          #!/bin/bash
          # Filename: search
          # Author: Dude
          # Purpose: Search file content demo
          #
          logfile=logfile.lst
          #
          for file in *.txt; do
             lnr=0
             while read line; do
                   (( lnr++ ))
                   for item in $(cat search.inf); do
                   [ $(echo $line | grep "$item") ] && \
                      echo "Found $item in $file, line $lnr." >> $logfile
                done       
             done < $file
          done
          #
          #eof
          3. Save the file and set execute privileges
          chmod u+x search
          4. Test the result:
          ./search
          cat logfile.lst
          Found 11 in file1.txt, line 1.
          Found 32 in file3.txt, line 2.
          Found 51 in file5.txt, line 1.
          Found 72 in file7.txt, line 2.
          Found 91 in file9.txt, line 1.