Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

hypmatize

macrumors regular
Original poster
Feb 25, 2008
245
0
SoCal
I'm a noob at scripting and don't really know where to start.

What I am trying to do is write a script that when executed writes text into another text file in a specific location in the file. Is that possible? I know you can write to a text file but I'm not sure if you can put it in a specific location.


If you can, I would really appreciate an help you guys can give me.
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,561
6,059
I don't know much about scripting (I mostly write programs... I've written a very few Applescripts and Shell scripts...) but this sounds definitely doable.

I suppose reading over the Apple Shell Scripting Primer (and doing whatever exercises it suggests) will get you going on your way:

https://developer.apple.com/library/mac/documentation/OpenSource/Conceptual/ShellScripting/Introduction/Introduction.html
 

mrichmon

macrumors 6502a
Jun 17, 2003
873
3
I'm a noob at scripting and don't really know where to start.

What I am trying to do is write a script that when executed writes text into another text file in a specific location in the file. Is that possible? I know you can write to a text file but I'm not sure if you can put it in a specific location.


If you can, I would really appreciate an help you guys can give me.

How do you want to specify the location? Line offset is easiest, but it can be done with byte offset.

Do you expect the inserted text to overwrite what was there, or be inserted?

Assume the file is:

Code:
aaa
aaa
aaa
bbb
bbb

And you are inserting the text line "cccc" after the first 3 lines. Do you expect the output to be:

Code:
aaa
aaa
aaa
cccc
bbb
bbb

or:

Code:
aaa
aaa
aaa
cccc
bbb
 

hypmatize

macrumors regular
Original poster
Feb 25, 2008
245
0
SoCal
I don't know much about scripting (I mostly write programs... I've written a very few Applescripts and Shell scripts...) but this sounds definitely doable.

I suppose reading over the Apple Shell Scripting Primer (and doing whatever exercises it suggests) will get you going on your way:

https://developer.apple.com/library...ShellScripting/Introduction/Introduction.html

Thanks I'll take a look at it :D

How do you want to specify the location? Line offset is easiest, but it can be done with byte offset.

Do you expect the inserted text to overwrite what was there, or be inserted?

Assume the file is:

Code:
aaa
aaa
aaa
bbb
bbb

And you are inserting the text line "cccc" after the first 3 lines. Do you expect the output to be:

Code:
aaa
aaa
aaa
cccc
bbb
bbb

or:

Code:
aaa
aaa
aaa
cccc
bbb

It would be the second example. I want it to do this.

Code:
write_preferences_at_exit true; # write_preferences_at_exit <bool>, Write preferences at exit #
app_multirun false; # app_multirun <bool>, Allow multiple instances of the application #

to this

Code:
write_preferences_at_exit true; # write_preferences_at_exit <bool>, Write preferences at exit #
mod "ETF.pack";
mod "mod_grandcampaigngraphics.pack";
mod "mod_BSM_4_1_Full_1_5.pack";
mod "sponge.pack";
mod "royalnavy2.pack";
mod "NPIv0.3.0.pack";
app_multirun false; # app_multirun <bool>, Allow multiple instances of the application #

it's basically a script to write into another script. I want it because that added text gets deleted everything I exit the application.

Now that I read the script, couldn't I just change something in the script so it doesn't delete the added text instead of writing a script for script?

Sorry if I wasn't clear in my OP. I guess I can just try and see what happens. :p
 

mrichmon

macrumors 6502a
Jun 17, 2003
873
3
Change the config file to:

write_preferences_at_exit false; # write_preferences_at_exit <bool>, Write preferences at exit #
app_multirun false; # app_multirun <bool>, Allow multiple instances of the application #

It looks like the application already has functionality to avoid rewriting the file.
 

hypmatize

macrumors regular
Original poster
Feb 25, 2008
245
0
SoCal
Change the config file to:

write_preferences_at_exit false; # write_preferences_at_exit <bool>, Write preferences at exit #
app_multirun false; # app_multirun <bool>, Allow multiple instances of the application #

It looks like the application already has functionality to avoid rewriting the file.

ok cool... That's what I assumed. My only programing experience was C# and CSS (if you CSS can be called programing :confused:), I should of known that :eek:.

Edit:

Tried it, but it still clears out the extra text after I exit : /
 
Last edited:

ChrisA

macrumors G5
Jan 5, 2006
12,578
1,695
Redondo Beach, California
I'm a noob at scripting and don't really know where to start.

What I am trying to do is write a script that when executed writes text into another text file in a specific location in the file. Is that possible? I know you can write to a text file but I'm not sure if you can put it in a specific location.


If you can, I would really appreciate an help you guys can give me.

The way to think about this is to create NEW file. Say you want to insert data after line four.

So the script does this:
1) copy the first four lines to the new file
2) copy new data to file.
3) copy remainder of fist file to new file.

Look at the shell command "split". At can cut a file into segments. Then you drop your new data into the list of segmants and then "cat" them all together.
 

mrichmon

macrumors 6502a
Jun 17, 2003
873
3
Thanks I'll take a look at it :D

It would be the second example. I want it to do this.

Code:
write_preferences_at_exit true; # write_preferences_at_exit <bool>, Write preferences at exit #
app_multirun false; # app_multirun <bool>, Allow multiple instances of the application #

to this

Code:
write_preferences_at_exit true; # write_preferences_at_exit <bool>, Write preferences at exit #
mod "ETF.pack";
mod "mod_grandcampaigngraphics.pack";
mod "mod_BSM_4_1_Full_1_5.pack";
mod "sponge.pack";
mod "royalnavy2.pack";
mod "NPIv0.3.0.pack";
app_multirun false; # app_multirun <bool>, Allow multiple instances of the application #

Based on your example, that matches the first example I gave. That is, no overwrite of existing lines.

But since your input file only has two lines and you are inserting text between the two lines the problem is greatly simplified.

The following shell script is rough and assumes that the input file has two lines and you want the inserted text to go between them.

Code:
#!/bin/bash

INPUT_FILE="input.txt"
INSERT_FILE="insert.txt"
OUTPUT_FILE="output.txt"

input_head=`head -1 ${INPUT_FILE}`
input_tail=`tail -1 ${INPUT_FILE}`

echo ${input_head} > ${OUTPUT_FILE}
cat ${INSERT_FILE} >> ${OUTPUT_FILE}
echo ${input_tail} >> ${OUTPUT_FILE}
 

hypmatize

macrumors regular
Original poster
Feb 25, 2008
245
0
SoCal
Based on your example, that matches the first example I gave. That is, no overwrite of existing lines.

But since your input file only has two lines and you are inserting text between the two lines the problem is greatly simplified.

The following shell script is rough and assumes that the input file has two lines and you want the inserted text to go between them.

Code:
#!/bin/bash

INPUT_FILE="input.txt"
INSERT_FILE="insert.txt"
OUTPUT_FILE="output.txt"

input_head=`head -1 ${INPUT_FILE}`
input_tail=`tail -1 ${INPUT_FILE}`

echo ${input_head} > ${OUTPUT_FILE}
cat ${INSERT_FILE} >> ${OUTPUT_FILE}
echo ${input_tail} >> ${OUTPUT_FILE}


Thanks! :D

The file has 100's of lines in it though : /

So do I count it out?
 

Narn

macrumors newbie
Jun 8, 2011
3
0
I want it to do this.

Code:
write_preferences_at_exit true; # write_preferences_at_exit <bool>, Write preferences at exit #
app_multirun false; # app_multirun <bool>, Allow multiple instances of the application #

to this

Code:
write_preferences_at_exit true; # write_preferences_at_exit <bool>, Write preferences at exit #
mod "ETF.pack";
mod "mod_grandcampaigngraphics.pack";
mod "mod_BSM_4_1_Full_1_5.pack";
mod "sponge.pack";
mod "royalnavy2.pack";
mod "NPIv0.3.0.pack";
app_multirun false; # app_multirun <bool>, Allow multiple instances of the application #

it's basically a script to write into another script. I want it because that added text gets deleted everything I exit the application.

You can use sed to append lines after a pattern:

Code:
#!/bin/bash

match="write_preferences_at_exit true; # write_preferences_at_exit <bool>, Write preferences at exit #"

insert=$(cat <<EOF
mod "ETF.pack";\\
mod "mod_grandcampaigngraphics.pack";\\
mod "mod_BSM_4_1_Full_1_5.pack";\\
mod "sponge.pack";\\
mod "royalnavy2.pack";\\
mod "NPIv0.3.0.pack";\\
EOF)

sed "/${match}/a\\
${insert}
"

This script will insert the text in the variable $insert after the line(s) matching the text in $match. The result will be written to standard output. To replace the original file (after making sure the script works as expected), redirect the output to a temporary file and then rename it:

Code:
$ script.sh < input.txt > tmp
$ mv tmp input.txt
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.