Skip to content

Instantly share code, notes, and snippets.

@jhyland87
Last active September 2, 2016 00:00
Show Gist options
  • Save jhyland87/305fa40db299f6024f40f0e4d4949875 to your computer and use it in GitHub Desktop.
Save jhyland87/305fa40db299f6024f40f0e4d4949875 to your computer and use it in GitHub Desktop.
DESCRIPTION:
Needed a pattern to regex through the console output for all commands, regardless of user/date/time/result. And I have a stupidly advanced PS1 prompt
PATTERN:
^\(\s*[0-9]+\)\[[a-zA-Z0-9\s:]*\|\s*[0-9]{1,5}\:[0-9]{3}\s*(?:\[[0-9]{1,3}\:[0-9]{1,3}\])?\]\{[0-9]{1,4}\}[a-zA-Z0-9\s:@\/~]+\(?:.+\)[\$|#]\s{1}(?<!\n)(.+)$
LINK: https://regex101.com/r/fH8eA4/1
EXAMPLES:
( 8)[Thu Sep 01 09:44:38| 0:000]{157}jhyland@jhmac:~(✓)$ whoami
( 8)[Thu Sep 01 09:44:42| 0:000]{158}jhyland@jhmac:~(✓)$ uptime
( 8)[Thu Sep 01 09:44:45| 1:000]{159}jhyland@jhmac:~(✓)$ pwd Documents/scripts/
( 8)[Thu Sep 01 09:44:51| 1:000]{160}jhyland@jhmac:~(✓)$ false
( 8)[Thu Sep 01 09:44:54| 0:000]{161}jhyland@jhmac:~(0)$ d
( 8)[Thu Sep 01 09:44:38| 0:000]{157}jhyland@jhmac:~(✓)$ whoami
( 8)[Thu Sep 01 09:44:42| 0:000]{158}jhyland@jhmac:~(✓)$ uptime
( 8)[Thu Sep 01 09:44:45| 1:000]{159}jhyland@jhmac:~(✓)$ pwd Documents/scripts/
( 8)[Thu Sep 01 09:44:51| 1:000]{160}jhyland@jhmac:~(✓)$ false
( 8)[Thu Sep 01 09:44:54| 0:000]{161}jhyland@jhmac:~(0)$
( 7)[Thu Sep 01 09:17:30| 1:000]{1}jhyland@jhmac:~(✓)$ man idk
( 7)[Thu Sep 01 09:17:32| 2:000]{2}jhyland@jhmac:~(0)$ man file
( 7)[Thu Sep 01 09:50:51| 1998:000 [00:33]]{3}jhyland@jhmac:~(✓)$
( 7)[Thu Sep 01 09:54:08| 0:000]{1}jhyland@jhmac:~(✓)$ . ~/.bash_profile
( 7)[Thu Sep 01 09:54:14| 0:000]{2}jhyland@jhmac:~(✓)$ return_code
( 7)[Thu Sep 01 09:54:17| 0:000]{3}jhyland@jhmac:~(0)$ sudo su -
( 7)[Thu Sep 01 10:05:49| 0:000]{1}root@jhmac:~(✓)# whoami
( 7)[Thu Sep 01 10:06:24| 0:000]{2}root@jhmac:~(✓)# cd ~
( 7)[Thu Sep 01 10:06:29| 0:000]{3}root@jhmac:~(✓)# cd /tmp
( 7)[Thu Sep 01 09:54:08| 0:000]{1}jhyland@jhmac:~(✓)$ . ~/.bash_profile
( 7)[Thu Sep 01 09:54:14| 0:000]{2}jhyland@jhmac:~(✓)$ return_code
( 7)[Thu Sep 01 09:54:17| 0:000]{3}jhyland@jhmac:~(0)$ sudo su -
( 7)[Thu Sep 01 10:05:49| 0:000]{1}root@jhmac:~(✓)# whoami
( 7)[Thu Sep 01 10:06:24| 0:000]{2}root@jhmac:~(✓)# cd ~
( 7)[Thu Sep 01 10:06:29| 0:000]{3}root@jhmac:~(✓)# cd /tmp
( 7)[Thu Sep 01 10:06:33| 0:000]{4}root@jhmac:/tmp(✓)# cd ~/.ssh/
( 7)[Thu Sep 01 10:15:35| 0:000]{5}root@jhmac:~/.ssh(✓)# exit
( 7)[Thu Sep 01 10:15:39| 591:000 [00:09]]{4}jhyland@jhmac:~(✓)$ cd Documents/scripts/javascript/
( 7)[Thu Sep 01 10:15:44| 0:000]{5}jhyland@jhmac:~/Documents/scripts/javascript(✓)$ ls
RESULTS:
[
{ "match": 1, "children": [ { "group" : 1, "start": 64, "end": 70, "value": "whoami" } ] },
{ "match": 2, "children": [ { "group" : 1, "start": 135, "end": 141, "value": "uptime" } ] },
{ "match": 3, "children": [ { "group" : 1, "start": 206, "end": 228, "value": "pwd Documents/scripts/" } ] },
{ "match": 4, "children": [ { "group" : 1, "start": 293, "end": 298, "value": "false" } ] },
{ "match": 5, "children": [ { "group" : 1, "start": 363, "end": 364, "value": "d" } ] },
{ "match": 6, "children": [ { "group" : 1, "start": 429, "end": 435, "value": "whoami" } ] },
{ "match": 7, "children": [ { "group" : 1, "start": 500, "end": 506, "value": "uptime" } ] },
{ "match": 8, "children": [ { "group" : 1, "start": 571, "end": 593, "value": "pwd Documents/scripts/" } ] },
{ "match": 9, "children": [ { "group" : 1, "start": 658, "end": 663, "value": "false" } ] },
{ "match": 10, "children": [ { "group" : 1, "start": 790, "end": 797, "value": "man idk" } ] },
{ "match": 11, "children": [ { "group" : 1, "start": 860, "end": 868, "value": "man file" } ] },
{ "match": 12, "children": [ { "group" : 1, "start": 1003, "end": 1020, "value": ". ~/.bash_profile" } ] },
{ "match": 13, "children": [ { "group" : 1, "start": 1083, "end": 1094, "value": "return_code" } ] },
{ "match": 14, "children": [ { "group" : 1, "start": 1157, "end": 1166, "value": "sudo su -" } ] },
{ "match": 15, "children": [ { "group" : 1, "start": 1226, "end": 1232, "value": "whoami" } ] },
{ "match": 16, "children": [ { "group" : 1, "start": 1292, "end": 1296, "value": "cd ~" } ] },
{ "match": 17, "children": [ { "group" : 1, "start": 1356, "end": 1363, "value": "cd /tmp" } ] },
{ "match": 18, "children": [ { "group" : 1, "start": 1426, "end": 1443, "value": ". ~/.bash_profile" } ] },
{ "match": 19, "children": [ { "group" : 1, "start": 1506, "end": 1517, "value": "return_code" } ] },
{ "match": 20, "children": [ { "group" : 1, "start": 1580, "end": 1589, "value": "sudo su -" } ] },
{ "match": 21, "children": [ { "group" : 1, "start": 1649, "end": 1655, "value": "whoami" } ] },
{ "match": 22, "children": [ { "group" : 1, "start": 1715, "end": 1719, "value": "cd ~" } ] },
{ "match": 23, "children": [ { "group" : 1, "start": 1779, "end": 1786, "value": "cd /tmp" } ] },
{ "match": 24, "children": [ { "group" : 1, "start": 1849, "end": 1859, "value": "cd ~/.ssh/" } ] },
{ "match": 25, "children": [ { "group" : 1, "start": 1924, "end": 1928, "value": "exit" } ] },
{ "match": 26, "children": [ { "group" : 1, "start": 2000, "end": 2032, "value": "cd Documents/scripts/javascript/" } ] },
{ "match": 27, "children": [ { "group" : 1, "start": 2124, "end": 2126, "value": "ls" } ]
}
]
DESCRIPTION:
Same thing as above, only it matches for the
PATTERN:
^\(\s*([0-9]+)\)\[([a-zA-Z0-9\s*:]*)\|\s*([0-9]{1,5}\:[0-9]{3})\s*(?:\[([0-9]{1,3})\:[0-9]{1,3}\])?\]\{([0-9]{1,4})\}([a-zA-Z0-9]+)\@([a-zA-Z0-9]+)\:(.+)\((?:.+)\)([\$|#])\s{1}(?<!\n)(.+)$
https://regex101.com/r/pY1aM8/2
EXAMPLES:
# Matches the results also
^\(\s*([0-9]+)\)\[([a-zA-Z0-9\s*:]*)\|\s*([0-9]{1,5}\:[0-9]{3})\s*(?:\[([0-9]{1,3})\:[0-9]{1,3}\])?\]\{([0-9]{1,4})\}([a-zA-Z0-9]+)\@([a-zA-Z0-9]+)\:(.+)\((?:.+)\)([\$|#])\s{1}(.+)(\n(.+))+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment