Skip to content

Instantly share code, notes, and snippets.

@fernandovmacedo
Last active April 25, 2024 20:53
Show Gist options
  • Save fernandovmacedo/9d9c6b8928bc6b7929446127c2ee07a0 to your computer and use it in GitHub Desktop.
Save fernandovmacedo/9d9c6b8928bc6b7929446127c2ee07a0 to your computer and use it in GitHub Desktop.
Convert gpt-cli logs to markdown
#!/usr/bin/env bash
help="Convert chat-gpt cli logs to markdown.
No args. Use STDIN and STDOUT
Requires ripgrep
Usage example using bat for formatting:
$0 < logfile | bat -l md -p"
main() {
# capture the first 4 groups of the log file
local base_regex='^(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2},\d{3}) - (\S+) - (\w+) - '
rg "$base_regex(Starting.*)$" -r '# New chat $1 $2' --passthru | # Create first header for new chats
rg "$base_regex(user:)(.*)$" -r $'\n## User: $6' --passthru | # Create second header for user input
rg "$base_regex(assistant: )(.*)$" -r $'\n$6' --passthru | # Clean log metadata from first line of answers
rg "$base_regex" --invert-match # delete log lines remaining. They contain only metadata
}
# From best practices template
# https://sharats.me/posts/shell-script-best-practices/
set -o errexit
set -o nounset
set -o pipefail
if [[ "${TRACE-0}" == "1" ]]; then
set -o xtrace
fi
if [[ "${1-}" =~ ^-{0,2}h(elp)?$ ]]; then
echo "$help"
exit
fi
cd "$(dirname "$0")"
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment