Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jun 7, 2013, 09:40 PM   #1
hypmatize
macrumors regular
 
Join Date: Feb 2008
Location: SoCal
Anyway to write a script that write text into an existing text file

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.
__________________
1000% Stock Gains
100% Winning Picks
www.PerfectPennyStocks.com
hypmatize is offline   0 Reply With Quote
Old Jun 7, 2013, 10:13 PM   #2
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
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
ArtOfWarfare is offline   0 Reply With Quote
Old Jun 8, 2013, 01:00 AM   #3
mrichmon
macrumors 6502a
 
Join Date: Jun 2003
Quote:
Originally Posted by hypmatize View Post
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
mrichmon is offline   0 Reply With Quote
Old Jun 8, 2013, 08:32 PM   #4
hypmatize
Thread Starter
macrumors regular
 
Join Date: Feb 2008
Location: SoCal
Quote:
Originally Posted by ArtOfWarfare View Post
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/...roduction.html
Thanks I'll take a look at it

Quote:
Originally Posted by mrichmon View Post
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.
__________________
1000% Stock Gains
100% Winning Picks
www.PerfectPennyStocks.com
hypmatize is offline   0 Reply With Quote
Old Jun 8, 2013, 08:45 PM   #5
mrichmon
macrumors 6502a
 
Join Date: Jun 2003
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.
mrichmon is offline   0 Reply With Quote
Old Jun 8, 2013, 09:10 PM   #6
hypmatize
Thread Starter
macrumors regular
 
Join Date: Feb 2008
Location: SoCal
Quote:
Originally Posted by mrichmon View Post
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 ), I should of known that .

Edit:

Tried it, but it still clears out the extra text after I exit : /
__________________
1000% Stock Gains
100% Winning Picks
www.PerfectPennyStocks.com

Last edited by hypmatize; Jun 8, 2013 at 09:15 PM.
hypmatize is offline   0 Reply With Quote
Old Jun 8, 2013, 09:27 PM   #7
ChrisA
macrumors G4
 
Join Date: Jan 2006
Location: Redondo Beach, California
Quote:
Originally Posted by hypmatize View Post
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.
ChrisA is offline   0 Reply With Quote
Old Jun 9, 2013, 12:57 AM   #8
mrichmon
macrumors 6502a
 
Join Date: Jun 2003
Quote:
Originally Posted by hypmatize View Post
Thanks I'll take a look at it

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}
mrichmon is offline   0 Reply With Quote
Old Jun 9, 2013, 03:27 AM   #9
hypmatize
Thread Starter
macrumors regular
 
Join Date: Feb 2008
Location: SoCal
Quote:
Originally Posted by mrichmon View Post
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!

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

So do I count it out?
__________________
1000% Stock Gains
100% Winning Picks
www.PerfectPennyStocks.com
hypmatize is offline   0 Reply With Quote
Old Jun 10, 2013, 08:05 AM   #10
cqexbesd
macrumors member
 
Join Date: Jun 2009
If the file doesn't change too much then use diff and patch.
cqexbesd is offline   0 Reply With Quote
Old Jun 12, 2013, 05:52 AM   #11
Narn
macrumors newbie
 
Join Date: Jun 2011
Quote:
Originally Posted by hypmatize View Post
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
Narn is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Can you write a script to auto delete text messages? bobfitz14 iOS 7 6 Mar 23, 2014 04:26 PM
VMware Fusion 5 - Shared Folder (Read/Write) - Yet no write permissions in VM altjx Windows, Linux & Others on the Mac 2 Aug 29, 2013 09:19 AM
TxtPub - Text file reader app by converting text to ePub automatically. Ralph Kim iPad Apps 0 Apr 26, 2013 09:13 PM
Write for Dropbox - A Markdown Text Editor and Notes App [iPhone Version] Tanmay iPhone and iPod touch Apps 8 Mar 25, 2013 01:21 PM
Is it possible to write a script, that would cause an action from the pass screen? TheEasterBunny iPhone/iPad Programming 7 Mar 23, 2013 03:50 PM

Forum Jump

All times are GMT -5. The time now is 05:32 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC