StringTokenizer from Linux shell script

B

Bart_D

Hi,

I have the folowing problem.

I got a script that gives me serverinfo. I need to parse that text in
such a way that I can manipulate it.

The returned text looks like this :

Location Space Available Used
-------------------------------------------
HD1 50GB 2GB 48GB
HD2 10GB 8GB 2GB


(something like the "df"-command in Linux.)

I need to find out how I can separate the text from the spaces and
"save" the text in a var. But I have no idea how to do this. I am new
to Java and have little or no experience with Linux ... The ssh script
is not the problem. Parsing the text is. Hope somebody can help me.
 
A

Andrew Thompson

I got a script that gives me serverinfo. I need to parse that text in
such a way that I can manipulate it.

The returned text looks like this :

Location Space Available Used

StringTokenizer st = new StringTokenizer(s, " ", false);
on each line should produce an ST with four tokens,
do with them as you will.
(Note though, that 'false' is default for the
other constructors)

ST is unfortuanately deprecated in 1.5, you might
look to String.split instead..
..But I have no idea how to do this. I am new
to Java

A better group for the moment might be..
<http://www.physci.org/codes/javafaq.jsp#cljh>
 
T

Thomas Weidenfeller

Andrew said:
ST is unfortuanately deprecated in 1.5, you might
look to String.split instead..

I don't see deprecated tags in the 1.5 documentation of StringTokenizer.
It only says that usage is discouraged [sp?]. I didn't try with a 1.5.
compiler. Does the compiler indeed complain?

/Thomas
 
T

Thomas Weidenfeller

Bart_D said:
The returned text looks like this :

Location Space Available Used
-------------------------------------------
HD1 50GB 2GB 48GB
HD2 10GB 8GB 2GB


(something like the "df"-command in Linux.)

I need to find out how I can separate the text from the spaces and
"save" the text in a var. But I have no idea how to do this. I am new
to Java and have little or no experience with Linux ... The ssh script
is not the problem. Parsing the text is. Hope somebody can help me.

I wouldn't fire up Java for this. Since you mentioned you are on Linux,
and you already have a script, awk or perl are likely a much better
choice. Even simple line reads from the shell would work.

Already the JVM startup might take more time than processing the first
half MB of such input data with on-board means.

/Thomas
 
A

Andrew Thompson

Andrew said:
ST is unfortuanately deprecated in 1.5, you might
look to String.split instead..

I don't see deprecated tags in the 1.5 documentation of StringTokenizer.
It only says that usage is discouraged [sp?]. I didn't try with a 1.5.
compiler. Does the compiler indeed complain?

Whoops, sorry. I based that entirely on
hearsay. I do not have, and am not intending
to get for some time in the future, the 1.5
SDK.

Checking the actual JDocs (wow, ..what an
original thought!) for 1.5 I notice it says..

"StringTokenizer is a legacy class that is retained
for compatibility reasons although its use is discouraged
in new code. It is recommended that anyone seeking this
functionality use the split method of String or the
java.util.regex package instead."

So, not _deprecated_, but 'discouraged'.
Seems we can rely on it for a while yet.
 
B

Bart_D

I need to find out how I can separate the text from the spaces and
I wouldn't fire up Java for this. Since you mentioned you are on Linux,
and you already have a script, awk or perl are likely a much better
choice. Even simple line reads from the shell would work.

Already the JVM startup might take more time than processing the first
half MB of such input data with on-board means.

/Thomas

Getting the data is not the problem. But my application has to
manipulate it (make a PDF of it etc.)

I managed to separate the words from the spaces with a regex:

String[] lines = in.split("(?:[ ]{1}[ ]*)|\\n");
String[] out = new String[0];
for (int i=0; i<lines.length; i++){
out = new String;
out = lines;

it now separates on 1 space and a newline which should be good for my
needs. Now I just need to find out how to pass the String[] object to
my main class, because at the moment it's giving me something like
[Ljava.lang.String;@9a8a68

(yes this is my first real java/programming project)

Thanks for the assistance, should anyone know how to get the String[]
values from the [Ljava.lang.String;@9a8a68-thingie, it would come in
handy :)

Bart
 
R

Roedy Green

String[] lines = in.split("(?:[ ]{1}[ ]*)|\\n");
String[] out = new String[0];
for (int i=0; i<lines.length; i++){
out = new String[



String[] lines = in.split("(?:[ ]{1}[ ]*)|\\n");
splits ONE line into several WORDS.
Give your variable accurate names and the errors will be much more
obvious.

You need to collect your lines/words as you build them in an
ArrayList.

ArrayList allwords = new ArrayList( 20000 );
while ( stillMoreLineToProcess )
{
....

String [] oneLineOfWords = in.split("(?:[ ]{1}[ ]*)|\\n");

for ( int i=0; i<oneLineOfWords.length(); i++ )
{
allwords.add ( oneLineOfWords );
}
...
}

// Now we have a giant ArrayList of all the words. If we want to
// convert it to an array of strings, one word per String:
int size = allwords.size();
String[] words = ( String[]) allwords.toArray( new String[size] );
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top