1

I have a bash script I am using to iterate over a list of files and search for regex matches. Here is a snippet from my current code:

for file in $Files_To_Parse; do (cat $file) | grep -ioE "($Keys$Delimiters$Payload+$End_String)" | grep -v 'null' | grep -v '*' done

  • $Files_To_Parse is a string of space separated file names

  • $Keys, $Delimiters, $Payload$, and End_String are regex patterns.

This code currently works (slowly).

I would like to either have multiple files processed in parallel, or one file which regex matches are searched for in parallel; however, I am not sure how to use GNU's Parallel package to accomplish this.

Thanks for looking.

1
  • Simplify a lot of it with grep -h pattern "$Files_To_Parse" - that'll at least give grep several files to look at at a time, instead of one at a time.
    – Jeff Schaller
    Commented Jul 6, 2017 at 0:04

1 Answer 1

1

Be aware that your disks I/O might be limiting you:

doit() {
    (cat "$1") | grep -ioE "($Keys$Delimiters$Payload+$End_String)" | grep -v 'null' | grep -v '*'
}
export -f doit
parallel doit ::: $Files_To_Parse

Consider walking through the tutorial. Your command line will love you for it:

man parallel_tutorial
1
  • 1
    Vastly under-rated answer. In the modern era of plentiful cores and plentiful I/O, especially when inputs don't need to be in a specific order, parallel demolishes these workloads. Kudos to you, Ole - you've built something amazing. Commented Dec 5, 2023 at 8:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .