Skip to content

Instantly share code, notes, and snippets.

@jeansymolanza
Created December 5, 2023 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeansymolanza/f151a5fd050618bd9af238b5e36ea6dc to your computer and use it in GitHub Desktop.
Save jeansymolanza/f151a5fd050618bd9af238b5e36ea6dc to your computer and use it in GitHub Desktop.
#!/bin/bash
num_threads=<num_threads>
pattern_file="pattern_list.txt"
output_file="output.txt"
directory_path="directory_path"
# Read each pattern from the pattern file
while IFS= read -r pattern; do
# Run grep in the background for each pattern
grep -r -n "$pattern" "$directory_path" >> "$output_file" &
# Limit the number of concurrent background processes
if [[ $(jobs -p | wc -l) -ge $num_threads ]]; then
wait -n
fi
done < "$pattern_file"
# Wait for any remaining background processes to finish
wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment