From 25e6d7ae248aa66757f986cf1d44e755162673f1 Mon Sep 17 00:00:00 2001 From: Ingo Karkat Date: Mon, 23 Jan 2012 11:05:12 +0100 Subject: [PATCH] FIX: Adapt deduplicate sed command for OS X. The sed command of BSD / OS X doesn't like { command } blocks in a single expression. So move the (dynamic) deduplicate sed command(s) to the end, and use a label to access them. This also makes the entire sed script somewhat easier to understand. --- tests/t1910-deduplicate.sh | 6 +++--- todo.sh | 26 +++++++++++++++++--------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/tests/t1910-deduplicate.sh b/tests/t1910-deduplicate.sh index 567c235..be225e8 100755 --- a/tests/t1910-deduplicate.sh +++ b/tests/t1910-deduplicate.sh @@ -82,9 +82,9 @@ EOF cat > todo.txt <>> todo.sh -p ls -2 a bold action +2 a bold task 1 normal task 3 something else 5 something more diff --git a/todo.sh b/todo.sh index 10b1246..2c03377 100755 --- a/todo.sh +++ b/todo.sh @@ -1240,7 +1240,7 @@ note: PRIORITY must be anywhere from A to Z." if [ $TODOTXT_PRESERVE_LINE_NUMBERS = 0 ]; then deduplicateSedCommand='d' else - deduplicateSedCommand='{ s/^.*//; p; b }' + deduplicateSedCommand='s/^.*//; p' fi # To determine the difference when deduplicated lines are preserved, only @@ -1251,17 +1251,25 @@ note: PRIORITY must be anywhere from A to Z." # We start with an empty hold space on the first line. For each line: # G - appends newline + hold space to the pattern space # s/\n/&&/; - double up the first new line so we catch adjacent dups - # /^\([^\n]*\n\).*\n\1/ + # /^\([^\n]*\n\).*\n\1/b dedup # If the first line of the hold space shows up again later as an - # entire line, it's a duplicate. - # d; - Delete the current pattern space, quit this line - # and move on to the next, or: - # { s/^.*//; p; b }; - Clear the task text, print this line and move on - # to the next. - # s/\n//; - else, drop the doubled newline + # entire line, it's a duplicate. Jump to the "dedup" label, where + # either of the following is executed, depending on whether empty + # lines should be preserved: + # d - Delete the current pattern space, quit this line and + # move on to the next, or: + # s/^.*//; p - Clear the task text, print this line and move on to + # the next. + # s/\n//; - else (no duplicate), drop the doubled newline # h; - replace the hold space with the expanded pattern space # P; - print up to the first newline (that is, the input line) - sed -i.bak -n 'G; s/\n/&&/; /^\([^\n]*\n\).*\n\1/'"$deduplicateSedCommand"'; s/\n//; h; P' "$TODO_FILE" + # b - end processing of the current line + sed -i.bak -n \ + -e 'G; s/\n/&&/; /^\([^\n]*\n\).*\n\1/b dedup' \ + -e 's/\n//; h; P; b' \ + -e ':dedup' \ + -e "$deduplicateSedCommand" \ + "$TODO_FILE" newTaskNum=$( sed -e '/./!d' "$TODO_FILE" | sed -n '$ =' ) deduplicateNum=$(( originalTaskNum - newTaskNum ))