0

If I am searching/checking for an KeyWord/String using GREP command from 1000 files for example; then is there any option/way to see/display the percentage or number of files processed by the GREP command...?.

Just I would like to know how long further it will take to complete the search process.

Please help

Krishna
  • 609
  • 1
  • 6
  • 17

1 Answers1

1

try awk. This will print a messages suggesting the file number being processed, followed by the line matching the pattern. I have chosen the pattern here to be character p as that was easy for my testing. You can replace with any valid regex for your purpose.

awk -v PAT="p" 'FNR == 1 { n++; printf "Processing %d of %d files\n", n, ARGC - 1 >> "/dev/stderr"} FILENAME ~ /.gz$/ {print "Skipping gz file: ", FILENAME; nextfile} /PAT/'

Explanation

awk -v PAT="p" '
    FNR == 1 {                                # Every first line of the file
       n++;                                   # Counter for file being processed
       printf "Processing %d of %d files\n",  # Print message
          n,                                  # file number
          ARGC - 1                            # Number of files on CLI
          >> "/dev/stderr"                    # Redirect to stderr
    }
    FILENAME ~ /.gz$/ {                       # Skip gunzip files
       print "Skipping gz file: ", FILENAME; 
       nextfile                               # move to next file
    }
    /PAT/                                     # Pattern to print
'

UPDATE

Updated the code to skip the gunzip (.gz) files when processing.

Jay Rajput
  • 1,813
  • 17
  • 23
  • 1
    mixing output with messages is not a good practice, you may want to redirect those to stderr. – karakfa Nov 12 '16 at 23:35
  • Thanks @JayRajput ... it did somewhat I need or meant. But in addition to that **printf** output, I am getting some **junk** values as well. Actually in my case, the files I am having to be searched are **.gz** files; so is this is why I am getting those unwanted junk values...? any solution to avoid this...? – Krishna Nov 13 '16 at 04:25
  • Thanks @JayRajput .. But I need to consider *.gz* files as well. So please suggest me a generic command; so that it will process both normal and .gz files similarly. Sorry if I missguided you on my requirements – Krishna Nov 16 '16 at 07:05