How to edit this file using newlisp file APIs

Q&A's, tips, howto's
Locked
csfreebird
Posts: 107
Joined: Tue Jan 15, 2013 11:54 am
Location: China, Beijing
Contact:

How to edit this file using newlisp file APIs

Post 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?

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: How to edit this file using newlisp file APIs

Post by cormullion »

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

saulgoode
Posts: 10
Joined: Sat Jul 16, 2011 6:15 am

Re: How to edit this file using newlisp file APIs

Post 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)

csfreebird
Posts: 107
Joined: Tue Jan 15, 2013 11:54 am
Location: China, Beijing
Contact:

Re: How to edit this file using newlisp file APIs

Post 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.

csfreebird
Posts: 107
Joined: Tue Jan 15, 2013 11:54 am
Location: China, Beijing
Contact:

Re: How to edit this file using newlisp file APIs

Post 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.

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: How to edit this file using newlisp file APIs

Post 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")) 

csfreebird
Posts: 107
Joined: Tue Jan 15, 2013 11:54 am
Location: China, Beijing
Contact:

Re: How to edit this file using newlisp file APIs

Post by csfreebird »

It seems I have no other choice for now.
Anyway, thank for your giving.

saulgoode
Posts: 10
Joined: Sat Jul 16, 2011 6:15 am

Re: How to edit this file using newlisp file APIs

Post 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.

Locked