Page 1 of 1

Problem Using Sed via Routine Activity

Posted: Wed Sep 22, 2010 12:49 am
by richieRich
Hi All,

I am having a strange problem whilst trying to use Sed via a Routine Activity Stage in my sequence job. I am doing a global replace of multiple patterns. Sed is working to replace text but as soon as I start to try and replace numbers it is getting totally confused. I assume this is something to do with the translation of my numbers from DataStage to unix because when I run a number replace directly in my unix prompt i don't have any problems....

My source file has one word in it...
BUSINESS_DATE_YYYYMMDD

Here is an example of what happens...

Example 1. Working
------------------------------
Routine Activity Code

"sed -e ":CHAR(34):" s/\'/\'/g;" :
"s/\</\</g;" :
"s/\>/\>/g;" :
"s/\BUSINESS_DATE_YYYYMMDD/\": "Test" : "/g" :
CHAR(34): " " :
<<SourceFile>> : ">> /tmp/out.txt && " :
"rm ": <<SourceFile>> && ":
"mv /tmp/out.txt": " " : <<SourceFile>>

Resultant File
Test

Example 2. Working
------------------------------
Routine Activity Code

"sed -e ":CHAR(34):" s/\&apos;/\'/g;" :
"s/\</\</g;" :
"s/\>/\>/g;" :
"s/\BUSINESS_DATE_YYYYMMDD/\": "#BUSINESS_DATE#" : "/g" :
CHAR(34): " " :
<<SourceFile>> : ">> /tmp/out.txt && " :
"rm ": <<SourceFile>> && ":
"mv /tmp/out.txt": " " : <<SourceFile>>

Resultant File
#BUSINESS_DATE#

Example 3. Not Working
------------------------------
Routine Activity Code

"sed -e ":CHAR(34):" s/\&apos;/\'/g;" :
"s/\</\</g;" :
"s/\>/\>/g;" :
"s/\BUSINESS_DATE_YYYYMMDD/\": "20100908" : "/g" :
CHAR(34): " " :
<<SourceFile>> : ">> /tmp/out.txt && " :
"rm ": <<SourceFile>> && ":
"mv /tmp/out.txt": " " : <<SourceFile>>

Resultant File
BUSINESS_DATE_YYYYMMDD


Yet when I try and do the same thing on the command line it works...
sed 's/BUSINESS_DATE_YYYYMMDD/20100908/g' <<SourceFile>>

Resultant File
20100908


Has anyone got any ideas?

Thanking in advance

Posted: Wed Sep 22, 2010 12:54 am
by ray.wurlod
You should not be using a Routine activity; you should be using an Execute Command activity. sed is not a routine, it is a command.

Posted: Wed Sep 22, 2010 6:43 pm
by richieRich
Thanks Ray. That way the hint I needed. I've got it to work now. The only thing I'm confused about is how to run a command straight after my sed.

In my routine activity I could do a sed, redirect the output to /tmp then do a && and then write another command to overwrite my original file with the redirected output from /tmp


eg. sed command /dir/sourcefile > /tmp/file && mv /tmp/file /dir/sourcefile

Is there a way I can run a command straight after sed to overwrite my original file using the execute command activity. Or do I need another execute command stage?

Thanks

Posted: Wed Sep 22, 2010 7:05 pm
by anbu
Use these commands in single Execute command activity

Code: Select all

sed command /dir/sourcefile > /tmp/file && mv /tmp/file /dir/sourcefile 
Or

Code: Select all

sed command /dir/sourcefile > /tmp/file;mv /tmp/file /dir/sourcefile 

Posted: Wed Sep 22, 2010 7:13 pm
by chulett
In other words, just like you thought. :wink:

Posted: Thu Sep 23, 2010 5:11 am
by richieRich
Yep Thanks guys....

Command=sed -e
Parameters=
"s/\&apos;/\'/g;s/\</\</g;s/\>/\>/g;s/DATE_YYYYMMDD/#DATE#/g" MyFile.txt > /tmp/out.txt && rm MyFile.txt && mv /tmp/out.txt MyFile.txt

It was just confusing seeing the Command as Sed -e. I wasn't sure whether that Execute Command activity was limited it to sed.