Search code examples
arraysbashpattern-matchingfile-processing

list files containing two specific strings into an array in bash


Inside a directory(without any subdirectories) I have several text files.

I want to create a array containing the list of files which contains specific two Strings say Started uploading and UPLOAD COMPLETE.

for i /directory_path/*.txt; do
  #perform operations on 1
done

This is considering all the text files present in the directory. Here, in i I want only the files which contains the above said strings.

Any suggestion/help will be appriciated!


Solution

  • If you continue with your code, then do can do this

    declare -a files; 
    for i in directory_path/*.txt; do
       if grep -q 'Started Uploading' "$i" && grep -q 'UPLOAD COMPLETE' "$i"; then
           files+=("$i")
       fi 
    done
    
    echo "matching files: ${files[@]}"