jtxt
v0.1.7
Published
A CLI tool like awk but using js grammar instead
Readme
$ jtxt 'var it = l.split("]")[2]; console.log(it)' gc.logQuick Start
Download from the release, run the binary executable file or node jtxt.js
从release中下载二进制文件直接运行即可,或者使用node jtxt.js。
Usage
$ jtxt -h
Processes lines of input with JavaScript
Usage: jtxt [OPTIONS] <function> [file]
Arguments:
<function> JavaScript code to process each line, l is the origin string
[file] Path to the input file. If not provided, reads from stdin.
Options:
-b, --begin <begin> JavaScript code to execute before processing any lines
-e, --end <end> JavaScript code to execute after processing all lines
-h, --help Print help
-V, --version Print versionExample with 1.txt
# 1 filter the lines that only contains numbers from 1.txt
# 1 从1.txt中过滤全是数字的行
$ jtxt 'if (l.match(/^\d+$/)) console.log(l)' 1.txt
200
404
200
# 2 compute the character number of 1.txt (exclude the [\r]\n)
# 2 计算1.txt中的字符个数(除了换行符[\r]\n)
$ jtxt -b 'ctx.s=0' 'ctx.s += l.length' -e 'console.log(ctx.s)' 1.txt
168
# 3 filter the lines that contains `Request`, and process the time text, then analyze the distribution of quantity per hour
# 3 过滤含有Request的行,并将时间部分进行处理,分析每个小时的请求数量分布
$ jtxt 'if(l.indexOf("Request")>=0) console.log(l)' 1.txt \
| jtxt -b 'ctx.m={}' 'var k = "h" + new Date(l.substring(0,19)).getHours(); if(!ctx.m[k]) ctx.m[k]=0; ctx.m[k]++' -e 'console.log(ctx.m)' 1.txt
{ h13: 2, h0: 3, h14: 1, hNaN: 1 }Well, to simplify the command, you can also use the preset variable: var ctx = { n1:0, n2:0, n3:0, s:'', arr:[]}
你也可以用内置的变量ctx来简化指令,var ctx = { n1:0, n2:0, n3:0, s:'', arr:[]}
Performance
Count the word count of a 10G random log file (generated by file_gen.js). Compared with awk, there is almost no difference.
对10G的随机日志文件进行字数统计(由file_gen.js产生),对比awk几乎没有差距,多次运行各有胜负:

