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?
# 8  
Old 10-21-2017
Does your application scripting allow for "include" files? Then on Sundays you could rename one on shell level.
# 9  
Old 10-21-2017
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 :

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

Quote:
Originally Posted by hicksd8
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???
I understand your train of thought here, however I'd prefer not to have unnecessary files out there if I can help it.
# 10  
Old 10-21-2017
Quote:
Originally Posted by hicksd8
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???
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.
Quote:
Originally Posted by SIMMS7400
Hi Rudi -

I'm not sure what you mean by include files? Please excuse my ignorance.
In the C programming language, you can use:
Code:
#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:
Code:
. 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.

Quote:
@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 :

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

... ... ...
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:
Code:
/*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:
Code:
    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

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