From 7feb65e8f9a00b96d1b6231ed3ac277ab1189350 Mon Sep 17 00:00:00 2001 From: Augusto Gunsch Date: Wed, 27 Oct 2021 20:02:04 -0300 Subject: [PATCH] Add support for multiple files at the sime time --- README.md | 2 +- fsub | 72 ++++++++++++++++++++++++++++++++----------------------- 2 files changed, 43 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index d5f5dc9..7865566 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # fsub -`fsub` is a very simple script (less than 50 lines of code) for cleaning a .srt file +`fsub` is a very simple script (less than 60 lines of code) for cleaning a .srt file # Usage `fsub ` diff --git a/fsub b/fsub index 887de3a..76e25a0 100755 --- a/fsub +++ b/fsub @@ -4,44 +4,56 @@ set -e FSUBRC=~/.config/fsubrc usage() { - echo "usage: fsub " + echo "usage: fsub " echo "fsub expects $FSUBRC to have a blacklist of words" exit 1 } [ -z "$1" ] && usage -[ -f "$1" ] || usage -[ -z "$2" ] || usage + +for arg in "$@"; do + if [ ! -f "$arg" ]; then + echo "$arg is not a file" + usage + fi + if [ "${arg: -4}" != ".srt" ]; then + echo "$arg is not a .srt file" + usage + fi +done + [ -f "$FSUBRC" ] || touch $FSUBRC -awk ' -BEGIN { - n = 1 - i = 0 - while(getline < "'$FSUBRC'") { - blacklist[i] = $0 - i++ +for arg in "$@"; do + awk ' + BEGIN { + n = 1 + i = 0 + while(getline < "'$FSUBRC'") { + blacklist[i] = $0 + i++ + } } -} -/^[[:digit:]]+[[:space:]]*$/ { - getline - time = $0 + /^[[:digit:]]+[[:space:]]*$/ { + getline + time = $0 - linen = 0 - while(getline) { - lines[linen] = $0 - linen++ - if($0 ~ /^[[:space:]]*$/) break - } + linen = 0 + while(getline) { + lines[linen] = $0 + linen++ + if($0 ~ /^[[:space:]]*$/) break + } - for(j = 0; j < i; j++) - for(k = 0; k < linen; k++) - if(lines[k] ~ blacklist[j]) next + for(j = 0; j < i; j++) + for(k = 0; k < linen; k++) + if(lines[k] ~ blacklist[j]) next - print n - n++ - print time - for(j = 0; j < linen; j++) - print lines[j] -}' "$1" | sed 's/\r//' > /tmp/fsub -mv /tmp/fsub "$1" + print n + n++ + print time + for(j = 0; j < linen; j++) + print lines[j] + }' "$arg" | sed 's/\r//' > /tmp/fsub + mv /tmp/fsub "$arg" +done