Skip to content

Commit 66a5ea8

Browse files
committed
..
1 parent 872dc8b commit 66a5ea8

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

vignettes/datatable-fread-and-fwrite.Rmd

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,12 @@ HEADER: Yet more
4545
"example_data.txt")
4646
4747
library(data.table)
48-
fread("grep -v HEADER example_data.txt")
48+
49+
all_lines <- readLines("example_data.txt")
50+
data_lines <- grep("HEADER", all_lines, value = TRUE, invert = TRUE)
51+
fread(text = data_lines)
52+
53+
file.remove("example_data.txt")
4954
```
5055

5156
The `-v` option makes `grep` return all lines except those containing the string 'HEADER'. Given the number of high quality engineers that have looked at the command tool grep over the years, it is most likely that it is as fast as you can get, as well as being correct, convenient, well documented online, easy to learn and search for solutions for specific tasks. If you need to perform more complex string filtering (e.g., matching strings at the beginning or end of lines), the `grep` syntax is very powerful. Learning its syntax is a transferable skill for other languages and environments.
@@ -177,11 +182,14 @@ c(
177182
),
178183
"insert_script.sql"
179184
)
185+
sql_lines <- readLines("insert_script.sql")
180186
181-
# 2. Process with awk and read into R
182-
dt_sql <- fread(cmd = "awk -F' *[(),]+ *' -v OFS=, '{for (i=2; i<=NF-1; i++) printf \"%s%s\", ($i), (i<NF-1?OFS:ORS)}' insert_script.sql",
183-
na.strings = "NULL") # "NULL" string from SQL becomes R's NA
187+
values_only <- gsub("INSERT INTO tbl VALUES \\((.*)\\);", "\\1", sql_lines)
188+
189+
dt_sql <- fread(text = values_only, na.strings = "NULL")
184190
print(dt_sql)
191+
192+
file.remove("insert_script.sql")
185193
```
186194

187195
- The `awk` command transforms each INSERT line into a comma-separated list of its values.

0 commit comments

Comments
 (0)