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

Read first character of each Line

Reply to Message Icon

Original Message
Name: abhayg4
Date: March 28, 2008 at 11:57:44 Pacific
Subject: Read first character of each Line
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


Report Offensive Message For Removal


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

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


Report Offensive Follow Up For Removal

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

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


Report Offensive Follow Up For Removal

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

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.


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Read first character of each Line

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge