?
Solved

Parsing Text into Tab Delimeted File

Posted on 2005-04-05
10
Medium Priority
?
272 Views
Last Modified: 2010-03-31
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!
0
Comment
Question by:superfly18
  • 4
  • 3
  • 3
10 Comments
 
LVL 15

Expert Comment

by:aozarov
ID: 13713125
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
 
LVL 15

Accepted Solution

by:
aozarov earned 2000 total points
ID: 13713156
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
 
LVL 5

Expert Comment

by:Naeemg
ID: 13714074
is ur intention to extract only :02:     :10:     :12:     :74:     :113:     :245:
strings?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:superfly18
ID: 13715514
Yes, only those strings.
0
 
LVL 5

Expert Comment

by:Naeemg
ID: 13715975
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
 
LVL 15

Expert Comment

by:aozarov
ID: 13716899
superfly18, did you undestand the half logic/ half code I gave you above?
0
 
LVL 1

Author Comment

by:superfly18
ID: 13716918
Still working on it, as soon as I get a chance to put it together I will post it

Thanks!
0
 
LVL 1

Author Comment

by:superfly18
ID: 13723857
As a side note, it was helpful to use the | as opposed to the /t for inputing into a database.  
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13723891
:)
0
 
LVL 5

Expert Comment

by:Naeemg
ID: 13724025
superfly18 did'nt u test my code?
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses
Course of the Month17 days, 1 hour left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question