跳转到主要内容

awkg是Python语言驱动的类似于awk的文本处理工具

项目描述

awkg

awkg 是一个类似于 awk 的实用工具,使用现代Python语言。 awk 简单、快速且非常实用。然而,它的特定领域约束有时会阻碍我们。 awkg 遵循 awk 的设计步骤(包括其命名约定😉)并公开了现代Python的全部功能。当然可以导入和使用Python的大量现成库。

安装

# Install from pypy 
$ pip install awkg

# Install from github
$ pip install git+https://github.com/thammegowda/awkg.git

命令行界面使用

$ awkg -h 
usage: awkg [-h] [-i INP] [-o OUT] [-F FS] [-OFS OFS] [-ORS ORS]
            [-b BEGIN_SCRIPT] [-e END_SCRIPT] [-im IMPORTS] [-it INIT_PATH]
            [-v]
            inline_script

awkg is an awk-like text-processing tool powered by python language

positional arguments:
  inline_script         Inline python script

optional arguments:
  -h, --help            show this help message and exit
  -i INP, --inp INP     Input file path; None=STDIN
  -o OUT, --out OUT     Output file path; None=STDOUT
  -F FS, -FS FS, --field-sep FS
                        the input field separator. Default=None implies white
                        space
  -OFS OFS, --out-field-sep OFS
                        the out field separator. Default=None implies same as
                        input FS.
  -ORS ORS, --out-rec-sep ORS
                        the output record separator. Default=None implies same
                        as input RS.
  -b BEGIN_SCRIPT, --begin BEGIN_SCRIPT
                        BEGIN block. initialize variables or whatever
  -e END_SCRIPT, --end END_SCRIPT
                        END block. Print summaries or whatever
  -im IMPORTS, --import IMPORTS
                        Imports block. Specify a list of module names to be
                        imported.Semicolon (;) is the delimiter. Ex:
                        json;numpy as np
  -it INIT_PATH, --init INIT_PATH
                        The rc file that initializes environment.Default is
                        $HOME/.awkg.py
  -v, --version         show program's version number and exit

示例

计算每序列单词的平均值和标准差

cat data/train.src | awkg -b 'arr=[]; import numpy as np' 'arr.append(NF)' \
   -e 'arr=np.array(arr); print(f"{NR} lines from {FNAME}, mean={arr.mean():.2f}; std={arr.std():.4f}")'

过滤记录

# use print() explicitely 
cat data/train.src  | awkg  'if NF >= 25: print(*R)' 

Assign boolean expression to special variable RET to trigger implicit print 
cat data/train.src  | awkg  'RET = NF >= 25'

# print respects the OFS value
cat data/train.src  |  awkg  'if NF >= 25: print(NR, NF)' -OFS='\t'

特殊变量

  • NF : 字段数
  • NR : 记录号
  • R : 一个数组,包含当前记录的所有列。
  • R0 : 类似于 $0,它存储在分割到 R 之前的输入行;由于Python不允许在标识符中使用 $,它被重命名为 R0
  • RET : 当此变量设置为真值 true 时,隐式触发 print(*R)
  • FS : 输入字段分隔符
  • OFS : 输出字段分隔符;除非显式设置,否则 OFS=FS
  • ORS : 输出记录分隔符
  • RS(当前未使用)
  • _locals_globals - 本地作用域和全局作用域中的所有变量

您可以使用任何有效的Python标识符,而不是上述变量

默认导入模块

以下模块默认导入

  • sys
  • os
  • re
  • from pathlib import Path

作者

相关工具

  • pawk 类似于此存储库,略有不同的实现。
  • gawk GNU awk

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源代码分发

awkg-0.3.0.tar.gz (5.3 kB 查看哈希值)

上传时间 源代码

构建分发

awkg-0.3.0-py3-none-any.whl (18.3 kB 查看哈希值)

上传时间 Python 3

由以下支持