我有一个代码将输入字符串写入/ etc / hosts
#Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this 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
但我不知道如何编写另一个AppleScript来查找“开始列表”和“结束列表”以删除其中的所有内容。
答案 0 :(得分:1)
这可以使用sudo osascript Untitled.scpt
之类的东西来运行。它不会删除字符串之前或之后的换行符。
set f to POSIX file "/etc/hosts"
set input to read f as «class utf8»
set o1 to offset of "# Start List" in input
set o2 to (offset of "# End List" in input) + 10
if o1 is 0 or o2 is 0 or o1 > o2 then return
if o1 is 1 then
set s to ""
else
set s to text 1 thru (o1 - 1) of input
end if
if (o2 - 1) is length of input then
set e to ""
else
set e to text o2 thru -1 of input
end if
{s, e}
set output to result as text
set b to open for access f with write permission
set eof b to 0
write output to b as «class utf8»
close access b
或者只使用sed:
sudo sed -i '' '/^# Start List$/,/^# End List$/d' /etc/hosts