Hello All Members,
I am new to this forum and to the shell scripting. I want to write a shell script to do the following:
Scenario: I have a pipe delimited .txt file with multiple fields in it. The very first row of the file contains the name of the column which resembles the name of the column in the corresponding database table. I am providing the example of the file below:
�Name�|�Dept�|�Empno�|�Salary�|�DOB�|�DOJ�
�Alexander�|�10�|�200�|�$1000�|�25/05/1977�|�01/01/2015�
�Graham�|�11�|�300�|�$1500�|�05/09/1970�|�19/02/2011�
�Bell� |�12�|�400�|�$2000�|�31/01/1965�|�01/10/1999�
Table Structure:
Create table EMPLOYEE (
Name VARCHAR2(255),
Dept NUMBER,
Empno NUMBER,
Salary NUMBER,
DOB DATE,
DOJ DATE);
Requirement: I want to write a script which can read the first row of the text file and create a dynamic insert statement based on the columns available in the file. The reason for this requirement is because:
- I do not know whether same number of columns will arrive in file every time or not. File may contains all the columns or very few of them.
- The order of the column is also not fixed. They could be in random order e.g. File can arrive with
�Dept�|�Empno�|�DOB�|�DOJ�|�Name�|�Salary�
Currently I am using external table but I need to change the table definition every time files arrive in different column order. I cannot force the format of the file on the supplier of this file. So I have to work with this.
PsuedoCode: With my limited knowledge, I think below would be the pseudo code.
- Read the first line of the file.
- Read the columns available in the first line.
- Create insert statement using the columns available in the first line.
- And put the values to the above insert statement from the second row onwards.
I know this will not be a simple script (atleast for me). Hence I am posting this in forum. I would appreciate if any of the senior unix member can help me out with this.
Thanks.