Generate a DML dynamically based off of header record


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Generate a DML dynamically based off of header record
# 1  
Old 06-12-2013
Generate a DML dynamically based off of header record

I have the following scenario where I need to use a header record from a file and generate a DML based off of it...

E.g.: The header can change periodically with an additional column in between or remove a col....

Sample header : (head -1 sample.txt)
ID,Key,KeyCreateDate,EmailAddress,DepDate, EndDate

And DML should be look like:

Code:
record 
string(',') ID; 
string(',') Key; 
date("YYYY-MM-DD")(',') KeyCreateDate; 
string(',') EmailAddress; 
date("YYYY-MM-DD")(',') DepDate; 
datetime("YYYY-MM-DD HH24:MI:SS")(',') EndDate; 
end

Appreciate your help.

~Anduzzi
# 2  
Old 06-12-2013
How is the column type picked?
# 3  
Old 06-12-2013
Col types...

That's the least complex part...the first few cols are fixed just as I mentioned and rest of the additions are mostly decimal...

~Anduzzi
# 4  
Old 06-12-2013
Least complex to you, because you know the rules and I don't... "mostly" decimal is guaranteed to guess wrong if I guess all decimal.

Does it go by name? I hope it goes by name. You can make a list of defaults.

Code:
ID%string(',')
Key%string(',')
KeyCreateDate%date("YYYY-MM-DD")(',')
EmailAddress%string(',')
DepDate%date("YYYY-MM-DD")(',')
EndDate%datetime("YYYY-MM-DD HH24:MI:SS")(',')

Code:
awk 'BEGIN {
FS="%"
while( (getline < "listfile") > 0) A[$1]=$2;
FS=","
}

{
        print "record";
        for(N=1; N<=NF; N++)
        {
                if($N in A)
                        print A[N], $N, ";";
                else  print "DECIMAL", $N, ";";
        }
        print "end";
        
}' inputfile

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 06-12-2013
This piece of code 'almost' works great for my purpose....thanks!

Almost coz, couple of things I need to make it fit the puzzle...

1.Escape the quotes in (',') in the decimal part...output should be like decimal (',') instead of decimal (,)

2.As soon as the code encounters last col, the decimal should go with ('\n') instead of (',')

Thanks again!

Code:
awk 'BEGIN {
FS="%"
while( (getline < "listfile") > 0) A[$1]=$2;
FS=","
}

{
  print "record";
        for(N=1; N<=NF; N++)
        {
                if($N in A)
                        print A[$N], $N, ";";
                else    print "decimal (',')", $N, ";";
        }
        print "end";

}' inputfile

# 6  
Old 06-12-2013
Here is another approach:

Input file with list of datatype:
Code:
$ cat listfile
ID              string
Key             string
KeyCreateDate   date("YYYY-MM-DD")
EmailAddress    string
DepDate         date("YYYY-MM-DD")
EndDate         datetime("YYYY-MM-DD HH24:MI:SS")

Input file with the list of column:
Code:
$ cat file
ID,Key,KeyCreateDate,EmailAddress,DepDate, EndDate

Here is awk program:
Code:
awk '
        NR == FNR {
                L[$1] = $2
                next
        }
        {
                print "record"
                n = split ( $0, A, "," )
                for ( i = 1; i <= n; i++ )
                {
                        sub (/[ \t]+/, X, A[i])
                        if ( A[i] in L )
                        {
                                print L[A[i]] "(\x27,\x27)" OFS A[i]";"
                        }
                        else
                        {
                                print "DECIMAL" "(\x27,\x27)" OFS A[i]";"
                        }
                }
                print "end"
        }
' listfile file

This User Gave Thanks to Yoda For This Post:
# 7  
Old 06-12-2013
There's a reason I used % as a delimiter in my list file; some of those items have spaces in them, $2 won't be the entire type.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to print multiple required columns dynamically in a file using the header name?

Hi All, i am trying to print required multiple columns dynamically from a fie. But i am able to print only one column at a time. i am new to shell script, please help me on this issue. i am using below script awk -v COLT=$1 ' NR==1 { for (i=1; i<=NF; i++) { ... (2 Replies)
Discussion started by: balu1234
2 Replies

2. Shell Programming and Scripting

Parameterizing to dynamically generate the extract file from Oracle table using Shell Script

I have below 2 requirements for parameterize the generate the extract file from Oracle table using Shell Script. Could you please help me by modifying the script and show me how to execute it. First Requirement: I have a requirement where I need to parameterize to generate one... (0 Replies)
Discussion started by: hareshvikram
0 Replies

3. UNIX for Beginners Questions & Answers

Help in printing records where there is a 'header' in the first record ???

Hi, I have a backup report that unfortunately has some kind of hanging indent thing where the first line contains one column more than the others I managed to get the output that I wanted using awk, but just wanting to know if there is short way of doing it using the same awk Below is what... (2 Replies)
Discussion started by: newbie_01
2 Replies

4. Shell Programming and Scripting

Generate class path dynamically based on source path

Hi experts, I have multiple file names ending with .jsp located in $SOME_DIR, $SOME_DIR/f1/,$SOME_DIR/f2/test,$SOME_DIR/f3/fa and there are equivalent class files in $SOME_DIR/WEB-INF/classes/_pages,$SOME_DIR/WEB-INF/classes/_pages/_f1,... (0 Replies)
Discussion started by: oraclermanpt
0 Replies

5. UNIX for Dummies Questions & Answers

Dynamically merging 2 files on header values

Hi All, I have 2 files which i need to merge together based on the column names provided in the file. The first line in both files are header records. The first file has fixed columns but second file can have subset of the columns from file 1 File 1: ... (6 Replies)
Discussion started by: kushagra
6 Replies

6. Shell Programming and Scripting

Approach on Header record

All, I currently have a requirement to fetch a Date value from a table. And then insert a Header record into a file along with that date value. ex: echo "HDR"" "`date +%Y%j` `date +%Y%m%d` In the above example I used julian date and standard date using Current Date. But the requirement... (0 Replies)
Discussion started by: cmaroju
0 Replies

7. Shell Programming and Scripting

Insertion of Header record

A header record is to be inserted in the begining of a flat file without using extra file or new file. It should be inserted into same file. Advace thanks for all help... (7 Replies)
Discussion started by: shreekrishnagd
7 Replies

8. Shell Programming and Scripting

Skip parsing the header record - Awk

Guys.... Got a scenario in which I need to skip parsing the header record while I do an awk. Does awk has the flexibility to accomplish this?. If so, how do we do this?. Thanks !!! -Anduzzi :) (2 Replies)
Discussion started by: anduzzi
2 Replies

9. Shell Programming and Scripting

awk script to update header record

I am using HP UX and think this may be done with awk but bot sure. I have a file with a several header records and undeneath many detail records I need to put in the header record the number of detail records above this header record and number of detail records below this header record Header... (5 Replies)
Discussion started by: klut
5 Replies

10. UNIX for Dummies Questions & Answers

How to extract duplicate records with associated header record

All, I have a task to search through several hundred files and extract duplicate detail records and keep them grouped with their header record. If no duplicate detail record exists, don't pull the header. For example, an input file could look like this: input.txt HA D1 D2 D2 D3 D4 D4... (17 Replies)
Discussion started by: run_eim
17 Replies
Login or Register to Ask a Question