Skip to content

sunwu51/jtxt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jtxt

A JavaScript syntax text processing tool, an awk alternative.

使用JavaScript语法进行文本处理的工具,来替换awk

Rust CI

$ jtxt 'var it = l.split("]")[2]; console.log(it)' gc.log

Quick Start

Download from the release, run the binary executable file or npm i -g jtxt

从release中下载二进制文件直接运行即可,或者使用npm i -g jtxt

Usage

注意logic/end/begin部分,在windows下需要用"而不能用'

$ jtxt -h
Usage: jtxt [options] <logic> [filename]

Arguments:
  logic               处理逻辑的 JavaScript 代码
  filename            要读取的文件名 (default: null)

Options:
  -V, --version       output the version number
  -b, --begin <code>  初始化的逻辑代码
  -e, --end <code>    结束后的逻辑代码
  -h, --help          display help for command

Example 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几乎没有差距,多次运行各有胜负:

image

About

A JavaScript syntax text processing tool, an awk alternative.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors