Possible to insert a few lines of code into a file at a specific point?

Hi Folks -

How would I go about inserting a chunk of lines (3) into a specific portion of a file?

The background is I have a script (non shell) that it executed daily, however on Sundays, I uncomment a section of code so that piece can be run as well.

So I was hoping to write a piece of code to insert the 3 lines into this particular file every Sunday, and remove when complete.

Is this possible?

The lines are as follows:

execute calculation  'lawson'.'cslaw_r'.'aggasc';

export database 'dailyinv'.'dailyinv' using server report_file 'oracle' to data_file '/hypbin/Oracle/Middleware/user_projects/epmsystem1/EssbaseServer/essbaseserver1/app/new_inv/new_inv/oracle1.txt';

export database 'lawson'.'cslaw_r' using server report_file 'oracle' to data_file '/hypbin/Oracle/Middleware/user_projects/epmsystem1/EssbaseServer/essbaseserver1/app/new_inv/new_inv/oracle2.txt';

Can I put two commented out identifiers in the script that says place code between these? and then delete lines between these when finished?

Thanks!

So, if your file isn't a shell script, what is used to interpret your file?

1 Like

Hi Don -

Good question - I should have clarified that.

My shell script is is what will drive the automation, but the actual file that gets executed is an .mxl file which is read by the application that it's getting executed against.

Its an oracle product (Essbase).

For example, my shell script would contain:

. "${_STARTMAXLPATH}startMaxl.sh" "${_MAXLSCRIPTPATH}AC.mxl" ${_ESSB_USER} ${_ESSB_PSWD} ${_ESSB_SRVR} ${_MAXLLOGFILE}

Can the Essbase application read AC.mxl (or should that be AC.xml ) from standard input or does it have to be given the pathname of the file containing the script as an operand?

Does the scripting language read by Essbase have a comment statement? If so what is its format?

What operating system are you using?

What shell are you using?

1 Like

Hi Don,

Essbase reads ".mxl" (not .xml) and it's passed to the Essbase application using the startMaxl.sh utility and corresponding path and file name of the maxl (as shown above).

A comment is created by the following:

/* Comment */ 

For this task, I am coding on an AIX environment using .ksh.

I was thinking something like this:

SundayTasks() {

    if [[ "$1" == "Add_Sun" ]]
    then
    sed -i '/cdef/r execute calculation  'lawson'.'cslaw_r'.'aggasc'; \
            export database 'dailyinv'.'dailyinv' using server report_file 'oracle' to data_file '/hypbin/Oracle/Middleware/user_projects/epmsystem1/EssbaseServer/essbaseserver1/app/new_inv/new_inv/oracle1.txt'; \
            export database 'lawson'.'cslaw_r' using server report_file 'oracle' to data_file '/hypbin/Oracle/Middleware/user_projects/epmsystem1/EssbaseServer/essbaseserver1/app/new_inv/new_inv/oracle2.txt';' Oracle_PEL.mxl
    elif [[ "$1" == "Del_Sun" ]]
    sed -i '/\/*Start*\//,/\/*End*\//{//!d}' Oracle_PEL.mxl
    fi
}

Where my .mxl would have the following comments:

/*Start*/

/*End*/

This is untested as I need to step out for a bit, but thats what i'm thinking.

Thank you!

Thank you very much - let me know if you need an

I would tend to leave the code in the file where it will be executed, but have it commented out on the days when it shouldn't run...

	/*Start Sunday Only*/
	execute calculation  'lawson'.'cslaw_r'.'aggasc'; \
	export database 'dailyinv'.'dailyinv' using server report_file 'oracle' to data_file '/hypbin/Oracle/Middleware/user_projects/epmsystem1/EssbaseServer/essbaseserver1/app/new_inv/new_inv/oracle1.txt'; \
	export database 'lawson'.'cslaw_r' using server report_file 'oracle' to data_file '/hypbin/Oracle/Middleware/user_projects/epmsystem1/EssbaseServer/essbaseserver1/app/new_inv/new_inv/oracle2.txt';' Oracle_PEL.mxl
	/*End Sunday Only*/

Have your script look for the strings (Start|End) Sunday Only and if it isn't Sunday, remove the comment terminator and start characters shown in red. If it is Sunday, insert the characters shown in red if they aren't already there. That way, anyone looking at the file will know what it will be doing any day of the week without having to know about a magic editing session that adds invisible code one day of the week.

1 Like

I appreciate that I'm coming to this thread very late but, having read it a few times, isn't there a better way than dynamically editing a script (or whatever you want to call it)?

If the 3 lines are always the same code (ie, they don't contain moving date information or anything like that) why can't you have two copies (different files) of the script, one for weekdays and one for Sundays? Then test for day of week at start of script and run the weekday or Sunday script as appropriate. Even if the file has to be a certain name you could rename files before execution on a Sunday and rename them back on completion.

Or have I completely missed some point here???

Does your application scripting allow for "include" files? Then on Sundays you could rename one on shell level.

Hi Rudi -

I'm not sure what you mean by include files? Please excuse my ignorance.

@Don -

I like that approach much better as there will be people other than myself interacting with this process so the less headaches the better for me as I'm sure folks would be confused by that 'ghost' process.

Here is what I arrived on :

SundayTasks() {

cd /hypbin/test

    if [[ "$1" == "Run_Sun" ]]
    then
        sed 's/\/\*Start Sunday Only/\/\*Start Sunday Only\*\//g; s/End Sunday Only\*\//\/\*End Sunday Only\*\//g' Oracle_PEL.mxl > Oracle_PEL_temp.mxl
        rm -f Oracle_PEL.mxl
        mv "Oracle_PEL_temp.mxl" "Oracle_PEL.mxl"
        chmod 777 "Oracle_PEL.mxl"
    elif [[ "$1" == "Del_Sun" ]]
    then
        sed 's/\/\*Start Sunday Only\*\//\/\*Start Sunday Only/g; s/\/\*End Sunday Only\*\//End Sunday Only\*\//g' Oracle_PEL.mxl > Oracle_PEL_temp.mxl
        rm -f Oracle_PEL.mxl
        mv "Oracle_PEL_temp.mxl" "Oracle_PEL.mxl"
        chmod 777 "Oracle_PEL.mxl"
    fi

}

Working as expected - thank you again for all your help!

---------- Post updated at 07:42 AM ---------- Previous update was at 07:38 AM ----------

I understand your train of thought here, however I'd prefer not to have unnecessary files out there if I can help it.

The reason that I don't like having a "Sunday" file and a "weekday" file is that eventually some future code maintainer is going to need to update the code and will end up updating only one of the two files when they need to be kept in sync.

In the C programming language, you can use:

#include <header_name>

to include the contents of the header named header_name in your C source code at that point. In the shell, you can use:

. file_name

to run the commands include in the file named file_name at that point in your script. If the language used in .mxl files has a language feature that allows you to include other .mxl files in a script, you could have Oracle_PEL.mxl include the Sunday code commands from a separate file. On Sunday, you could give it the name of the file containing .mxl code to be run on Sundays and on other days you could include /dev/null .

Your code is a little bit dangerous. If the Sunday portion of your script is run twice, you can end up with invalid comments that look like:

/*Start Sunday Only*/*/

Instead of editing the actual script file, you might want to create a template file that looks like what should be run on days other than Sunday (let's call it Oracle_PEL.mxl.template ) and change your code to something like:

    if [[ "$1" == 'Run_Sun' ]]
    then
        sed -e 'sX/\*Start Sunday OnlyX/*Start Sunday Only*/Xg' \
            -e 'sXEnd Sunday Only\*/X/*End Sunday Only*/Xg' \
            Oracle_PEL.mxl.template > Oracle_PEL.mxl
    elif [[ "$1" == 'Del_Sun' ]]
    then
        cp Oracle_PEL.mxl.template Oracle_PE.mxl
    fi