I am trying to search a particular pattern “JOB” in all the files in the mentioned directories, and the checking if the next word after JOB has atleast 5 characters and if the 5th character is “H” or “I” in the next word, and then trying to print the file name, pattern and the next word in a file. but it is only printing the directory name in the output file.
#!/bin/bash
directories=("dir1" "dir2" "dir3" "dir4")
output_file="output.txt"
> "$output_file"
for dir in "${directories[@]}"; do
echo "Searching in directory: $dir"
while IFS= read -r -d '' file; do
while read -r word; do
if [[ ${#word} -ge 5 ]]; then
fifth_char="${word:4:1}"
if [[ $fifth_char == 'H' || $fifth_char == 'I' ]]; then
echo "$(basename "$file") JOB $word"
fi
fi
done < <(grep -oP 'JOB Kw+' "$file")
done < <(find "$dir" -type f -print)
done > "$output_file"
echo "Search complete. Results are in $output_file."