From e4951db8b4ce100ea216c4d5569830550925c52c Mon Sep 17 00:00:00 2001 From: Thomas Lange Date: Fri, 8 Mar 2019 16:54:04 +0100 Subject: Add script "weechatlog" --- Bash/weechatlog | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100755 Bash/weechatlog (limited to 'Bash') diff --git a/Bash/weechatlog b/Bash/weechatlog new file mode 100755 index 0000000..a1d7f04 --- /dev/null +++ b/Bash/weechatlog @@ -0,0 +1,55 @@ +#!/bin/bash +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%# +# Weechat log file converter [Thomas Lange ] # +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%# +# # +# This script can convert WeeChat log files to another format like markdown or # +# better readable plaintext. The script assumes that the WeeChat log files are # +# given in the following format: 2019-01-10 22:52:30\t\t # +# # +# OPTION [-f]: Output format ("md" or "txt"; default is "txt") # +# # +# USAGE: # +# weechatlog {-f txt|md} username.log > converted.txt # +# cat username.log | weechatlog {-f txt|md} > converted.txt # +# # +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%# + +#=============================================================================== +# Parse command-line arguments with getopts +#=============================================================================== +ARGUMENT_FORMAT="txt" # default output format + +while getopts :f: option +do + case $option in + f) ARGUMENT_FORMAT="$OPTARG" ;; + esac +done; shift $((OPTIND-1)) + +#=============================================================================== +# Pattern part +#=============================================================================== +PATTERN_DATE="\([0-9]*\)-\([0-9]*\)-\([0-9]*\)" # References: 1, 2, 3 +PATTERN_TIME="\([0-9]*\):\([0-9]*\):\([0-9]*\)" # References: 4, 5, 6 + +PATTERN="${PATTERN_DATE} ${PATTERN_TIME}\t\([^\s]*\)\t\(.*\)" + +#=============================================================================== +# Replace part +#=============================================================================== +case $ARGUMENT_FORMAT in + txt) REPLACE='\7 am \3.\2.\1 um \4:\5:\6:\n\8\n' ;; + md) REPLACE='**\7 am \3.\2.\1 um \4:\5:\6:**\n

\8

\n' ;; + + *) echo "Unknown format (-f): $ARGUMENT_FORMAT" && exit 1 +esac + +#=============================================================================== +# Execute sed +#=============================================================================== +if [ -z "$1" ]; then + sed "s#$PATTERN#$REPLACE#" # read input from stdin +else + sed "s#$PATTERN#$REPLACE#" "$1" +fi -- cgit v1.2.3