Reputation: 151
In GC logs, each line is having timestamp at the start. but sometimes we are missing that.
I am looking to write a bash script which check if Timestamp is missing it will add some static timestamp at the beginning.
In below example there are 2 lines. 1st line is having timestamp but 2nd line doesn't.
2018-11-22T11:58:39.381+0100: 79412.217: [GC (Allocation Failure) 2018-11-22T11:58:39.381+0100: 79412.217: [ParNew: 1265865K->36835K(1380160K), 0.1419160 secs] 23560553K->22332825K(29206784K), 0.1421816 secs] [Times: user=2.39 sys=0.00, real=0.14 secs]
79412.947: [GC (Allocation Failure) 2018-11-22T11:58:40.112+0100: 79412.947: [ParNew: 1263651K->36868K(1380160K), 0.1502318 secs] 23559641K->22334516K(29206784K), 0.1504887 secs] [Times: user=2.47 sys=0.00, real=0.15 secs]
For example In 2nd line: 79412.947 + 153434343 = some value. Convert this value is Timestamp format and append at the beginning.
Regex to extract timestamp. https://regex101.com/r/3CtU4y/1/
Any suggestion, helpful advice is appreciated. Thank you in advance.
Upvotes: 0
Views: 119
Reputation: 4969
The first suggestion is that bash is probably not your best choice if you have a lot of logging.
That aside, writing scripts is very much doing exactly what is asked. So it will be:
1) set up a loop to read the input lines
#!/bin/bash
cat $* | while read line ; do
2) test if the input-line starts with a timestamp (did not bother looking up the regex you supplied.
if echo "$line " | grep '^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]T[0-9][0-9]:[0-9][0-9]' ; then
true
Note that, if the condition is true, grep
will output the line, so no separate echo is needed for that.
3) Otherwise, do some things with the epoch timestamp that I can't quite understand what you want:
else
estamp=$(echo "$line" | sed 's/:.*//')
rol=$(echo "$line" | sed 's/[0-9.]*://)
# Do something with adding to the timestamp that I did not grasp
echo "$estamp:$rol"
fi
Or something like that.
O yeah, close the loop
done
Upvotes: 1