From 38270a89a9a044005f949f5b73bf55c54117dbdd Mon Sep 17 00:00:00 2001 From: Mike Mason Date: Thu, 22 Nov 2007 16:22:47 +0000 Subject: Added a search replace function for files git-svn-id: svn://svn.alpinelinux.org/acf/core/trunk@356 ab2d0c66-481e-0410-8bed-d214d4d58bed --- lib/fs.lua | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'lib/fs.lua') diff --git a/lib/fs.lua b/lib/fs.lua index c27f613..48364a6 100644 --- a/lib/fs.lua +++ b/lib/fs.lua @@ -46,6 +46,7 @@ function read_file_as_array ( path ) local f = {} for line in file:lines() do table.insert ( f , line ) + --sometimes you will see it like f[#f+1] = line end file:close() return f @@ -72,6 +73,18 @@ end -- returns a table to iterate over without the blank or commented lines return lines end + +--will search and replace through the whole of the file and return a table + +function search_replace (path , find, replace) +local f = fs.read_file_as_array(path) +local lines = {} +for a,b in ipairs(f) do +local c = string.gsub(b, find, replace) +lines[#lines + 1] = c end +return lines +end + -- write a string to a file !! MM-will replace file contents @@ -83,7 +96,7 @@ function write_file ( path, str ) end end --- this could do more than a line +-- this could do more than a line. This will append -- fs.write_line_file ("filename", "Line1 \nLines2 \nLines3") function write_line_file ( path, str ) -- cgit v1.2.3