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!

1
  • I already showed you how to do that in my answer to your previous question. 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. Commented Sep 30, 2018 at 22:22

2 Answers 2

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[@]}"
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.