Computing.Net > Forums > Unix > Read first character of each Line

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

Read first character of each Line

Reply to Message Icon

Name: abhayg4
Date: March 28, 2008 at 11:57:44 Pacific
OS: Windows XP Professional
CPU/Ram: 2 GB
Comment:

Hi,

My query is as follows:

(1) I want to write a Schell Script which will first read multiple files.Each file contains multiple lines/records.
(2) Each line /record in a file starts with 'H' , 'D' or 'P'
(3) So want a shell script - which will read the FIRST CHARACTER of each line in a file.
(4) If the first character is 'H' , it will insert that line/record in database TABLE1
(5) If the first character is 'D' , it will insert that line/record in database TABLE2
(6) If the first character is 'P' , it will insert that line/record in database TABLE3

So depending on the FIRST CHARACTER of each line in a file , that line/record will be inserted into different oracle database tables.

-- Abhay

--Abhay



Sponsored Link
Ads by Google

Response Number 1
Name: abhayg4
Date: March 28, 2008 at 12:03:32 Pacific
Reply:

Hi ,
I am just putting a sample of the Lines in a file:

H|820020080422|AAA|03/27/2008|3.0.5
D|99.114|ADLT|1|60.00||60.00|60.00
D|99.683|ADLT|1|28.00||28.00|28.00
P|03/27/2008|CS|KKKK|HHHHH|||70.00
P|03/27/2008|CC|kkk|kkkk|1111||18.00
P|03/27/2008|CS|KKKK|HHHHH|||-28.00

So shell script will read each line and then depending on first character of the line - will insert into different tables.

Thanks

--Abhay


0

Response Number 2
Name: James Boothe
Date: March 28, 2008 at 13:08:16 Pacific
Reply:

Your shell script cannot insert into Oracle tables. SQL*Loader (sqlldr) can read your file and insert into the various tables.


0

Response Number 3
Name: ghostdog
Date: March 30, 2008 at 03:12:30 Pacific
Reply:

if you can download and install gawk from here:
http://gnuwin32.sourceforge.net/pac...

save the below code as script.awk


BEGIN{FS="|";q="\047"}
/^H/{ t=table1}
/^D/{ t=table2}
/^P/{ t=table3}
{
sql="insert " t "values("q$1q","q$2q","q$3q","q$4q","q$5q")"
cmd="sqlplus blah blah " sql
print cmd
# system(cmd) #uncomment when needed.
}

on the command line:


c:\test> gawk -f script.awk inputfile
sqlplus blah blah insert values('H','820020080422','AAA','03/27/2008','3.0.5')
sqlplus blah blah insert values('D','99.114','ADLT','1','60.00')
sqlplus blah blah insert values('D','99.683','ADLT','1','28.00')
sqlplus blah blah insert values('P','03/27/2008','CS','KKKK','HHHHH')
sqlplus blah blah insert values('P','03/27/2008','CC','kkk','kkkk')
sqlplus blah blah insert values('P','03/27/2008','CS','KKKK','HHHHH')

of course, i have created a fictitious command for inserting your tables. you have to modify it your own to use with oracle(?) , accordingly.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


U guys R great! One more ... option to run file that w...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Read first character of each Line

Find length of each line in a file. www.computing.net/answers/unix/find-length-of-each-line-in-a-file/7852.html

How to get rid of spaces at the beginning of each line? www.computing.net/answers/unix/how-to-get-rid-of-spaces-at-the-beginning-of-each-line/1804.html

fmthard command www.computing.net/answers/unix/fmthard-command/4477.html