#!/bin/bash # parallel.sh - bash script for filtering/parallelising using sed. # (C) 2023 Felix Hauri - felix@f-hauri.ch # Licensed under terms of GPL v3. www.gnu.org prog=${0##*/} usage() { cat <<-EOUsage Usage: $prog -t [-b ] [-a ] command args -h show this -t coma separated liste of tags to send to separated tasks or single tag, '-t' option could be submited multiple times -b sed regex to match before tags -a sed regex to match after tags command Any command to be run once for each tag. Special string "" will be replaced by current tag. EOUsage } die() { echo >&2 "ERROR $prog: $*" exit 1 } while getopts "ht:a:b:" opt; do case $opt in h ) usage; exit ;; t ) IFS=, read -a crttags <<<"$OPTARG" tags+=("$crttags");; b ) before=$OPTARG ;; a ) after=$OPTARG ;; *) die Wrong argument. ;; esac done shift $((OPTIND-1)) [[ -v tags ]] || die "No tags submited" (( $# )) || die "No command submited" sedcmd='' paren='' declare -i crtFd=4 for re in "${tags[@]}";do printf -v crtcmd '%q ' "${@//\/$re}" printf -v crtcmd ' -e \47%s/%s/{s/%s//;w/dev/fd/%d\47 %d> >(exec %s) ' \ "$paren" "$before$re$after"{,} $crtFd $crtFd "$crtcmd" paren='};' sedcmd+="$crtcmd" crtFd+=1 done sedcmd+=" -e '$paren'" eval sed -n "$sedcmd"