I would like to ask how to grep in a certain file that contains filenames, criteria for grep will come from another file. Example:

fileA.txt contains:


fileB.txt contains:

870752450724347692,2019-11-11 00:12:17,1573411337,1573407737,1573414937
870423450725349173,2019-11-11 00:21:08,1573411868,1573408268,1573415468
868909450732354559,2019-11-11 00:56:43,1573414003,1573410403,1573417603

where 2nd column is date/time,

3rd column is date/time in epoch,

4th column is date/time -1hr in epoch,

5th column is date/time +1hr in epoch.


How do I grep in fileA.txt (get each matching newFile0000_XXXXXXXXXX000_000_0.txt)

using 4th column (lower limit) and 5th column (upper limit) as range from each line in fileB.txt?

So from the example, fileA.txt 3rd entry should be fetched based from fileB.txt 2nd entry.

  • For this task, grep is not the right tool. This would require something like a shell script with two loops, one inside the other: the outer loop would parse fileB.txt, one line at a time, and find the lower and upper limits for each search. Then the inner loop would read fileA.txt, one line at a time, pick out just the timestamp on the line, compare it numerically to the limits found by the outer loop, and based on that, make a decision whether to output the line from fileA.txt or not.
    – telcoM
    Nov 11 '19 at 6:37
  • hi @telcoM, that is what i have tried so far but it's taking time. i am reducing it now to just browsing through each line in fileB.txt and directly grep in fileA.txt given the conditions but i am having a hard time.
    – dcdum2018
    Nov 11 '19 at 6:41

Using awk:

awk -F, 'NR==FNR { myArray[$0]=substr($0,13,10); next } 
                 { for (file in myArray) {
                       if (myArray[file]>=$4 && myArray[file]<=$5) 
                           { print file; delete myArray[file] }
                 }' fileA.txt fileB.txt

Alternative using multiple separators. Change the first code-block to:

NR==FNR { myArray[$0] = $2/1000; next }

Run it like this:

awk '...awk-script...' FS=_ fileA.txt FS=, fileB.txt
  • hi @αғsнιη, i have tried your suggestion in a bash script but it's not working
    – dcdum2018
    Nov 11 '19 at 10:40
  • it's not returning anything, i have declared fileA='pwd'/fileA.txt and fileB='pwd'/fileB.txt then used $fileA $fileB at the last line... i have also tried renaming the files as is to fileA fileB, still not returning anything. i really appreciate your help on this :)
    – dcdum2018
    Nov 11 '19 at 11:02
  • with 'pwd'/fileA.txt I'm assuming you mean backticks `pwd`/fileA.txt, right? and for files you only need to pass your fileA.txt mentioned in your question as first file and fileB.txt in your question as second file, both in last line only in command above (it was my fault I mentioned array name same as files' name) Nov 11 '19 at 11:09
  • yes, i used backticks. with the updated suggestion, no result is being shown. note that i am putting it in a bash script
    – dcdum2018
    Nov 12 '19 at 2:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.