bash - output command that can install listed npm global packages


bash - output command that can install listed npm global packages
I know npm ls -g --depth=0
will produce output
npm ls -g --depth=0
C:UsersleonAppDataRoamingnpm
+-- create-react-app@1.5.2
+-- eslint@5.2.0
+-- prettier@1.13.7
`-- serve@9.4.0
I'm looking for a bash command that can output the following based on the above output:
npm i -g create-react-app
npm i -g eslint
npm i -g prettier
npm i -g serve
2 Answers
2
you can use the sed
tool to handle this
sed
npm ls -g --depth=0 | sed -nE 's/^W+/npm i -g /p'
@version
Add
s/@.*//;
just after the opening single quote.– tripleee
17 hours ago
s/@.*//;
W+
isn't properly portable, though; maybe switch to [^A-Za-z0-9_]*
instead. Then you can also drop the -E
option which isn't portable, either.– tripleee
17 hours ago
W+
[^A-Za-z0-9_]*
-E
@tripleee can you post the full command plz, I'm not very familiar with bash syntax
– Leon Du
7 hours ago
I've done so now, and fixed a typo I noticed in this answer. +1
– tripleee
3 mins ago
Here is a slight refinement of the answer posted by @0.sh
npm ls -g --depth=0 |
sed -n 's/^[^A-Za-z0-9_]*//;s/@.*$//p' |
xargs -n 1 npm i -g
Moving the code into xargs
causes the generated commands to be executed, not just printed.
xargs
If you prefer, the extraction could be done with Awk or whatever instead. Even grep
;
grep
npm ls -g --depth=0 |
grep -o '[A-Za-z][^@]*' |
xargs -n 1 npm i -g
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
It's really close, but how to produce them without the trailing
@version
– Leon Du
23 hours ago