seek {seekr}R Documentation

Extract Matching Lines from Files

Description

These functions search through one or more text files, extract lines matching a regular expression pattern, and return a tibble containing the results.

Usage

seek(
  pattern,
  path = ".",
  ...,
  filter = NULL,
  negate = FALSE,
  recurse = FALSE,
  all = FALSE,
  relative_path = TRUE,
  matches = FALSE
)

seek_in(files, pattern, ..., matches = FALSE)

Arguments

pattern

A regular expression pattern used to match lines.

path

A character vector of one or more directories where files should be discovered (only for seek()).

...

Additional arguments passed to readr::read_lines(), such as skip, n_max, or locale.

filter

Optional. A regular expression pattern used to filter file paths before reading. If NULL, all text files are considered.

negate

Logical. If TRUE, files matching the filter pattern are excluded instead of included. Useful to skip files based on name or extension.

recurse

If TRUE recurse fully, if a positive number the number of levels to recurse.

all

If TRUE hidden files are also returned.

relative_path

Logical. If TRUE, file paths are made relative to the path argument. If multiple root paths are provided, relative_path is automatically ignored and absolute paths are kept to avoid ambiguity.

matches

Logical. If TRUE, all matches per line are also returned in a matches list-column.

files

A character vector of files to search (only for seek_in()).

Details

[Experimental]

The overall process involves the following steps:

These functions are particularly useful for analyzing source code, configuration files, logs, and other structured text data.

Value

A tibble with one row per matched line, containing:

See Also

fs::dir_ls(), readr::read_lines(), stringr::str_detect()

Examples

path = system.file("extdata", package = "seekr")

# Search all function definitions in R files
seek("[^\\s]+(?= (=|<-) function\\()", path, filter = "\\.R$")

# Search for usage of "TODO" comments in source code in a case insensitive way
seek("(?i)TODO", path, filter = "\\.R$")

# Search for error/warning in log files
seek("(?i)error", path, filter = "\\.log$")

# Search for config keys in YAML
seek("database:", path, filter = "\\.ya?ml$")

# Looking for "length" in all types of text files
seek("(?i)length", path)

# Search for specific CSV headers using seek_in() and reading only the first line
csv_files <- list.files(path, "\\.csv$", full.names = TRUE)
seek_in(csv_files, "(?i)specie", n_max = 1)


[Package seekr version 0.1.3 Index]