Parsing Text into Tab Delimeted File

I have some text data from a legacy system (old Mainframe) which I am trying to input into
a tab delimeted file for input into a relational database.  The goal is to read in a file
of type *.DAT, and then output into a tab delimeted *.TXT file
I only have soft copies of the *.DAT files for input.  The data output is static
and certain fields are specified by their line number, :#:.  I would like to parse these
files, but I am not sure on how to go about doing it.  The file structure looks like this.  
A5

A5543645674645646446
     :01:KI
:02:AMERA123456C897
:03:
:04:
:05:
:10:BIRDCAGE
:12:A50212USD1234,89
:74:LONG STRING HERE
:113:B
:245:123456XIX1234
-
A62354424334242423234
     :01:KI
:02:EURO123456C897
:03:
:04:
:05:
:06:
:10:BIRDCAGE
:12:A50212USD2345,89
:74:LONG STRING HERE
:113:B
:245:123456XIX1235
-

The values theat I would like to import into SQL Server, are only :02:,:10: :12:,:74:
:113:,:245: However in some cases these values will be null, and
sometimes those lines, and line numbers will not exist at all.  When parsing into
tab delimeted, I want the output to look like this:


:02:     :10:     :12:     :74:     :113:     :245:
Value     Value     Value     Value     Value     Value
Value     Value     Null     Value     Value     Value

So that each line number is a column, and the values for those columns are the strings
next to those values.  If the line number does not exist, or there is no data in the line
number, the value will be null.

As you can see, there are multiple records in one file, and it there are not always
the same number of records in each file.  

I have never done text parsing before, however need a lot of help on this one, any code,
suggestions, or pointers to the right direction will be most helpful.

Thank You!
LVL 1
superfly18Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aozarovCommented:
Here is half code / half pseudo code:

 Set itemsThatICare = new HashSet();
itemsThatICare.add(":02:");
itemsThatICare.add(":10:");
..

Map values = new HashMap();

// read the file:
 BufferedReader in = new BufferedReader(new FileReader("infilename"));
        String str;
        while ((str = in.readLine()) != null) {
            process(str, values, itemsThatIcare);
        }
        in.close();
    } catch (IOException e) {
    }

// write to new File
 try {
        BufferedWriter out = new BufferedWriter(new FileWriter("outfilename"));
        // write headers
        for (Iterator i = itemsThatIcare.iterator(); i.hasNext(); )
        {
             out.write(i.next().toString());
             out.write("\t");
        }
         out.write("\n");

       //write values
       while (!values.isEmpty())
       {
        for (Iterator i = itemsThatIcare.iterator(); i.hasNext(); )
        {
            LinkedList list = (LinkedList) values.get(i.next());
           if (list.isEmpty())
               out.write("Null");
          else
          {
               out.write(list.removeFirst().toString());
          }
         
          out.write("\t");
        }
         out.write("\n");

       }

        out.close();
    } catch (IOException e) {
    }

// process logic:
if (!str.startsWith(":"))
return;

int indexOfColon = str.indexOf(':',  1);
if (indexOfcolon <= 0)
return;

String token  = str.substing(0, indexOfColon + 1);
String value = str.substring(indexOfColon + 1);
if (itemsThatICare.contains(token))
{
List list = (List) values.get(token);
if (list == null)
{
list = new LinkedList();
values.put(token, list);
}
list.add(value);
}
0
aozarovCommented:
You also might want to start the process logic with
str = str.trim();
If  you want to accept :02: that has spaces before it.

Also, to prevent infinite loop and NullPointerException change
the loop in  "write values" to:

 for (Iterator i = itemsThatIcare.iterator(); i.hasNext(); )
        {
            LinkedList list = (LinkedList) values.get(i.next());
           if (list == null)
               out.write("Null");
          else
          {
               out.write(list.removeFirst().toString());
               if (list.isEmpty())
                    i.remove();
          }
         
          out.write("\t");
        }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NaeemgCommented:
is ur intention to extract only :02:     :10:     :12:     :74:     :113:     :245:
strings?
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

superfly18Author Commented:
Yes, only those strings.
0
NaeemgCommented:
i've made a method for u. try the following ,

//define these globally in ur class.

  Vector requiredStringsArray = new Vector();
  int firstIndexOfColon = -1;
  int lastIndexOfColon = -1;

// add followin method
  void getStr(String str)
  {
    //System.out.println(str);
      firstIndexOfColon = str.indexOf(":");
      if(firstIndexOfColon > -1)
      {
        for(int i = firstIndexOfColon + 1; i < str.length(); i++)
        {
          if((str.charAt(i) == ':'))
          {
            lastIndexOfColon = i;
            break;
          }
        }
       
        requiredStringsArray.addElement(str.substring(firstIndexOfColon, lastIndexOfColon + 1));
        String s = str.substring((lastIndexOfColon + 1), str.length());
        //System.out.println(s);
        getStr(s.trim());
      }    
  }


// now call that method
    getStr(":02:     asdf:10:BFDF     343:12:adfs     :74:232     :113:     :245:");

//-- display extracted stings
    for(int i = 0; i < requiredStringsArray.size(); i++)
      System.out.println(requiredStringsArray.elementAt(i));

// and tell me whether it works or not? i tested here with above string , it works fine
0
aozarovCommented:
superfly18, did you undestand the half logic/ half code I gave you above?
0
superfly18Author Commented:
Still working on it, as soon as I get a chance to put it together I will post it

Thanks!
0
superfly18Author Commented:
As a side note, it was helpful to use the | as opposed to the /t for inputing into a database.  
0
aozarovCommented:
:)
0
NaeemgCommented:
superfly18 did'nt u test my code?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.