Page 1 of 1
How to edit this file using newlisp file APIs
Posted: Tue Mar 05, 2013 8:57 am
by csfreebird
Hello
I want to edit my script file, fill in correct value for CLOUD_ENGINE_HOME variable. Its value is empty at first.
Code: Select all
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
CLOUD_ENGINE_HOME=
DAEMON=$CLOUD_ENGINE_HOME/nginx/bin/nginx
Then I excute my newlisp codes as follows:
Code: Select all
> (set 'file (open "/etc/init.d/nginx" "update"))
3
> (search file "CLOUD_ENGINE_HOME=" true)
442
> (write file "/opt/cloud_engine_home\n")
23
> (close file)
true
My files was modified, but it deleted some characters of third line:
Code: Select all
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
CLOUD_ENGINE_HOME=/opt/cloud_engine_home
OME/nginx/bin/nginx
DAEMON=$CLOUD_ENGINE_HOME/nginx/bin/nginx
was changed into
OME/nginx/bin/nginx
What mistake did I make?
Re: How to edit this file using newlisp file APIs
Posted: Tue Mar 05, 2013 12:07 pm
by cormullion
Not sure that write inserts characters... Perhaps it just overwrites...
Re: How to edit this file using newlisp file APIs
Posted: Tue Mar 05, 2013 12:30 pm
by saulgoode
You are overwriting the characters that follow "CLOUD_ENGINE_HOME=".
You may have to split your file into lines and then replace the entire line.
For example (note: I do not do much Newlisp coding so there is probably a better way):
Code: Select all
#!/usr/bin/newlisp
(set 'filename (main-args 2))
(set 'entire-file (read-file filename))
(set 'lines (parse entire-file "\n"))
(replace "CLOUD_ENGINE_HOME=" lines "CLOUD_ENGINE_HOME=/opt/cloud_engine_home")
(device (open filename "write"))
(while lines
(println (pop lines)) )
(close (device))
(exit)
Re: How to edit this file using newlisp file APIs
Posted: Tue Mar 05, 2013 12:33 pm
by csfreebird
cormullion wrote:Not sure that write inserts characters... Perhaps it just overwrites...
Thanks for your reminding. I will try to open the file using "append" option instead of "update" tomorrow.
Re: How to edit this file using newlisp file APIs
Posted: Wed Mar 06, 2013 1:45 am
by csfreebird
I tried "append" option, unfortunately this caused "/opt/cloud_engine_home" was inserted at the end of file.
Code: Select all
esac
exit 0
/opt/cloud_engine_home
I just want to modify one part of my file instead of rewriting the whole file.
Could anyone give me a simple way?
I hope write APIs could provide one option for indicating insert or overwrite, just like most editors.
Re: How to edit this file using newlisp file APIs
Posted: Wed Mar 06, 2013 9:45 am
by cormullion
I don't think there's an "insert text in file" option anywhere. But Saul's solution above is fine.
Note that there's a
write-file function: So this is possible:
Code: Select all
(set 'input-file {/tmp/temp.txt})
(set 'lines (parse (read-file input-file) "\n"))
(replace "CLOUD_ENGINE_HOME=" lines "CLOUD_ENGINE_HOME=/opt/cloud_engine_home")
(write-file input-file (join lines "\n"))
Re: How to edit this file using newlisp file APIs
Posted: Wed Mar 06, 2013 3:46 pm
by csfreebird
It seems I have no other choice for now.
Anyway, thank for your giving.
Re: How to edit this file using newlisp file APIs
Posted: Sun Mar 10, 2013 6:18 am
by saulgoode
cormullion wrote:Code: Select all
(write-file input-file (join lines "\n"))
Aha,
join was the function I was missing. I have always been fond of the
strbreakup and
unbreakupstr functionality of
SIOD and GIMP's Script-fu interpreter, and the fact that Newlisp offers this with
parse and
join is a definite plus.