Re: Insert Text Here
Re: Insert Text Here
- Subject: Re: Insert Text Here
- From: David Ferrington <email@hidden>
- Date: Sun, 22 Jan 2012 10:18:45 +0000
Why not use sed to make a simple substitution of a known text string. My A/S is not good, but the unix sed command to do that is :
cp file_to_be_changed /tmp/file_to_be_changed.bak
sed -e's/INSERT-TEXT-HERE/your new text to be inserted' /tmp/file_to_be_changed.bak > file_to_be_changed
rm -f /tmp/file_to_be_changed.bak
sed will put the text out to stdout, so you need to buffer it if you want to redirect, hence copy your file to another temp file and then use that as the source of the file to be changed.
run a terminal, do 'man sed' for use and test it
--
The universe is like a safe to which there is a combination. But the
combination is locked up in the safe.
-Peter De Vries, editor, novelist (1910-1993)
On 21 Jan 2012, at 18:03, Jon Pugh wrote:
> On Jan 21, 2012, at 9:04 AM, Marconi wrote:
>> I need to open a text file for writing but, instead of appending text, I need to insert text at a given point.
>>
>> Suppose I have a text file that is like this:
>>
>> File starts here
>> some more stuff
>> insert text here.
>> some more stuff too
>> end of the stuff
>>
>>
>> And suppose I wanted to insert some text ("this has been inserted") after the designator "insert text here" such that the result is:
>>
>>
>> File starts here
>> some more stuff
>> insert text here.
>> this has been inserted
>> some more stuff too
>> end of the stuff
>>
>> Is there a way, when writing a text file, to insert text rather than appending or overwriting?
>
> You have to read in the whole file, modify it in memory and then write out the whole file.
>
> Time to flog my SmartString class again. ;)
>
> property ss : load script file ((path to application support from user domain as string) & "Script Debugger:Script Libraries:SmartString.scpt")
> set file_path to (path to desktop as string) & "file.txt"
> set new_path to (path to desktop as string) & "newfile.txt"
> set file_text to read file file_path
> set insert_location to "insert text here."
> set inserted_string to "this has been inserted"
> tell ss's SmartString
> setString(file_text)
> set new_text to beforeString(insert_location) & insert_location & return & inserted_string & afterString(insert_location)
> end tell
> set file_ref to open for access file new_path with write permission
> write new_text to file_ref
> close access file_ref
>
> SmartString is available here: http://www.seanet.com/~jonpugh/software/SmartString.applescript
>
> It’s also easier to use from Script Debugger, as you just choose it from a popup and it’s included. Very nice.
>
> Jon
>
>
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> AppleScript-Users mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
> Archives: http://lists.apple.com/archives/applescript-users
>
> This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden