Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix clipdel cutting timestamps from file cache #94

Merged
merged 1 commit into from
Apr 28, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions clipdel
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ fi
raw_pattern=$1
esc_pattern=${raw_pattern//\#/'\#'}

# We use 2 separate sed commands so "esc_pattern" matches only the 'clip' text
# without the timestamp (e.g. $> clipdel '^delete_exact_match$')
sed_common_command="s#^[0-9]\+ ##;\\#${esc_pattern}#"

if ! [[ $raw_pattern ]]; then
printf '%s\n' 'No pattern provided, see --help' >&2
exit 2
Expand All @@ -60,8 +64,8 @@ if (( CM_REAL_DELETE )) && [[ "$raw_pattern" == ".*" ]]; then
exit 0
else
mapfile -t matches < <(
cat "${line_cache_files[@]}" | cut -d' ' -f2- | sort -u |
sed -n "\\#${esc_pattern}#p"
sed -n "${sed_common_command}p" "${line_cache_files[@]}" |
sort -u
)

if (( CM_REAL_DELETE )); then
Expand All @@ -74,7 +78,11 @@ else

for file in "${line_cache_files[@]}"; do
temp=$(mktemp)
cut -d' ' -f2- < "$file" | sed "\\#${esc_pattern}#d" > "$temp"
# sed 'h' and 'g' here means save and restore the line, so
# timestamps are not removed from non-deleted lines. 'd' deletes the
# line and restarts, skipping 'g'/restore.
# https://www.gnu.org/software/sed/manual/html_node/Other-Commands.html#Other-Commands
sed "h;${sed_common_command}d;g" "$file" > "$temp"
mv -- "$temp" "$file"
done

Expand Down