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
# 8  
Old 06-12-2013
Works perfect.....

Yoda - Thanks a ton for your approach as well...works just perfect solving the escaping quotes....but my #2 requirement to assign a new line '\n' to the last decimal still needs a minor re-tweak....

Code:
record
----
----
decimal('\n') lastCol;
end

~Anduzzi
# 9  
Old 06-12-2013
Quote:
Originally Posted by anduzzi
but my #2 requirement to assign a new line '\n' to the last decimal still needs a minor re-tweak....
Apply this change:
Code:
print "DECIMAL" "(\x27\\n\x27)" OFS A[i]";"

# 10  
Old 06-12-2013
...I just need to apply '\n' ONLY to the last decimal col...this change would apply for all the cols....please check...

Code:
record
----
----
decimal('\n') lastCol;
end

Also one more thing I observed...the last col in listfile is a timestamp defined as

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")

but the array is getting only till the "space" and omitting the timestamp part...which is required...

~Anduzzi
# 11  
Old 06-12-2013
Here is what I get by adding a testfield in the input file:
Code:
$ ./anduzzi.awk
record
string(',') ID;
string(',') Key;
date("YYYY-MM-DD")(',') KeyCreateDate;
string(',') EmailAddress;
date("YYYY-MM-DD")(',') DepDate;
datetime("YYYY-MM-DD(',') EndDate;
DECIMAL('\n') testfield;
end

Isn't that what you get?
This User Gave Thanks to Yoda For This Post:
# 12  
Old 06-12-2013
Sorry...may be I wasn't completely clear on that part....there might be some 20 decimals following...and I need only the last decimal to tag with '\n' to represent end of the record format....so my output should look like this:

Code:
record
string(',') ID;
string(',') Key;
date("YYYY-MM-DD")(',') keyCreateDate;
string(',') EmailAddress;
date("YYYY-MM-DD")(',') DepDate;
datetime("YYYY-MM-DD(',') EndDate;
decimal(',') Q1;
decimal(',') Q2;
decimal(',') Q3;
decimal(',') Q4;
decimal('\n') Q5;
end;

In this case, Q5 is the last col and so the '\n'....Also, please check the EndDate the timestamp part is missing in the output(even in yours)...since there is a space between date and timestamp defined in the listfile.

~Anduzzi
# 13  
Old 06-12-2013
Oh ok. I got it.

Create a comma separated list file instead:
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")

And try this modified code:
Code:
awk -F, '
        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 ( i == n ? L[A[i]] "(\x27\\n\x27)" OFS A[i]";" : L[A[i]] "(\x27,\x27)" OFS A[i]";" )
                        }
                        else
                        {
                                print ( i == n ? "DECIMAL" "(\x27\\n\x27)" OFS A[i]";" : "DECIMAL" "(\x27,\x27)" OFS A[i]";" )
                        }
                }
                print "end"
        }
' listfile file

This User Gave Thanks to Yoda For This Post:
# 14  
Old 06-13-2013
Champ!!!!

Works like a Champ, you rock Yoda!!!!....A Million thanks!

Thanks Corona688 for giving a kickstart to this problem....

~Anduzzi SmilieSmilieSmilie

---------- Post updated 06-13-13 at 02:50 PM ---------- Previous update was 06-12-13 at 05:32 PM ----------

Adding to the Dynamic DML Generation, I have the following scenario basically need to normalize...

Code:
record
string(',') A1;
string(',') A2;
date("YYYY-MM-DD")(',') A3;
string(',') A4;
date("YYYY-MM-DD")(',') A5;
decimal(',') Q1;
decimal(',') Q2;
decimal(',') Q3;
decimal(',') Q4;
decimal(',') Q5;
decimal(',') Q6;
decimal(',') Q7;
decimal('\n') Q8;
end;

----------------
So, Once I have the above DML and the corresponding file...I need to normalize the Questions part and generate the output rows accordingly...Output would look like :

Code:
A1,A2,A3,A4,A5,Q1(Question number),Response(value in the qn number col)
A1,A2,A3,A4,A5,Q2(Question number),Response(value in the qn number col) 
A1,A2,A3,A4,A5,Q3(Question number),Response(value in the qn number col)
A1,A2,A3,A4,A5,Q4(Question number),Response(value in the qn number col) 
A1,A2,A3,A4,A5,Q5(Question number),Response(value in the qn number col)
A1,A2,A3,A4,A5,Q6(Question number),Response(value in the qn number col) 
A1,A2,A3,A4,A5,Q7(Question number),Response(value in the qn number col) 
A1,A2,A3,A4,A5,Q8(Question number),Response(value in the qn number col)

Any efficient way to accomplish this from scripting?.

~Anduzzi

Last edited by radoulov; 06-13-2013 at 05:04 PM..
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