Wednesday, May 21, 2008

Transform a sequence of text in a Java String Array

When your input text sound like:
oil sunflowerse    tonn   march    said  tender  import  trader   egypt     may

than you will get the following String Array:
{"oil", "sunflowerse", "tonn", "march", "said", "tender", "import", "trader", "egypt", "may"}

by using this sed script:
sed 's/^ *//; s/ *$//; s/ \{1,\}/ /g; s/ /\", \"/g; s/^/{"/; s/$/"}/' filename

It performs this tasks:

  • Remove all multiple spaces at the start

  • Remove all multiple spaces at the end

  • Remove all multiple spaces between the words

  • Replace all single spaces with ", "

  • Add {" at the start

  • Add "} at the end

No comments: