How to read a file in UNIX
DBAsupport.com Forums - Powered by vBulletin
Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: How to read a file in UNIX

  1. #1
    Join Date
    Sep 2001
    Location
    Düsseldorf, Germany.
    Posts
    588

    Question

    Hello,

    How do I read a '|' (pipe) seperated ASCII file in UNIX?

    Thanks

    Sameer

  2. #2
    Join Date
    Feb 2001
    Location
    Paris, France
    Posts
    809
    you can use awk or cut to get your data from the file, but what exactly do you want to do ???

  3. #3
    Join Date
    Sep 2001
    Location
    Düsseldorf, Germany.
    Posts
    588

    Question How to read a file in UNIX

    >you can use awk or cut to get your data from the file, but >what exactly do you want to do ???

    I have a .txt file '|' (tab) seperated.

    which looks like..

    ID|FIRSTNAME|LASTNAME|DATE|EMAIL|DOB

    I want to read this file line by line in a LOOP into variables..

    say .. into variables..

    $ID
    $FIRSTNAME
    $LASTNAME
    $DATE
    $EMAIL
    $DOB

    and do some processing...

    how do I do this using awk or any other method in '#bin/sh'

    Thanks

    Sameer

  4. #4
    Join Date
    Dec 2001
    Location
    UK
    Posts
    1,684
    Are you intending to read this file into the database?

    If so you could use:

    a) The UTL_FILE package to read and process the file.

    http://technet.oracle.com/docs/produ...ile.htm#998101

    b) SQL*Loader to load it straight into the database.

    http://technet.oracle.com/docs/produ...rt2.htm#435958

    c) DIY$ Dynamic views using UTL_FILE.

    http://www.oracle.com/oramag/webcolu...y_dynamic.html

    Cheers!
    Tim...
    OCP DBA 7.3, 8, 8i, 9i, 10g, 11g
    OCA PL/SQL Developer
    Oracle ACE Director
    My website: www.oracle-base.com
    My blog: www.oracle-base.com/blog

  5. #5
    Join Date
    Dec 2001
    Location
    UK
    Posts
    1,684
    Sameer - Just seen you reply after I posted my answer. Ignore my suggestions.
    Tim...
    OCP DBA 7.3, 8, 8i, 9i, 10g, 11g
    OCA PL/SQL Developer
    Oracle ACE Director
    My website: www.oracle-base.com
    My blog: www.oracle-base.com/blog

  6. #6
    Join Date
    Feb 2001
    Location
    Paris, France
    Posts
    809
    #!/bin/ksh

    for LINE in $(cat file)
    do
    echo $LINE | sed 's/|/ /g' | read ID FIRSTNAME LASTNAME DATE EMAIL JOB
    # here you have your values in your variables
    # and you can do what you want
    done

    note : replace $(...) with backquotes `...` if using sh

  7. #7
    Join Date
    Sep 2001
    Location
    Düsseldorf, Germany.
    Posts
    588

    Question How to read a file in UNIX

    Hi,

    Thanks for the reply.. But here it..

    My script is


    -------------
    #bin/sh
    filename=`/opt/oracle/sameerd/sam/sam.txt`
    for LINE in $filename
    do
    rc=`echo $LINE | sed -e 's/|/ /g' | read ID FNAME NAME`
    echo $ID
    done
    -------------

    and .txt file is

    -------------
    10 | JOHN | MILLER |
    20 | MARK | BRUNCE |
    -------------

    And the output is
    -------------
    /opt/oracle/sameerd/sam/sam.txt: 10: not found
    /opt/oracle/sameerd/sam/sam.txt: JOHN: not found
    /opt/oracle/sameerd/sam/sam.txt: MILLER: not found
    /opt/oracle/sameerd/sam/sam.txt: 20: not found
    /opt/oracle/sameerd/sam/sam.txt: MARK: not found
    /opt/oracle/sameerd/sam/sam.txt: BRUNCE: not found
    -------------

    Please let me know, when I have said
    echo $ID.. why it echo all the parsed elements?

    Thanks

    Sameer

  8. #8
    Join Date
    Feb 2001
    Location
    Paris, France
    Posts
    809
    1) /bin/sh, not bin/sh
    2) look at the structure :

    echo $LINE | sed 's/|/ /g' | read ID FIRSTNAME LASTNAME DATE EMAIL JOB

    I echo my line, substitute | by spaces, and then allocate each part of the line in the different variables ID, ..., JOB

    what you've typed is incorrect, you do not need rc=`...`, variables ID,..., JOB are already affected

  9. #9
    Join Date
    Sep 2001
    Location
    Düsseldorf, Germany.
    Posts
    588

    How to read a file in UNIX

    Hi,

    Thanks lot ..

    It worked.. Thanks a lot

    >I echo my line, substitute | by spaces, and then allocate >each part of the line in the different variables ID, ...,

    We substiture | by space

    But If I have SPACE in my name
    f.ex 'ARNO VAN MARK' is the first name

    i.e

    FIRSTNAME|LASTNAME =

    ARNO VAN MARK | MILLER

    then.. it takes 'VAN MARK' as another element ..

    Can we eliminate this?

    Thanks

    Sameer

  10. #10
    Join Date
    Feb 2001
    Location
    Paris, France
    Posts
    809
    sure, using awk or cut (as we said a long time ago)

    example :

    ID=`echo $LINE | cut -d'|' -f1`
    FIRSTNAME=`echo $LINE | cut -d'|' -f2`
    LASTNAME=`echo $LINE | cut -d'|' -f3`

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width