I'm trying to use awk to parse an output generated by a java application, but it isn't working. It seems that the command after the pipe isn't able to get/see the data throwed by the java app.

I'm executing the following command (with the return generated by the command):

[root@localhost]# java -jar jmxclient.jar usr:pass host:port java.lang:type=Threading ThreadCount
06/11/2010 15:46:37 -0300 org.archive.jmx.Client ThreadCount: 103

What I need it's only the last part of the string. So I'm tryng to use awk (with pipe at the end of the line |awk -F ':' '{print $4}':

[root@localhost]# java -jar jmxclient.jar usr:pass host:port java.lang:type=Threading ThreadCount|awk -F ':' '{print $4}'

But the output isn't being parsed. It throws the entire string: 06/11/2010 15:46:37 -0300 org.archive.jmx.Client ThreadCount: 103

I also tryed to use |cut -f4 -d":" with the same result: the string isn't parsed.

So my question is, how do I parse the output in order to get just the number at the end of the string?

TIA,

Bob

link|improve this question

feedback

2 Answers

up vote 5 down vote accepted

Like Ignacio Vazquez-Abrams said, it's probably going to stderr. As well, there's an awk trick that will make your life easier: $NF, which is the last field. Thus, I would do something like this:

java -jar jmxclient.jar usr:pass host:port java.lang:type=Threading ThreadCount 2>&1 | awk -F: '{print $NF}'
link|improve this answer
feedback

The log is probably being output to stderr.

Instructions for bash

link|improve this answer
Thanks. That was the case. But simply using 2>$1 was generating an ambiguous redirect error. Using Bill Weiss awk trick solved it. – Bob Rivers Jun 11 '10 at 20:43
1  
2>$1 won't work, but 2>&1 will. I'm just adding on to that with a bit of Awk-fu. – Bill Weiss Jun 11 '10 at 21:48
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.