【问题标题】:How to extract root level elements from the npm ls tree output?如何从 npm ls 树输出中提取根级元素?
【发布时间】:2019-12-18 12:20:22
【问题描述】:

我有一个 npm ls --prod --depth=0 命令的输出。我需要提取一个根级元素,而不需要在它们前面加上非字符 - 只是名称“:”版本。

树如下所示:

├─┬ UNMET DEPENDENCY body-parser@1.18.2
│ ├── bytes@3.0.0
│ ├── content-type@1.0.4
│ ├── debug@2.6.9
│ ├── depd@1.1.2
│ ├── http-errors@1.6.2
│ ├── iconv-lite@0.4.19
│ ├── on-finished@2.3.0
│ ├── qs@6.5.1
│ ├── raw-body@2.3.2
│ └── type-is@1.6.15
├── UNMET DEPENDENCY bootstrap@3.3.7
├─┬ UNMET DEPENDENCY cfenv@1.0.4
│ ├── js-yaml@3.7.0
│ ├── ports@1.1.0
│ └── underscore@1.8.3
├─┬ UNMET DEPENDENCY cloudant@1.10.0
│ ├── async@2.1.2
│ ├── cloudant-nano@6.7.0
│ ├── debug@3.1.0
│ └── request@2.88.0
├─┬ UNMET DEPENDENCY cookie-parser@1.4.3
│ ├── cookie@0.3.1
│ └── cookie-signature@1.0.6
├── UNMET DEPENDENCY es6-promise@4.2.4
├── UNMET DEPENDENCY express@^4.13.4
├── UNMET DEPENDENCY express-async-handler@1.1.4
├─┬ UNMET DEPENDENCY express-handlebars@3.0.0
│ ├── glob@6.0.4
│ ├── graceful-fs@4.1.11
│ ├── handlebars@4.0.11
│ ├── object.assign@4.1.0
│ └── promise@7.3.1
├─┬ UNMET DEPENDENCY express-session@1.15.6
│ ├── cookie@0.3.1
│ ├── cookie-signature@1.0.6
│ ├── crc@3.4.4
│ ├── debug@2.6.9
│ ├── depd@1.1.2
│ ├── on-headers@1.0.1
│ ├── parseurl@1.3.2
│ ├── uid-safe@2.1.5
│ └── utils-merge@1.0.1
├─┬ UNMET DEPENDENCY express-ws@3.0.0
│ └── ws@2.3.1
├─┬ UNMET DEPENDENCY isomorphic-fetch@2.2.1
│ ├── node-fetch@1.7.3
│ └── whatwg-fetch@2.0.3
├── UNMET DEPENDENCY jquery@3.3.1
├── UNMET DEPENDENCY moment@2.20.1
├─┬ UNMET DEPENDENCY morgan@1.9.1
│ ├── basic-auth@2.0.1
│ ├── debug@2.6.9
│ ├── depd@1.1.2
│ ├── on-finished@2.3.0
│ └── on-headers@1.0.1
├─┬ UNMET DEPENDENCY multer@1.3.0
│ ├── append-field@0.1.0
│ ├── busboy@0.2.14
│ ├── concat-stream@1.6.0
│ ├── mkdirp@0.5.1
│ ├── object-assign@3.0.0
│ ├── on-finished@2.3.0
│ ├── type-is@1.6.15
│ └── xtend@4.0.1
├─┬ UNMET DEPENDENCY passport@0.3.2
│ ├── passport-strategy@1.0.0
│ └── pause@0.0.1
├─┬ passport-http@0.3.0
│ └── passport-strategy@1.0.0
├─┬ UNMET DEPENDENCY sha1@1.1.1
│ ├── charenc@0.0.2
│ └── crypt@0.0.2
├─┬ UNMET DEPENDENCY socket.io@2.0.4
│ ├── debug@2.6.9
│ ├── engine.io@3.1.4
│ ├── socket.io-adapter@1.1.1
│ ├── socket.io-client@2.0.4
│ └── socket.io-parser@3.1.2
├── UNMET DEPENDENCY vcap_services@0.3.4
├─┬ UNMET DEPENDENCY watson-developer-cloud@2.42.0
│ ├── async@2.6.0
│ ├── buffer-from@0.1.1
│ ├── cookie@0.3.1
│ ├── csv-stringify@1.0.4
│ ├── extend@3.0.1
│ ├── isstream@0.1.2
│ ├── object.omit@3.0.0
│ ├── object.pick@1.3.0
│ ├── request@2.83.0
│ ├── solr-client@0.7.0
│ ├── vcap_services@0.3.4
│ └── websocket@1.0.25
└─┬ UNMET DEPENDENCY xlsx@0.12.13
  ├── adler-32@1.2.0
  ├── cfb@1.0.8
  ├── codepage@1.13.1
  ├── commander@2.15.1
  ├── crc-32@1.2.0
  ├── exit-on-epipe@1.0.1
  └── ssf@0.10.2

以下代码解析这棵树并提取所有行而不是根行:

npm ls --prod --depth=0 | sed -e 's/[^a-zA-Z0-9@.\\/ -]//g' -e 's/^[ \\t@]*//' | sed '/deduped/d' | sed -e 's/UNMET DEPENDENCY //' | sed -e 's/UNMET OPTIONAL DEPENDENCY //' | sed '1 d' | sed -e 's/@/:/g' -e 's/^:/@/' | sort | uniq

根级元素列表的预期结果

body-parser:1.18.2
bootstrap:3.3.7
cfenv:1.0.4
cloudant:1.10.0
cookie-parser:1.4.3
express:4.13.4
express-async-handler:1.1.4
express-handlebars:3.0.0
express-session:1.15.6
express-ws:3.0.0
isomorphic-fetch:2.2.1
jquery:3.3.1
moment:2.20.1
morgan:1.9.1
multer:1.3.0
passport:0.3.2
passport-http:0.3.0
sha1:1.1.1
socket.io:2.0.4
vcap_services:0.3.4
watson-developer-cloud:2.42.0
xlsx:0.12.13

实际结果 - 以 name:version 格式列出的树中所有元素的列表。我需要让它至少在 RHEL GNU/Linux 和 Ubuntu 上运行。

还有一个条件是不要使用带有树符号的表达式,如下所示: sed -e 's/^├─┬ //;s/^├── //;s/^─┬ //;s/^└─┬ //;s/^└── //;'从 Java 运行这样的表达式是行不通的

提前感谢您!

【问题讨论】:

  • 请将该示例输入的所需输出(无描述)添加到您的问题(无评论)。

标签: java sed sh


【解决方案1】:
sed  -E '/^(├|└─)/!d;s/^(├|└─).*UNMET DEPENDENCY ([a-zA-Z0-9_\.-]+)@\^?(([0-9]\.?)+)/\2:\3/g'

在 Ubuntu 上提供以下输出:

body-parser:1.18.2
bootstrap:3.3.7
cfenv:1.0.4
cloudant:1.10.0
cookie-parser:1.4.3
es6-promise:4.2.4
express:4.13.4
express-async-handler:1.1.4
express-handlebars:3.0.0
express-session:1.15.6
express-ws:3.0.0
isomorphic-fetch:2.2.1
jquery:3.3.1
moment:2.20.1
morgan:1.9.1
multer:1.3.0
passport:0.3.2
passport-http:0.3.0
sha1:1.1.1
socket.io:2.0.4
vcap_services:0.3.4
watson-developer-cloud:2.42.0
xlsx:0.12.13

根据 cmets 的要求:使用以下带有UNMET OPTIONAL DEPENDENCY 或不带任何 UNMET... 的 sed-command 根项目将以相同的格式列出:

sed  -E '/^(├|└─)/!d;s/^(├|└─)[^a-zA-Z0-9_]+ ?(UNMET (OPTIONAL )?DEPENDENCY)? ?([a-zA-Z0-9_\.-]+)@\^?(([0-9]\.?)+)/\4:\5/g'

【讨论】:

  • 是的,这是预期的。我会在 RHEL 上试用并通知您。
  • 它适用于 ubuntu 和 RHEL。谢谢!但问题是并非所有行都可以包含 UNMET DEPENDENCY 词。可能你也会有解决方案吗?我也在尝试,但我在 sed 中没有那么快。
  • 我试试这个:cat dependencies_tree.tmp | sed -E '/^(├|└─)/!d;s/^(├|└─).*(未满足的依赖|未满足的可选依赖)([a-zA-Z0-9_\\.-]+ )@\^?(([0-9]\.?)+)/\3:\4/g'| sed '/deduped/d' |排序 | uniq 但不知道如何在 sed 中说 UNMET DEPENDENCY 或 UNMET OPTIONAL DEPENDENCY 可以始终为 1 或不为 1
  • 你可以使用?:表示它可以有1个根本没有
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-10
  • 2013-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-14
相关资源
最近更新 更多