How to edit this file using newlisp file APIs

Started by csfreebird, March 05, 2013, 12:57:39 AM

Previous topic - Next topic

csfreebird

Hello

  I want to edit my script file, fill in correct value for CLOUD_ENGINE_HOME variable. Its value is empty at first.

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:

> (set 'file (open "/etc/init.d/nginx" "update"))
3
> (search file "CLOUD_ENGINE_HOME=" true)
442
> (write file "/opt/cloud_engine_homen")
23
> (close file)
true


My files was modified, but it deleted some characters of third line:

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?

cormullion

#1
Not sure that write inserts characters... Perhaps it just overwrites...

saulgoode

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

csfreebird

#3
Quote from: "cormullion"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.

csfreebird

#4
I tried "append" option, unfortunately this caused "/opt/cloud_engine_home" was inserted at the end of file.

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.

cormullion

#5
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:
(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"))

csfreebird

#6
It seems I have no other choice for now.

Anyway, thank for your giving.

saulgoode

#7
Quote from: "cormullion"(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 http://www.cs.indiana.edu/scheme-repository/imp/siod.html">SIOD and GIMP's Script-fu interpreter, and the fact that Newlisp offers this with parse and join is a definite plus.