Using Applescript to remove text from file -


i have code write input strings /etc/hosts

#host database # # localhost used configure loopback interface # when system booting.  not change entry. ## 127.0.0.1   localhost 255.255.255.255 broadcasthost ::1             localhost  fe80::1%lo0 localhost  # start list 173.252.100.26  abc.com 173.252.100.26  xyz.com # end list 

but dont know how write applescript find 'start list' , 'end list' remove of contents in it.

this can run sudo osascript untitled.scpt. doesn't remove linefeeds before or after strings.

set f posix file "/etc/hosts" set input read f «class utf8» set o1 offset of "# start list" in input set o2 (offset of "# end list" in input) + 10 if o1 0 or o2 0 or o1 > o2 return if o1 1     set s "" else     set s text 1 thru (o1 - 1) of input end if if (o2 - 1) length of input     set e "" else     set e text o2 thru -1 of input end if {s, e} set output result text set b open access f write permission set eof b 0 write output b «class utf8» close access b 

or use sed:

sudo sed -i '' '/^# start list$/,/^# end list$/d' /etc/hosts 

Comments