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

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Possible to insert a few lines of code into a file at a specific point?
# 1  
Old 10-20-2017
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:
Code:
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!
# 2  
Old 10-20-2017
So, if your file isn't a shell script, what is used to interpret your file?
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 10-20-2017
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:

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

# 4  
Old 10-20-2017
Quote:
Originally Posted by SIMMS7400
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:

Code:
. "${_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?
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 10-20-2017
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:

Code:
/* Comment */

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

I was thinking something like this:

Code:
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:

Code:
/*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
# 6  
Old 10-21-2017
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...

Code:
	/*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.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 10-21-2017
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???
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert charactera in 1st position of specific lines using vi editor or sed command

Dear all, i am having text file like below surya rama ranga laxman rajesh reddy i want add string (OK) before a text from line 3 to 5 the result will be surya rama OK ranga OK laxman OK rajesh reddy (1 Reply)
Discussion started by: suryanarayana
1 Replies

2. Solaris

Insert a file at specific line

Hi, Anyone can help me in Solaris command on how to insert a file at specific line. I want file1.sql content to be inserted on file2.sh after "recover database using backup controlfile until cancel". # file1.sql /archivelogs/927_822338133.arc /archivelogs/671_822338107.arc... (3 Replies)
Discussion started by: fspalero
3 Replies

3. UNIX for Dummies Questions & Answers

Quick UNIX command to display specific lines in the middle of a file from/to specific word

This could be a really dummy question. I have a log text file. What unix command to extract line from specific string to another specific string. Is it something similar to?: more +/"string" file_name Thanks (4 Replies)
Discussion started by: aku
4 Replies

4. UNIX for Dummies Questions & Answers

Join Lines at a specific point

Hi I'm a beginner, and i've been having trouble joining two lines. I need to convert this file 1097ALABAMA Mobile County METHOMYL INSE CTICIDES 6 1.6200000E+00 1.8000001E+00 1003ALABAMA Baldwin County ... (5 Replies)
Discussion started by: kf_1434
5 Replies

5. Shell Programming and Scripting

how to Insert values in multiple lines(records) within a pipe delimited text file in specific cols

this is Korn shell unix. The scenario is I have a pipe delimited text file which needs to be customized. say for example,I have a pipe delimited text file with 15 columns(| delimited) and 200 rows. currently the 11th and 12th column has null values for all the records(there are other null columns... (4 Replies)
Discussion started by: vasan2815
4 Replies

6. Shell Programming and Scripting

insert data into specific lines of a CSV

So I work in a 1 to 1 laptop deployment and sometimes we need to mass order parts. The vendor will send us a text file and we have to manually input serial numbers. Well I have a full blown web based inventory system which I can pull serial number reports from. I then have to input the part... (4 Replies)
Discussion started by: tlarkin
4 Replies

7. Shell Programming and Scripting

Insert character in a specific position of a file

Hi, I need to add Pipe (|) at 5th and 18th position of all records a file. How can I do this? I tried to add it at 5th position using the below code. It didnt work. Please help!!! awk '{substr($0,5,1) ~ /|/}{print}' $input_file > $temp_file (1 Reply)
Discussion started by: gpaulose
1 Replies

8. Shell Programming and Scripting

Search for a pattern in a file and print previous lines from a particular point

Hi, I am new to ksh scripting and I have a problem. I have a file in which I have to search for a particular pattern say 'a' then from that line I need to search for another pattern say 'b' in the previous lines and thne print the file from pattern 'b' till the end of file. For eg: ... (2 Replies)
Discussion started by: umaislearning
2 Replies

9. Shell Programming and Scripting

Insert 2 lines in a file at a specific location

Hi, I need to insert two new lines in a file: The file: "..... ...... ULIMIT_MAX_FILES="ulimit -S -n `ulimit -H -n`" .... .... " I need to add the lines: LD_LIBRARY_PATH='$LD_LIBRARY_PATH:$APACHE_HOME/modules' DOWNLOADMODULE_CONF_PATHNAME='$APACHE_HOME/conf/DWLModule.cfg' right... (2 Replies)
Discussion started by: potro
2 Replies

10. Shell Programming and Scripting

Insert lines at specific location in file

Hi There I have this file that I would like to add entries to, however, there is a "}" as the last line that I need to keep. Basically i would like to know how I can write a script that will add new lines at the second to last line position (ie always add new line above the close bracket) ... (17 Replies)
Discussion started by: hcclnoodles
17 Replies
Login or Register to Ask a Question