how to find a pattern in a file and get output in another file indifferent format

Discussion in 'Java' started by vinitbhu, Mar 17, 2008.

  1. vinitbhu

    vinitbhu Guest

    Hello members,

    I am very new in Java programming, I want some help from you.
    Actually I have one file named as "list01.txt" in which the content
    is in following format

    Record # 1
    Au: Kuzur, A D
    Au: Dito, Bolte
    Ti: Elements of semantic web.
    Pu: Nasadac
    Pl: New Delhi

    Record # 2
    Au: Kumar, Vinit
    Ti: Learning how to learn.
    Pu: Pooja
    Pl: Bangalore

    Record # 3
    Au: Pandey, Anand K
    Ti: Emerging minds.
    Pu: Lakhotia
    Pl: Varanasi

    what I am trying to do is to get output in some new files in following
    format

    In file record1.txt
    <author> Kuzur, A D</author>
    <author>Dito, Bolte </author>
    <title> Elements of semantic web.</title>
    <publisher> Nasadac</publisher>
    <place>New Delhi</place>

    In file "record2.txt"
    <author> Kumar, Vinit</author>
    <title> Learning how to learn.</title>
    <publisher> Pooja</publisher>
    <place>Bangalore</place>

    In file "record3.txt"
    <author> Pandey, Anand K</author>
    <title> Emerging minds.</title>
    <publisher> Lakhotia</publisher>
    <place> Varanasi</place>


    can anybody help me I am new to Java.
    thanks.
    vinitbhu, Mar 17, 2008
    #1
    1. Advertising

  2. vinitbhu

    GArlington Guest

    On Mar 17, 10:55 am, vinitbhu <> wrote:
    > Hello members,
    >
    > I am very new in Java programming, I want some help from you.
    > Actually I have one file named as "list01.txt" in which the content
    > is in following format
    >
    > Record # 1
    > Au: Kuzur, A D
    > Au: Dito, Bolte
    > Ti: Elements of semantic web.
    > Pu: Nasadac
    > Pl: New Delhi
    >
    > Record # 2
    > Au: Kumar, Vinit
    > Ti: Learning how to learn.
    > Pu: Pooja
    > Pl: Bangalore
    >
    > Record # 3
    > Au: Pandey, Anand K
    > Ti: Emerging minds.
    > Pu: Lakhotia
    > Pl: Varanasi


    You will need (custom) file reader to get this file format into XML-
    like document.

    >
    > what I am trying to do is to get output in some new files in following
    > format
    >
    > In file record1.txt
    > <author> Kuzur, A D</author>
    > <author>Dito, Bolte </author>
    > <title> Elements of semantic web.</title>
    > <publisher> Nasadac</publisher>
    > <place>New Delhi</place>
    >
    > In file "record2.txt"
    > <author> Kumar, Vinit</author>
    > <title> Learning how to learn.</title>
    > <publisher> Pooja</publisher>
    > <place>Bangalore</place>
    >
    > In file "record3.txt"
    > <author> Pandey, Anand K</author>
    > <title> Emerging minds.</title>
    > <publisher> Lakhotia</publisher>
    > <place> Varanasi</place>
    >
    > can anybody help me I am new to Java.
    > thanks.


    And then you can choose any XML processor/parser to do that.
    GArlington, Mar 17, 2008
    #2
    1. Advertising

  3. vinitbhu

    Roedy Green Guest

    Re: how to find a pattern in a file and get output in another file in different format

    On Mon, 17 Mar 2008 03:55:15 -0700 (PDT), vinitbhu
    <> wrote, quoted or indirectly quoted someone who
    said :

    >Au: Pandey, Anand K


    you can read the file with CSVReader. Just tell is your field
    separator is a ':' insteao of the usual ','.

    See http://mindprod.com/jgloss/csv.html
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Mar 17, 2008
    #3
  4. vinitbhu

    Lord Zoltar Guest

    You can get the data from the file with regular expressions and
    groupings. I haven't tried it, but this expression:
    Record\ \#\ ([0-9])\ \n(Au: .+\n){1,}(Ti: .+\n)(Pu: .+\n)(Pl: .+)
    would probably match well with what you want.
    Lord Zoltar, Mar 17, 2008
    #4
  5. vinitbhu

    Mark Space Guest

    Re: how to find a pattern in a file and get output in another filein different format

    Roedy Green wrote:
    > On Mon, 17 Mar 2008 03:55:15 -0700 (PDT), vinitbhu
    > <> wrote, quoted or indirectly quoted someone who
    > said :
    >
    >> Au: Pandey, Anand K

    >
    > you can read the file with CSVReader. Just tell is your field
    > separator is a ':' insteao of the usual ','.
    >
    > See http://mindprod.com/jgloss/csv.html


    I would not do this, just because it would be easy for a book title to
    have ":" in it.

    I'd have to see the actual file format spec, but it looks like the
    identifier for the record type is always two letters at the beginning of
    the line. I'd key off that. Just use readLine(), and check char(0) and
    char(1). Dead simple, and bomb proof. Most likely faster too.
    Mark Space, Mar 17, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TechBookReport

    Generics - good, bad or indifferent?

    TechBookReport, Oct 28, 2005, in forum: Java
    Replies:
    35
    Views:
    1,126
    TechBookReport
    Nov 1, 2005
  2. Steven T. Hatton
    Replies:
    9
    Views:
    414
    Victor Bazarov
    Oct 20, 2004
  3. sunny
    Replies:
    1
    Views:
    461
    Salt_Peter
    Dec 7, 2006
  4. JT
    Replies:
    3
    Views:
    568
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=
    Jul 1, 2007
  5. nani
    Replies:
    2
    Views:
    160
    comp.llang.perl.moderated
    Mar 14, 2008
Loading...

Share This Page