mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-06-19 00:17:51 +02:00
add wc parser
This commit is contained in:
@ -4,6 +4,7 @@ jc changelog
|
||||
- Add hashsum parser tested on linux, macos
|
||||
- Add hash parser tested on linux, macos
|
||||
- Add cksum parser tested on linux, macos
|
||||
- Add wc parser tested on linux, macos
|
||||
- Add python 3.9 to github automation tests
|
||||
|
||||
20200805 v1.13.4
|
||||
|
26
EXAMPLES.md
26
EXAMPLES.md
@ -2540,6 +2540,32 @@ w | jc --w -p # or: jc -p w
|
||||
}
|
||||
]
|
||||
```
|
||||
### wc
|
||||
```bash
|
||||
wc * | jc --wc -p # or: jc -p wc *
|
||||
```
|
||||
```json
|
||||
[
|
||||
{
|
||||
"filename": "airport-I.json",
|
||||
"lines": 1,
|
||||
"words": 30,
|
||||
"characters": 307
|
||||
},
|
||||
{
|
||||
"filename": "airport-I.out",
|
||||
"lines": 15,
|
||||
"words": 33,
|
||||
"characters": 348
|
||||
},
|
||||
{
|
||||
"filename": "airport-s.json",
|
||||
"lines": 1,
|
||||
"words": 202,
|
||||
"characters": 2152
|
||||
}
|
||||
]
|
||||
```
|
||||
### who
|
||||
```bash
|
||||
who | jc --who -p # or: jc -p who
|
||||
|
@ -175,6 +175,7 @@ The JSON output can be compact (default) or pretty formatted with the `-p` optio
|
||||
- `--uname` enables the `uname -a` command parser
|
||||
- `--uptime` enables the `uptime` command parser
|
||||
- `--w` enables the `w` command parser
|
||||
- `--wc` enables the `wc` command parser
|
||||
- `--who` enables the `who` command parser
|
||||
- `--xml` enables the `XML` file parser
|
||||
- `--yaml` enables the `YAML` file parser
|
||||
|
@ -87,6 +87,7 @@ parsers = [
|
||||
'uname',
|
||||
'uptime',
|
||||
'w',
|
||||
'wc',
|
||||
'who',
|
||||
'xml',
|
||||
'yaml'
|
||||
|
130
jc/parsers/wc.py
Normal file
130
jc/parsers/wc.py
Normal file
@ -0,0 +1,130 @@
|
||||
"""jc - JSON CLI output utility `wc` command output parser
|
||||
|
||||
Usage (cli):
|
||||
|
||||
$ wc file.txt | jc --wc
|
||||
|
||||
or
|
||||
|
||||
$ jc wc file.txt
|
||||
|
||||
Usage (module):
|
||||
|
||||
import jc.parsers.wc
|
||||
result = jc.parsers.wc.parse(wc_command_output)
|
||||
|
||||
Compatibility:
|
||||
|
||||
'linux', 'darwin', 'cygwin', 'aix', 'freebsd'
|
||||
|
||||
Examples:
|
||||
|
||||
$ wc * | jc --wc -p
|
||||
[
|
||||
{
|
||||
"filename": "airport-I.json",
|
||||
"lines": 1,
|
||||
"words": 30,
|
||||
"characters": 307
|
||||
},
|
||||
{
|
||||
"filename": "airport-I.out",
|
||||
"lines": 15,
|
||||
"words": 33,
|
||||
"characters": 348
|
||||
},
|
||||
{
|
||||
"filename": "airport-s.json",
|
||||
"lines": 1,
|
||||
"words": 202,
|
||||
"characters": 2152
|
||||
},
|
||||
...
|
||||
]
|
||||
"""
|
||||
import jc.utils
|
||||
|
||||
|
||||
class info():
|
||||
version = '1.0'
|
||||
description = 'wc command parser'
|
||||
author = 'Kelly Brazil'
|
||||
author_email = 'kellyjonbrazil@gmail.com'
|
||||
details = 'Parses wc and sum program output'
|
||||
|
||||
# compatible options: linux, darwin, cygwin, win32, aix, freebsd
|
||||
compatible = ['linux', 'darwin', 'cygwin', 'aix', 'freebsd']
|
||||
magic_commands = ['wc', 'sum']
|
||||
|
||||
|
||||
__version__ = info.version
|
||||
|
||||
|
||||
def process(proc_data):
|
||||
"""
|
||||
Final processing to conform to the schema.
|
||||
|
||||
Parameters:
|
||||
|
||||
proc_data: (dictionary) raw structured data to process
|
||||
|
||||
Returns:
|
||||
|
||||
List of dictionaries. Structured data with the following schema:
|
||||
|
||||
[
|
||||
{
|
||||
"filename": string,
|
||||
"lines": integer,
|
||||
"words": integer,
|
||||
"characters": integer
|
||||
}
|
||||
]
|
||||
"""
|
||||
|
||||
for entry in proc_data:
|
||||
int_list = ['lines', 'words', 'characters']
|
||||
for key in int_list:
|
||||
if key in entry:
|
||||
try:
|
||||
entry[key] = int(entry[key])
|
||||
except (ValueError):
|
||||
entry[key] = None
|
||||
return proc_data
|
||||
|
||||
|
||||
def parse(data, raw=False, quiet=False):
|
||||
"""
|
||||
Main text parsing function
|
||||
|
||||
Parameters:
|
||||
|
||||
data: (string) text data to parse
|
||||
raw: (boolean) output preprocessed JSON if True
|
||||
quiet: (boolean) suppress warning messages if True
|
||||
|
||||
Returns:
|
||||
|
||||
List of dictionaries. Raw or processed structured data.
|
||||
"""
|
||||
if not quiet:
|
||||
jc.utils.compatibility(__name__, info.compatible)
|
||||
|
||||
raw_output = []
|
||||
|
||||
if jc.utils.has_data(data):
|
||||
|
||||
for line in filter(None, data.splitlines()):
|
||||
split_line = line.split(maxsplit=3)
|
||||
item = {
|
||||
'filename': split_line[3] if len(split_line) == 4 else None,
|
||||
'lines': split_line[0],
|
||||
'words': split_line[1],
|
||||
'characters': split_line[2]
|
||||
}
|
||||
raw_output.append(item)
|
||||
|
||||
if raw:
|
||||
return raw_output
|
||||
else:
|
||||
return process(raw_output)
|
1
tests/fixtures/centos-7.7/wc.json
vendored
Normal file
1
tests/fixtures/centos-7.7/wc.json
vendored
Normal file
File diff suppressed because one or more lines are too long
67
tests/fixtures/centos-7.7/wc.out
vendored
Normal file
67
tests/fixtures/centos-7.7/wc.out
vendored
Normal file
@ -0,0 +1,67 @@
|
||||
0 0 0 bin
|
||||
59 182 1943 cksum.out
|
||||
97735 543757 24150660 devtoolset-3-gcc-4.9.2-6.el7.x86_64.rpm
|
||||
41 173 1196 digout
|
||||
11810 38755 277288 dmidecode.out
|
||||
0 0 0 Downloads
|
||||
24 77 748 ethtool.out
|
||||
0 0 0 file with spaces in the name
|
||||
0 0 0 git
|
||||
1 4 129 id-centos.out
|
||||
1 164 1600 ifcfg.json
|
||||
26 157 1278 ifconfig.out
|
||||
0 0 0 iptables-tests
|
||||
455649 1920906 58764280 jc
|
||||
1 2 69 jc-1.10.5-linux.sha256
|
||||
80475 458331 21978413 jc-1.10.5-linux.tar.gz
|
||||
80635 456923 21716708 jc-1.10.5.rpm
|
||||
1 2 69 jc-1.11.1-linux.sha256
|
||||
74510 421533 20226936 jc-1.11.1-linux.tar.gz
|
||||
1 2 69 jc-1.11.1.sha256
|
||||
73756 421486 20225785 jc-1.11.1.tar.gz
|
||||
1 2 69 jc-1.11.2-linux.sha256
|
||||
73861 421614 20231427 jc-1.11.2-linux.tar.gz
|
||||
1 2 69 jc-1.11.8-linux.sha256
|
||||
74127 421977 20241790 jc-1.11.8-linux.tar.gz
|
||||
1 2 69 jc-1.13.1-linux.sha256
|
||||
74620 421848 20268540 jc-1.13.1-linux.tar.gz
|
||||
1 2 69 jc-1.13.2-linux.sha256
|
||||
74786 423881 20263630 jc-1.13.2-linux.tar.gz
|
||||
1 2 69 jc-1.13.4-linux.sha256
|
||||
74763 423408 20264188 jc-1.13.4-linux.tar.gz
|
||||
1 2 72 jello-1.2.8-linux.sha256
|
||||
72997 415356 19899656 jello-1.2.8-linux.tar.gz
|
||||
1 2 72 jello-1.2.9-linux.sha256
|
||||
73111 414558 19901109 jello-1.2.9-linux.tar.gz
|
||||
6557 479675 6014042 journaljson
|
||||
19009 66861 3426382 jp
|
||||
4154 24072 1174326 jp_1.1.12_linux_x86_64.zip
|
||||
144 55888 738160 jq_twitter.json
|
||||
1 2 71 jtbl-1.1.6-linux.sha256
|
||||
66057 377780 18224334 jtbl-1.1.6-linux.tar.gz
|
||||
58 520 4152 kbls.out
|
||||
5 35 269 lastb.out
|
||||
7 95 1559 lsblk-cols
|
||||
25 207 1751 ping-ip-O-D.out
|
||||
25 187 1351 ping-ip-O.out
|
||||
104 912 7786 psfile.txt
|
||||
16 53 355 resizeterm.sh
|
||||
23 159 1802 route-6-n.out
|
||||
23 159 1814 route-6.out
|
||||
41 74 690 routeout
|
||||
56 117 6434 sha384sum.out
|
||||
56 117 2850 shafile.txt
|
||||
205 2004 26724 ss-aeep.out
|
||||
205 1843 20190 ssout
|
||||
58 179 1668 sum.out
|
||||
229 1609 38746 systemctl.out
|
||||
0 0 0 testfiles
|
||||
0 0 0 tmp
|
||||
34 457 5314 top.out
|
||||
6 25 252 tracepath6.out
|
||||
34 105 939 tracepath-cnn.out
|
||||
34 113 1137 tr.out
|
||||
6 36 355 who-aH.out
|
||||
2 9 93 who.out
|
||||
5 28 281 whotext
|
||||
1490176 8218431 338121827 total
|
1
tests/fixtures/osx-10.14.6/wc.json
vendored
Normal file
1
tests/fixtures/osx-10.14.6/wc.json
vendored
Normal file
File diff suppressed because one or more lines are too long
150
tests/fixtures/osx-10.14.6/wc.out
vendored
Normal file
150
tests/fixtures/osx-10.14.6/wc.out
vendored
Normal file
@ -0,0 +1,150 @@
|
||||
1 30 307 airport-I.json
|
||||
15 33 348 airport-I.out
|
||||
1 202 2152 airport-s.json
|
||||
15 112 1423 airport-s.out
|
||||
98 170 2140 arp-a.json
|
||||
12 99 968 arp-a.out
|
||||
1 240 2979 arp-a2.json
|
||||
20 162 1597 arp-a2.out
|
||||
1 870 10244 cksum.json
|
||||
145 435 4298 cksum.out
|
||||
1 147 1686 df-h.json
|
||||
9 85 1276 df-h.out
|
||||
1 147 1764 df.json
|
||||
9 85 1384 df.out
|
||||
1 49 438 dig-aaaa.json
|
||||
20 80 536 dig-aaaa.out
|
||||
1 585 5984 dig-axfr.json
|
||||
59 382 3511 dig-axfr.out
|
||||
1 49 442 dig-x.json
|
||||
20 80 529 dig-x.out
|
||||
1 138 1266 dig.json
|
||||
41 169 1182 dig.out
|
||||
1 13448 295780 du.json
|
||||
3357 6734 221925 du.out
|
||||
1 370 3589 file.json
|
||||
53 264 2890 file.out
|
||||
1 458 4715 file2.json
|
||||
88 282 4780 file2.out
|
||||
1 1015 9646 group.json
|
||||
135 174 2823 group.out
|
||||
1 79 759 id.json
|
||||
1 3 386 id.out
|
||||
1 1107 10823 ifconfig.json
|
||||
91 331 3779 ifconfig.out
|
||||
1 1162 11375 ifconfig2.json
|
||||
95 349 3979 ifconfig2.out
|
||||
1 5483 45274 last.json
|
||||
383 3261 25827 last.out
|
||||
1 88 852 ls-R-newlines.json
|
||||
36 43 278 ls-R-newlines.out
|
||||
1 18354 469418 ls-R.json
|
||||
5017 4805 133787 ls-R.out
|
||||
1 550 4653 ls-al.json
|
||||
35 314 2111 ls-al.out
|
||||
1 82256 959674 ls-alR.json
|
||||
4996 41842 364578 ls-alR.out
|
||||
1 550 4684 ls-alh.json
|
||||
35 314 2111 ls-alh.out
|
||||
1 7272 91544 ls-glob.json
|
||||
1831 1825 19369 ls-glob.out
|
||||
1 138 1162 ls-l-newlines.json
|
||||
34 99 583 ls-l-newlines.out
|
||||
1 34164 355547 ls-lR-empty-folder.json
|
||||
2453 17640 128740 ls-lR-empty-folder.out
|
||||
1 168 1450 ls-lR-newlines.json
|
||||
38 111 656 ls-lR-newlines.out
|
||||
1 56 498 ls-newlines.json
|
||||
33 41 260 ls-newlines.out
|
||||
1 46 585 ls.json
|
||||
23 23 193 ls.out
|
||||
1 416 7764 md5.json
|
||||
104 416 5683 md5.out
|
||||
1 55 672 mount.json
|
||||
6 43 349 mount.out
|
||||
1 66 841 mount2.json
|
||||
7 52 464 mount2.out
|
||||
1 15133 151042 netstat-Abn.json
|
||||
689 5961 76835 netstat-Abn.out
|
||||
1 14085 142438 netstat-An.json
|
||||
689 5431 71005 netstat-An.out
|
||||
1 840 7246 netstat-i.json
|
||||
43 376 3354 netstat-i.out
|
||||
1 1456 16929 netstat-r.json
|
||||
93 435 6178 netstat-r.out
|
||||
1 1940 21015 netstat-rnl.json
|
||||
96 667 10055 netstat-rnl.out
|
||||
1 14100 141195 netstat.json
|
||||
737 5316 70387 netstat.out
|
||||
1 1517 14855 passwd.json
|
||||
108 292 6804 passwd.out
|
||||
1 67 714 ping-hostname-p.json
|
||||
9 51 400 ping-hostname-p.out
|
||||
1 67 714 ping-hostname-s.json
|
||||
8 49 389 ping-hostname-s.out
|
||||
1 67 706 ping-hostname.json
|
||||
8 49 381 ping-hostname.out
|
||||
1 235 2251 ping-ip-dup.json
|
||||
20 147 1156 ping-ip-dup.out
|
||||
1 67 691 ping-ip-p.json
|
||||
9 51 381 ping-ip-p.out
|
||||
1 67 699 ping-ip-s.json
|
||||
8 49 375 ping-ip-s.out
|
||||
1 67 689 ping-ip.json
|
||||
8 49 367 ping-ip.out
|
||||
1 69 786 ping6-hostname-p.json
|
||||
9 50 460 ping6-hostname-p.out
|
||||
1 69 788 ping6-hostname-s.json
|
||||
8 48 449 ping6-hostname-s.out
|
||||
1 69 784 ping6-hostname.json
|
||||
8 48 446 ping6-hostname.out
|
||||
1 909 9447 ping6-ip-dup.json
|
||||
68 530 5225 ping6-ip-dup.out
|
||||
1 69 683 ping6-ip-p.json
|
||||
9 50 355 ping6-ip-p.out
|
||||
1 69 688 ping6-ip-s.json
|
||||
8 48 352 ping6-ip-s.out
|
||||
1 69 681 ping6-ip.json
|
||||
8 48 341 ping6-ip.out
|
||||
1 20 224 pip-list.json
|
||||
7 14 168 pip-list.out
|
||||
1 84 1067 pip-show.json
|
||||
32 82 910 pip-show.out
|
||||
1 8641 99173 ps-axu.json
|
||||
378 4505 60107 ps-axu.out
|
||||
1 6379 71162 ps-ef.json
|
||||
378 3371 48758 ps-ef.out
|
||||
1 568 11817 shasum.json
|
||||
142 284 7982 shasum.out
|
||||
1 4136 35152 stat.json
|
||||
94 2632 15599 stat.out
|
||||
1 876 9234 sum.json
|
||||
146 438 3247 sum.out
|
||||
1 2793 46473 sysctl-a.json
|
||||
1287 2785 42505 sysctl-a.out
|
||||
1 108 1123 traceroute-asn.json
|
||||
5 44 323 traceroute-asn.out
|
||||
1 75 807 traceroute-mult-addresses.json
|
||||
4 39 305 traceroute-mult-addresses.out
|
||||
1 75 793 traceroute-no-header.json
|
||||
3 22 166 traceroute-no-header.out
|
||||
1 115 1240 traceroute-q.json
|
||||
4 42 272 traceroute-q.out
|
||||
1 244 2419 traceroute.json
|
||||
11 81 579 traceroute.out
|
||||
1 83 859 traceroute6-mult-addresses.json
|
||||
8 48 401 traceroute6-mult-addresses.out
|
||||
1 83 859 traceroute6.json
|
||||
7 40 304 traceroute6.out
|
||||
1 20 221 uname-a.json
|
||||
1 15 131 uname-a.out
|
||||
1 1 7 uname.out
|
||||
1 14 110 uptime.json
|
||||
1 12 65 uptime.out
|
||||
1 99 836 w.json
|
||||
10 69 570 w.out
|
||||
1 86 659 who-a.json
|
||||
9 62 419 who-a.out
|
||||
1 32 251 who.json
|
||||
4 20 128 who.out
|
||||
24562 360529 4491471 total
|
Reference in New Issue
Block a user