-
Hello,
How do I read a '|' (pipe) seperated ASCII file in UNIX?
Thanks
Sameer
-
you can use awk or cut to get your data from the file, but what exactly do you want to do ???
-
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
-
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!
-
Sameer - Just seen you reply after I posted my answer. Ignore my suggestions.
-
#!/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
-
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
-
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
-
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
-
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|