bmw0128
bmw0128

Reputation: 13718

How can I split out individual column values from each line in a text file?

I have lines in an ASCII text file that I need to parse. The columns are separated by a variable number of spaces, for instance:

column1 column2     column3

How would i split this line to return an array of only the values?

thanks

Upvotes: 1

Views: 2484

Answers (3)

Amber
Amber

Reputation: 527528

String testvar = "Some   Data    separated  by     whitespace";
String[] vals = testvar.split("\\s+");

\s means a whitespace character, the + means 1 or more. .split() splits a string into parts divided by the specified delimiter (in this case 1 or more whitespace characters).

Upvotes: 8

Check the StringTokenizer class.

Upvotes: 0

JP Silvashy
JP Silvashy

Reputation: 48555

sed 's/  */\n/g' < input

Two spaces there btw.

Upvotes: 0

Related Questions