[SOLVED] Parse a Log File to Display on Conky

Add functionality to your desktop
Forum rules
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
Jator
Level 2
Level 2
Posts: 80
Joined: Sat Mar 13, 2021 10:58 am

[SOLVED] Parse a Log File to Display on Conky

Post by Jator »

Forgive me, I'm still learning. I'd like to extract the following segment:

Code: Select all

2021-06-26 16:00:23,rc5-72 r=19998/20000, d=12/3, 24052.0 Mkeys[/u]/sec, tot=3453137/3453084
from the following text in a log file:

Code: Select all

2021-06-26 15:59:21,ogrng r=20/20, d=0/3, 0.0 Mnodes/sec, tot=0/0 stubs
2021-06-26 15:59:21,Status: Uptime: 7.04:37:23, 2 listeners, 0 uplinks
2021-06-26 15:59:21,Status: 0 active clients (peak: 2, mean: 0.00)
2021-06-26 15:59:52,rc5-72 r=19998/20000, d=12/3, 24060.6 Mkeys/sec, tot=3453137/3453084
2021-06-26 15:59:52,ogrng r=20/20, d=0/3, 0.0 Mnodes/sec, tot=0/0 stubs
2021-06-26 15:59:52,Status: Uptime: 7.04:37:54, 2 listeners, 0 uplinks
2021-06-26 15:59:52,Status: 0 active clients (peak: 2, mean: 0.00)
2021-06-26 16:00:23,rc5-72 r=19998/20000, d=12/3, 24052.0 Mkeys/sec, tot=3453137/3453084
2021-06-26 16:00:23,ogrng r=20/20, d=0/3, 0.0 Mnodes/sec, tot=0/0 stubs
2021-06-26 16:00:23,Status: Uptime: 7.04:38:25, 2 listeners, 0 uplinks
2021-06-26 16:00:23,Status: 0 active clients (peak: 2, mean: 0.00)
and display the following 3 values:

Code: Select all

19998
12
24052.0 Mkeys
in various lines in my Conky scripts. I've not yet figured out how to extract values other than the "cut -c x-y" which doesn't help me with variable length characters such as what is shown above. Any pointers on how to extract the info? I can limit the output to only pull the last record in the log file with the criteria using 'tail -1' so I don't need to worry about any lines that meet the criteria before the last line.

TIA
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
xenopeek
Level 25
Level 25
Posts: 29607
Joined: Wed Jul 06, 2011 3:58 am

Re: Parse a Log File to Display on Conky

Post by xenopeek »

What is a unique part of the line you are interested in? Is it the "rc5-72" string? Then you can use grep to only get lines that have that on it:
grep rc5-72 logfile
sed can then be used to split that line in the parts you want:
grep rc5-72 logfile | tail -1 | sed -r 's|.*rc5-72 r=([0-9]*)/[0-9]*, d=([0-9]*)/[0-9]*, ([^,]*),.*|\1\n\2\n\3|'
If I run that on your example logfile it returns:

Code: Select all

19998
12
24052.0 Mkeys/sec
The sed command uses regular expressions with grouping to do what you want. In short the command:
  • sed -r 's|: the -r option says to use extended regular expression syntax which is a bit easier to read. The s at the start makes this a substitute command. It will try to match the next part and then replace the matched part with the third part. Parts are split by the | pipe character.
  • .*rc5-72 r=([0-9]*)/[0-9]*, d=([0-9]*)/[0-9]*, ([^,]*),.*: If you don't use regular expression often this will be harder to follow. The .* says "match anything of any length" so this matches from the start of the line until it encounters the rc5-72 r= string. Then next it groups with () brackets the digits that follow. The [0-9]* means any length of characters from the character set range 0-9, so digits. And so on it groups with () brackets the parts you want to keep. The last bit ([^,]*),.* is to capture the 24052.0 Mkeys part. On the line in your logfile that is followed by a comma. So [^,]* matches any character except (the caret inverts the character set) the comma.
  • |\1\n\2\n\3|': this is what to replace what was matched with (and because we had .* at the beginning and ending of the match it replaces the entire line). The \1 is a back reference to the first matched group, the part enclosed in () brackets, and so on for \2 and \3. \n is newline.
Image
Jator
Level 2
Level 2
Posts: 80
Joined: Sat Mar 13, 2021 10:58 am

Re: Parse a Log File to Display on Conky

Post by Jator »

Much appreciated xenopeek. I'll have time tonight to play with it and figure it out (I'm a tactile learner, having examples I can reverse engineer is my lane).
Locked

Return to “Compiz, Conky, Docks & Widgets”