diff options
author | Thomas Lange <code@nerdmind.de> | 2019-03-08 16:54:04 +0100 |
---|---|---|
committer | Thomas Lange <code@nerdmind.de> | 2019-03-08 16:54:04 +0100 |
commit | e4951db8b4ce100ea216c4d5569830550925c52c (patch) | |
tree | ee0c27b1d6a4b85a614886985ebc3bf8e5a5a811 /Bash/weechatlog | |
parent | d74453055ea120622418a6cd853f8ee00619f839 (diff) | |
download | snippets-e4951db8b4ce100ea216c4d5569830550925c52c.tar.gz snippets-e4951db8b4ce100ea216c4d5569830550925c52c.tar.xz snippets-e4951db8b4ce100ea216c4d5569830550925c52c.zip |
Add script "weechatlog"
Diffstat (limited to 'Bash/weechatlog')
-rwxr-xr-x | Bash/weechatlog | 55 |
1 files changed, 55 insertions, 0 deletions
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 <code@nerdmind.de>] # +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%# +# # +# 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<username>\t<message> # +# # +# 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='**<u>\7</u> <small>am \3.\2.\1 um \4:\5:\6:</small>**\n<p>\8</p>\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 |