【发布时间】: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 运行这样的表达式是行不通的
提前感谢您!
【问题讨论】:
-
请将该示例输入的所需输出(无描述)添加到您的问题(无评论)。