AIX

AIX

Connect with fellow AIX users and experts to gain knowledge, share insights, and solve problems.


#Power
#Power
#Operatingsystems
#Servers
 View Only
  • 1.  Creating files based on template & parameters list

    Posted 02/23/09 07:13 PM

    Originally posted by: Tim_at_Boeing


    I am new at AIX/UNIX and need to build a set of output files based on a template file and a list of parameters. I've done this in other languages but not in UNIX. We are at AIX/UNIX Version 5.3.

    I assume with a loop of some kind but I'm not sure how to do edits and output within loops.

    Read record 1 from parameter.txt file. Store field2 and field3. Edit sample.sql file replacing the question marks with the vaule of field2. Then output the results into a new file called sample_one.sql (using value of field2) and quit sample.sql.
    Read record 2 from parameter.txt file. Store field2 and field3. Edit sample.sql file replacing the question marks with the vaule of field2. Then output the results into a new file called sample_two.sql (using value of field2) and quit sample.sql.
    Read record 3 from parameter.txt file. Store field2 and field3. Edit sample.sql file replacing the question marks with the vaule of field2. Then output the results into a new file called sample_eleven.sql (using value of field2) and quit sample.sql.

    === Template file ===

    sample.sql
    SELECT F1,F2,F3
    FROM TABLE1
    WHERE F1 = ?

    === Parameter file ===

    parameter.txt
    01~1~one~
    02~2~two~
    11~11~eleven~

    === Output files ===

    sample_one.sql
    SELECT F1,F2,F3
    FROM TABLE1
    WHERE F1 = 1

    sample_two.sql
    SELECT F1,F2,F3
    FROM TABLE1
    WHERE F1 = 2

    sample_eleven.sql
    SELECT F1,F2,F3
    FROM TABLE1
    WHERE F1 = 11
    #AIX-Forum


  • 2.  Re: Creating files based on template & parameters list

    Posted 02/24/09 03:48 AM

    Originally posted by: grukrz1


    Is that what you want? :

    $ ls
    parameter.txt sample.sql
    $ cat parameter.txt
    01~1~one~
    02~2~two~
    11~11~eleven~
    $ cat sample.sql
    SELECT F1,F2,F3
    FROM TABLE1
    WHERE F1 = ?
    $ sed s/\~/\ /g parameter.txt|while read a b c;do sed "s/\?/$b/g" sample.sql >sample_${c}.sql;done
    $ ls
    parameter.txt sample.sql sample_eleven.sql sample_one.sql sample_two.sql
    $ cat sample_eleven.sql
    SELECT F1,F2,F3
    FROM TABLE1
    WHERE F1 = 11
    $ cat sample_one.sql
    SELECT F1,F2,F3
    FROM TABLE1
    WHERE F1 = 1
    $ cat sample_two.sql
    SELECT F1,F2,F3
    FROM TABLE1
    WHERE F1 = 2
    $
    #AIX-Forum


  • 3.  Re: Creating files based on template & parameters list

    Posted 02/25/09 04:44 PM

    Originally posted by: Tim_at_Boeing


    grukrz1,

    That seems to do exactly what I want EXCEPT it doesn't seem to process the last record in the parameter file. When I added an additional record for 12 in the parameter file then it created the "sample_eleven.sql" file but not the "sample_twelve.sql" file.

    Thank you so very much for your help on this.
    #AIX-Forum


  • 4.  Re: Creating files based on template & parameters list

    Posted 02/25/09 05:42 PM

    Originally posted by: Tim_at_Boeing


    grukrz1,

    Should I just concatinate a blank record to the end of my parameter.txt file? That is one thing I can do that wouldn't require changing your suggested solution.
    #AIX-Forum


  • 5.  Re: Creating files based on template & parameters list

    Posted 02/26/09 11:39 AM

    Originally posted by: Tim_at_Boeing


    I found if I add a dummy line containing a single period to the end of the parameter.txt file then the solution shown above works great.

    $ echo "." > tmpfile.txt
    $ cat tmpfile.txt >> parameter.txt
    $ sed s/\~/\ /g parameter.txt|while read a b c;do sed "s/\?/$b/g" sample.sql >sample_${c}.sql;done

    Thank you so much for your help grukrz1.
    #AIX-Forum