Domanda

I'm new to shell scripts and I'm not having much luck with it. How would I extend the following snippet to just get the number between the [ and ] ?

$ forever list | grep app.js
--> data:   [0] LxSl node    app.js 26017   49833 /Users/username/.forever/LxSl.log 0:0:0:0.57 

Thanks for your help

È stato utile?

Soluzione

Pipe it to awk '{print $3}'}, that'll give you [0] To remove the brackets, you can e.g. use tr -d []

Everything can also be done from within awk, using gsub. Then there is sed...

...| awk '{print $3}' | tr -d []
0

...| awk '{gsub(/\[|\]/, "", $3);print $3}'
0

Altri suggerimenti

You can use sed to do this, an example:

$ echo "--> data:   [0] LxSl node    app.js 26017   49833 /Users/username/.forever/LxSl.log 0:0:0:0.57"|sed -e "s/[^[]*\[\([0-9]\)\].*/\1/"
0

If you have GNU grep: ... | grep -Po '(?<=\[)\d+(?=\])'

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top