If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > automating task

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-26-04, 18:41
ramuk_ramuk2003 ramuk_ramuk2003 is offline
Registered User
 
Join Date: Feb 2004
Posts: 7
Question 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
Reply With Quote
  #2 (permalink)  
Old 02-27-04, 03:13
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
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.
Reply With Quote
  #3 (permalink)  
Old 02-27-04, 10:34
ramuk_ramuk2003 ramuk_ramuk2003 is offline
Registered User
 
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

Quote:
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
Reply With Quote
  #4 (permalink)  
Old 02-27-04, 12:04
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
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.
Reply With Quote
  #5 (permalink)  
Old 02-27-04, 23:42
ramuk_ramuk2003 ramuk_ramuk2003 is offline
Registered User
 
Join Date: Feb 2004
Posts: 7
thankx a lot it it helps.........
Reply With Quote
  #6 (permalink)  
Old 02-28-04, 06:19
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
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.
Reply With Quote
  #7 (permalink)  
Old 02-28-04, 11:03
ramuk_ramuk2003 ramuk_ramuk2003 is offline
Registered User
 
Join Date: Feb 2004
Posts: 7
I guess this helps a lot as i am familier with sed command
Thanks a lot



Quote:
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 ?
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On