Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question
add comment

2 Answers 2

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}'
share|improve this answer
add comment

The log is probably being output to stderr.

Instructions for bash

share|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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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