Groovy grep word

I wanted grep for a java process and then find the maximum heap memory. I tried this

def ex =['sh','-c','ps -aef | grep Xmx']
String str =  ex.execute().text

while strhas something like java -Xmx1024M / kv / classes / bebo / -Xms512M How to extract the value of 1024M? I planned on a java regex user, but thought someone might know a cool way in groovy.

+3
source share
3 answers

Here's a groovy version that doesn't need grep (or sed :):

("ps -aef".execute().text =~ /.*-Xmx([0-9]+M).*/).each { full, match -> println match } 
+5
source

In Java:

String ResultString = null;
Pattern regex = Pattern.compile("-Xmx(\\d+M)");
Matcher regexMatcher = regex.matcher(str);
if (regexMatcher.find()) {
    ResultString = regexMatcher.group(1);
} 
0
source

, , Xmx, ex:

def ex =['sh','-c',"ps -aef | grep Xmx | sed -e 's/^.*Xmx\([0-9]*[mM]*\) *$/\1/'"]; 
String str = ex.execute().text;

sed ps , ps .

0

Source: https://habr.com/ru/post/1699599/


All Articles