summaryrefslogtreecommitdiffstats
path: root/Bash/weechatlog
blob: a1d7f04a2a88d76ced8bfa6388d73f34753e54aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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