LinuxCommandLibrary

recsel

Select and print specific record fields

TLDR

Extract name and version field

$ recsel [[-p|--print]] name,version [data.rec]
copy

Use "~" to match a string with a given regex
$ recsel [[-e|--expression]] "[field_name] ~ '[regex]' [data.rec]"
copy

Use a predicate to match a name and a version
$ recsel [[-e|--expression]] "name ~ '[regex]' && version ~ '[regex]'" [data.rec]
copy

SYNOPSIS

recsel [options] [query-expression] [file...]

PARAMETERS

-q, --query=EXPR
    Specify the query expression to filter records. Records matching the expression are selected.

-P, --print
    Print the selected records. This is often implied but can be used to explicitly show output.

-e, --exact
    Perform exact string matches for queries, rather than using regular expressions.

-s, --sort=FIELD
    Sort the selected records by the specified field(s). Can be specified multiple times for multi-level sorting.

-C, --cut=FIELD
    Remove the specified field(s) from the output records.

-S, --select=FIELD
    Select and display only the specified field(s) from the output records. Fields are printed in the order specified.

-F, --format=FORMAT
    Specify the output format. Supported formats include rec, csv, plain, and custom.

-i, --index=FIELD
    Assume that the specified field is indexed, potentially speeding up queries on large files.

-n, --no-headers
    Do not print field headers when using plain or csv output formats.

-c, --count
    Instead of printing records, print only the count of selected records.

-t, --record-type=TYPE
    Process only records of the specified type. Useful when a rec file contains multiple record definitions.

-o, --output=FILE
    Write output to the specified file instead of standard output.

-v, --invert
    Invert the match; select records that do NOT match the query expression.

-O, --output-fields=FIELDS
    When using --format=custom, specify the fields and their format for the output. Example: Name:%n,Age:%a.

DESCRIPTION

recsel is a powerful command-line utility from the GNU Recutils package, designed for querying, filtering, and manipulating data stored in rec files. rec files are a plain text, human-readable format for simple flat-file databases, structured as a sequence of records, each composed of fields.

The command allows users to select records based on complex query expressions (supporting boolean logic, comparisons, and regular expressions), sort them by specific fields, and select which fields to display. It can output the results in various formats, including the original rec format, CSV, plain text tables, or a custom format defined by the user. recsel is essential for automating data extraction and transformation from rec databases, making it a versatile tool for shell scripting and data processing.

CAVEATS

recsel is specifically designed for GNU Recutils' rec file format, making it less applicable to other database or data interchange formats. Its query language, while powerful, is unique to Recutils and not directly compatible with standard SQL.

QUERY LANGUAGE

The query expression used with -q supports a rich set of operators and functions. It allows boolean logic (AND, OR, NOT), comparisons (==, !=, <, <=, >, >=), regular expression matching (~), and field existence checks (FIELD?). Expressions can be nested using parentheses, enabling highly specific data filtering.

OUTPUT FORMATS

Beyond the default rec format, recsel offers flexibility in output. csv generates comma-separated values, ideal for spreadsheets. plain produces a human-readable table with aligned columns. The custom format, used with --output-fields, provides granular control over how each field is presented, allowing for highly specific report generation.

HISTORY

recsel is a core component of GNU Recutils, a project initiated and primarily developed by Jose E. Marchesi. The Recutils package aims to provide a simple yet robust set of tools for managing flat-file databases in a human-readable format. Its development reflects a focus on simplicity, portability, and text-based data management, aligning with traditional Unix philosophies.

SEE ALSO

recfmt(1), reccat(1), recset(1), recfix(1), recinfo(1)

Copied to clipboard