{"id":21895673,"url":"https://github.com/yedf2/awkj","last_synced_at":"2025-07-02T06:35:33.961Z","repository":{"id":57188124,"uuid":"85166615","full_name":"yedf2/awkj","owner":"yedf2","description":"nodejs version of simplified awk / nodejs版的简化awk","archived":false,"fork":false,"pushed_at":"2017-03-17T14:12:34.000Z","size":4,"stargazers_count":9,"open_issues_count":0,"forks_count":5,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-23T02:21:40.158Z","etag":null,"topics":["awk","command-line","nodejs"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yedf2.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-03-16T07:38:37.000Z","updated_at":"2023-11-03T17:12:47.000Z","dependencies_parsed_at":"2022-08-28T14:50:47.442Z","dependency_job_id":null,"html_url":"https://github.com/yedf2/awkj","commit_stats":null,"previous_names":["yedf/awkj"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/yedf2/awkj","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yedf2%2Fawkj","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yedf2%2Fawkj/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yedf2%2Fawkj/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yedf2%2Fawkj/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yedf2","download_url":"https://codeload.github.com/yedf2/awkj/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yedf2%2Fawkj/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263087967,"owners_count":23411970,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["awk","command-line","nodejs"],"created_at":"2024-11-28T13:39:01.004Z","updated_at":"2025-07-02T06:35:33.927Z","avatar_url":"https://github.com/yedf2.png","language":"JavaScript","readme":"awkj --node版本的awk\n====\n\n*   使用js，拥有强大的编程能力，无需记忆awk特有语法\n*   高效运行，在一个百万行日志处理的测试中，用时仅比awk多30%\n\n### 安装\n\n```sh\n  sudo npm -g install awkj  #需要node6+ 环境\n```\n\n### 使用\n\n```sh\n  echo -e \"1  3\\n2 5\" | awkj 'console.log($2)'\n```\n\n*  可使用的预定义变量有：FS NR $n (含义与awk相同) G（用于全局数据的存储）\n*  END 含义与awk相同\n\n```sh\n  echo -e \"1  3\\n2 5\" | awkj 'if(NR==1) {G.sum=0;} G.sum+=parseFloat($2); END console.log(G.sum/NR)'\n```\n\n### 实现\n\n核心代码如下：\n```javascript\n  let G={NR:0}; // 全局数据\n  let [body, end] = cont.split('END'); //用户输入的代码分为两部分，前面的body每行执行，END后面的代码结束时执行\n  eachLine(line=\u003e{\n    G.NR ++; // G.NR为行号\n    let fs = line.split(sep); // 把行切分为字段\n    let obj = {FS:fs.length, $0:line, G, NR:G.NR}; // 构建实参\n    fs.forEach((e,i)=\u003eobj['$'+(i+1)] = e); //构建$1...$n\n    //函数内部需要解构obj，并且解构$1...$n，最多解构到$99\n    G.func = G.func || eval(`obj=\u003e{ \n        let {${Object.keys(obj).join(',')}, ${'a'.repeat(99-fs.length).split('').map((e,k)=\u003e`$${k+1+fs.length}`).join(',')}} = obj;\n        ${body}\n      }`);\n    G.func(obj); //根据用户输入构建函数并调用\n  }, ()=\u003e{ //文件结束的回调\n    end \u0026\u0026 eval(`obj=\u003e{let {NR,G}=obj;${end}}`)({NR:G.NR,G}); //构建尾部函数并调用\n  });\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyedf2%2Fawkj","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyedf2%2Fawkj","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyedf2%2Fawkj/lists"}