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.
This commit is contained in:
@@ -82,9 +82,9 @@ EOF
|
|||||||
|
|
||||||
cat > todo.txt <<EOF
|
cat > todo.txt <<EOF
|
||||||
normal task
|
normal task
|
||||||
a [1mbold[0m action
|
a [1mbold[0m task
|
||||||
something else
|
something else
|
||||||
a [1mbold[0m action
|
a [1mbold[0m task
|
||||||
something more
|
something more
|
||||||
EOF
|
EOF
|
||||||
test_todo_session 'deduplicate with non-printable duplicates' <<EOF
|
test_todo_session 'deduplicate with non-printable duplicates' <<EOF
|
||||||
@@ -92,7 +92,7 @@ test_todo_session 'deduplicate with non-printable duplicates' <<EOF
|
|||||||
TODO: 1 duplicate task(s) removed
|
TODO: 1 duplicate task(s) removed
|
||||||
|
|
||||||
>>> todo.sh -p ls
|
>>> todo.sh -p ls
|
||||||
2 a [1mbold[0m action
|
2 a [1mbold[0m task
|
||||||
1 normal task
|
1 normal task
|
||||||
3 something else
|
3 something else
|
||||||
5 something more
|
5 something more
|
||||||
|
|||||||
26
todo.sh
26
todo.sh
@@ -1240,7 +1240,7 @@ note: PRIORITY must be anywhere from A to Z."
|
|||||||
if [ $TODOTXT_PRESERVE_LINE_NUMBERS = 0 ]; then
|
if [ $TODOTXT_PRESERVE_LINE_NUMBERS = 0 ]; then
|
||||||
deduplicateSedCommand='d'
|
deduplicateSedCommand='d'
|
||||||
else
|
else
|
||||||
deduplicateSedCommand='{ s/^.*//; p; b }'
|
deduplicateSedCommand='s/^.*//; p'
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# To determine the difference when deduplicated lines are preserved, only
|
# 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:
|
# We start with an empty hold space on the first line. For each line:
|
||||||
# G - appends newline + hold space to the pattern space
|
# G - appends newline + hold space to the pattern space
|
||||||
# s/\n/&&/; - double up the first new line so we catch adjacent dups
|
# 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
|
# If the first line of the hold space shows up again later as an
|
||||||
# entire line, it's a duplicate.
|
# entire line, it's a duplicate. Jump to the "dedup" label, where
|
||||||
# d; - Delete the current pattern space, quit this line
|
# either of the following is executed, depending on whether empty
|
||||||
# and move on to the next, or:
|
# lines should be preserved:
|
||||||
# { s/^.*//; p; b }; - Clear the task text, print this line and move on
|
# d - Delete the current pattern space, quit this line and
|
||||||
# to the next.
|
# move on to the next, or:
|
||||||
# s/\n//; - else, drop the doubled newline
|
# 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
|
# h; - replace the hold space with the expanded pattern space
|
||||||
# P; - print up to the first newline (that is, the input line)
|
# 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 '$ =' )
|
newTaskNum=$( sed -e '/./!d' "$TODO_FILE" | sed -n '$ =' )
|
||||||
deduplicateNum=$(( originalTaskNum - newTaskNum ))
|
deduplicateNum=$(( originalTaskNum - newTaskNum ))
|
||||||
|
|||||||
Reference in New Issue
Block a user