gawk: Readfile Function
1
1 10.2.8 Reading a Whole File at Once
1 -----------------------------------
1
1 Often, it is convenient to have the entire contents of a file available
1 in memory as a single string. A straightforward but naive way to do
1 that might be as follows:
1
1 function readfile1(file, tmp, contents)
1 {
1 if ((getline tmp < file) < 0)
1 return
1
1 contents = tmp RT
1 while ((getline tmp < file) > 0)
1 contents = contents tmp RT
1
1 close(file)
1 return contents
1 }
1
1 This function reads from 'file' one record at a time, building up the
1 full contents of the file in the local variable 'contents'. It works,
1 but is not necessarily efficient.
1
1 The following function, based on a suggestion by Denis Shirokov,
1 reads the entire contents of the named file in one shot:
1
1 # readfile.awk --- read an entire file at once
1
1 function readfile(file, tmp, save_rs)
1 {
1 save_rs = RS
1 RS = "^$"
1 getline tmp < file
1 close(file)
1 RS = save_rs
1
1 return tmp
1 }
1
1 It works by setting 'RS' to '^$', a regular expression that will
1 never match if the file has contents. 'gawk' reads data from the file
1 into 'tmp', attempting to match 'RS'. The match fails after each read,
1 but fails quickly, such that 'gawk' fills 'tmp' with the entire contents
1 of the file. (⇒Records for information on 'RT' and 'RS'.)
1
1 In the case that 'file' is empty, the return value is the null
1 string. Thus, calling code may use something like:
1
1 contents = readfile("/some/path")
1 if (length(contents) == 0)
1 # file was empty ...
1
1 This tests the result to see if it is empty or not. An equivalent
1 test would be 'contents == ""'.
1
1 ⇒Extension Sample Readfile for an extension function that also
1 reads an entire file into memory.
1