Results 1 to 7 of 7

Thread: automating task

  1. #1
    Join Date
    Feb 2004
    Posts
    7

    Question Unanswered: automating task

    hello,
    I am using ETL tool informatica and informatica server reads the values of variables stored in the text file in a directory.

    Problem: I have an employee table and has 4 departments namely 10,20,30,40. I have to write a shell script in such way that every time i run the shell script it sud update the text file which informatica server uses in doin its ETL task.

    ---------------------------------------------
    TEXT FILE CONTENTS:
    [FOLDERNAME.SESSIONNAME]
    $$DEPTNO=10
    ----------------------------------------------
    The script file should update the value of $$DEPTNO each time it is run and it sud update in sequence....10 first time...next time i run the script it sud be 20...so on until it is 40 and it sud loop back to 10 and start the cycle ...i mean every time it is run.

    I am also supposed to create a backup files in the same directory for every value of $$deptno used.

    For example :
    bkp1.txt 10
    bkp2.txt 20
    ......
    Could any one help me in this regard.

    Thanks,
    ashton

  2. #2
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    Adapt and try this script :

    Code:
    data_file=deptno.txt
    work_file=${data_file}.$$
    
    old_deptno=$(awk -v FILE="$work_file" '
            BEGIN {
              FS  = "="; OFS = "=";
            }
            $1 == "$$DEPTNO" {
              print $2;
              $2 = $2 % 40 + 10
            }
            {
              print $0 >> FILE
            }
            ' deptno.txt )
    
    cp $data_file bkp${old_deptno}.txt
    mv $work_file $data_file
    The file is backuped before modification : bkp<deptno>.txt
    Jean-Pierre.

  3. #3
    Join Date
    Feb 2004
    Posts
    7

    Question

    hi,
    thanks for the reply.as i am a beginner cud u xplain the following code

    old_deptno=$(awk -v FILE="$work_file" '
    BEGIN {
    FS = "="; OFS = "=";
    }
    $1 == "$$DEPTNO" {
    print $2;
    $2 = $2 % 40 + 10
    }
    {
    print $0 >> FILE
    }
    ' deptno.txt )
    thanks,
    ashton

    Originally posted by aigles
    Adapt and try this script :

    Code:
    data_file=deptno.txt
    work_file=${data_file}.$$
    
    old_deptno=$(awk -v FILE="$work_file" '
            BEGIN {
              FS  = "="; OFS = "=";
            }
            $1 == "$$DEPTNO" {
              print $2;
              $2 = $2 % 40 + 10
            }
            {
              print $0 >> FILE
            }
            ' deptno.txt )
    
    cp $data_file bkp${old_deptno}.txt
    mv $work_file $data_file
    The file is backuped before modification : bkp<deptno>.txt

  4. #4
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    Code:
    old_deptno=$(awk -v FILE="$work_file" '
    	BEGIN {
    	   FS = "="; OFS = "=";
    	}
    	$1 == "$$DEPTNO" {
    	   print $2;
    	   $2 = $2 % 40 + 10
    	}
    	{
    	   print $0 >> FILE
    	}
    	' deptno.txt )
    old_deptno=$(awk ..... )
    Set the variable old_deptno to the result (stdout) of the awk command.

    awk -v FILE="$work_file" ' ..... ' deptno.txt
    Awk set an awk variable FILE and execute the inline script with deptno as input_file.
    FILE is the name of the file where to write result of script.
    The old value of $$DEPTNO will be written to stdout.

    BEGIN { FS="=" ; OFS="=" }
    Awk initialization, the code of the BEGIN pattern is excuted before opening the first file.

    FS="=" set the internal FIELD SEPARATOR to "=", records will be split in fields that are delimited by FS.
    $$DEPTNO=10 gives $1=$$DEPTNO and $2=10

    OFS="=" set the internal OUTPUT FIELD SEPARATOR to "=", fields will be written with "=" as separator.
    For example: print "aaa","bbb" will be printed as 'aaa=bbb'

    $1 == "$$DEPTNO" { ..... }
    The line select input records where field $1 is equal to "$$DEPTNO" and specify the code to execute for that record.

    print $2;
    Write to stdout the old value of $$DEPTNO

    $2 = $2 % 40 + 10
    Set the new value for $$DEPTNO, 10->20 20->30 30->40 40->10
    The record ($0) is now '$$DEPTNO=new_value'

    { print $0 >> FILE }
    This code is executed for every input record (no pattern specified).
    The current record ($0) is written (append) to the file which name is in variable FILE.
    If the record is the '$$DEPTNO' record, the new value had already be set by the previous pattern/action code.


    Hope this help, like said PHV ...
    Jean-Pierre.

  5. #5
    Join Date
    Feb 2004
    Posts
    7
    thankx a lot it it helps.........

  6. #6
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    This is another solution which use 'sed' instead of 'awk'

    Code:
    #
    # Data files
    #
    data_file=deptno.txt
    work_file=${data_file}.$$
    #
    # Get current deptno value and backup file
    #
    deptno=`sed -n '/$$DEPTNO=/{;s/.*=\([0-9]*\).*$/\1/;p;}' $data_file`
    cp deptno.txt bkp${deptno}.txt
    #
    # Compute new deptno value and replace in file
    #
    deptno=`expr $deptno % 40 + 10`
    sed -e 's/^$$DEPTNO=.*/$$DEPTNO='$deptno'/' $data_file > $work_file
    mv $work_file $data_file

    Have a look to thread need help in modifying the contents of a text file
    Homework ?
    Jean-Pierre.

  7. #7
    Join Date
    Feb 2004
    Posts
    7
    I guess this helps a lot as i am familier with sed command
    Thanks a lot



    Originally posted by aigles
    This is another solution which use 'sed' instead of 'awk'

    Code:
    #
    # Data files
    #
    data_file=deptno.txt
    work_file=${data_file}.$$
    #
    # Get current deptno value and backup file
    #
    deptno=`sed -n '/$$DEPTNO=/{;s/.*=\([0-9]*\).*$/\1/;p;}' $data_file`
    cp deptno.txt bkp${deptno}.txt
    #
    # Compute new deptno value and replace in file
    #
    deptno=`expr $deptno % 40 + 10`
    sed -e 's/^$$DEPTNO=.*/$$DEPTNO='$deptno'/' $data_file > $work_file
    mv $work_file $data_file

    Have a look to thread need help in modifying the contents of a text file
    Homework ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •