0

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!

RandomCoder
  • 79
  • 1
  • 7
  • I already showed you how to do that in [my answer to your previous question](https://stackoverflow.com/a/52579268/1745001). See the very first script in my answer. You're going down the wrong path with these shell loops and multiple greps solutions you keep accepting, there's just no need for more than a single awk command. – Ed Morton Sep 30 '18 at 22:22

2 Answers2

2

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[@]}"
apatniv
  • 1,771
  • 10
  • 13
0

As mentioned, grep -l can give you a list of files contining you match phrases. Using IFS, you can read that list straight from grep into an array:

#! /usr/bin/env bssh

OLD_IFS=$IFS
IFS=$'\n'

declare -a array=( $(grep -l 'Started uploading\|UPLOAD COMPLETE' "dir"/*) )

IFS=$OLD_IFS

echo "count: ${#array[@]}"
printf "    %s\n" "${array[@]}"

You do have to save/restore IFS

Craig
  • 756
  • 3
  • 8