-2

I am writing a script that uses the sed command to replace in a file the specific identifier. Currently the script has the sed commands hard coded

Ex Current Method:

s*ed 's/@Number@/ '  "${Number}" '/g; s/@Value@/ ' "${Value}" '/g; 
        s/@Socket@/ ' "${Socket}" '/g' File_Template > index.html*

Due to the fact that the sed command will not be hard coded anymore and we are running a lot of processes with different sed requirements, I will like to do something like this

Goal:

INITIALIZE_SED_COMMAND='s/@Number@/ ' "${Number}" '/g;
        s/@Value@/ ' "${Value}" '/g; 
        s/@Socket@/ ' "${Socket}" '/g;'
# Some logic happens here
INITIALIZE_SED_COMMAND="$INITIALIZE_SED_COMMAND File_Template > index.html"  ;

sed $INITIALIZE_SED_COMMAND

This worked in a Linux platform as a .sh script but I need to do it in .ksh unixand I keep getting cannot parse through.

ERROR:

sed: Cannot find or open file s/@Number@/npdsfg/g;.

sed: Cannot find or open file s/@Value@/SQR.

sed: Cannot find or open file #s/@Socket/semi_/g;.

closed as unclear what you're asking by jasonwryan, JRFerguson, Kusalananda, DarkHeart, phk May 23 '17 at 8:19

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • sed function cannot parse – user232550 May 22 '17 at 21:19
  • I just want to be able to keep the substitution of the sed command in a variable and when finish adding to the variable , execute the sed command with the variable holding the string – user232550 May 22 '17 at 21:32
  • sed $variable -> where variable= all the fancy substitutions ; – user232550 May 22 '17 at 21:33
0

This isn't a consequence of moving from a Linux-based system to some non-Linux based system (AIX, perchance?). It's a fundamental misunderstanding of how the shell parses and executes commands.

You cannot write this and expect it to write a timestamp to a file:

v="date > /tmp/date.txt"
$v

Similarly you cannot write this and have it perform substitutions and write the results to an output file

v="s/abc/XYZ/ /tmp/source > /tmp/destination"
sed $v

The file redirection cannot not be specified within the variable.

What I think you're looking for is sed -f, where you can create a file of substitutions and have them applied to a source datafile:

#!/bin/ksh
# Usage: thisprogram  <transform_file>  <src_file>  <dst_file>
#
transform="$1"
src="$1"
dst="$2"

sed -f "$transform" "$src" >"$dst"

The transform file might contain operations like these:

s/@Number@/1234/g
s/@Value@/somevalue/g
s/@Socket@/99b44/g

Obviously you'd have to prepare this within the context of the particular execution.

With more detail in your question it might be possible to take this solution further, but I trust you have enough information here to rework it for your particular use case.

  • Thank You Mate, your explanation was marvelous.Cheers mates and have a wonderful day all – user232550 May 23 '17 at 1:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.