PDA

View Full Version : Anyway to write a script that write text into an existing text file




hypmatize
Jun 7, 2013, 09:40 PM
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
Jun 7, 2013, 10:13 PM
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
Jun 8, 2013, 01:00 AM
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:

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:

aaa
aaa
aaa
cccc
bbb
bbb

or:

aaa
aaa
aaa
cccc
bbb

hypmatize
Jun 8, 2013, 08:32 PM
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

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:

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:

aaa
aaa
aaa
cccc
bbb
bbb

or:

aaa
aaa
aaa
cccc
bbb


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


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


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
Jun 8, 2013, 08:45 PM
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
Jun 8, 2013, 09:10 PM
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 :o.

Edit:

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

ChrisA
Jun 8, 2013, 09:27 PM
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
Jun 9, 2013, 12:57 AM
Thanks I'll take a look at it :D

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


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


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.

#!/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
Jun 9, 2013, 03:27 AM
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.

#!/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?

cqexbesd
Jun 10, 2013, 08:05 AM
If the file doesn't change too much then use diff and patch.

Narn
Jun 12, 2013, 05:52 AM
I want it to do this.


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


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:


#!/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:


$ script.sh < input.txt > tmp
$ mv tmp input.txt