Records and text processing

Sort records by a field

Sort the entries of a directory by size, largest first.

list "/home/user/app" |> sort size order: desc
/home/user/app

Take the first records

Take the ten most recently modified files.

find "/home/user/app" |> sort modified order: desc |> first 10
/home/user/app

Select fields

Show only the path and the size of the files.

find "/home/user/app" |> select path size
path

Compute a field

Show the size of the files in kilobytes.

find "/home/user/app" |> select path kb: size / 1000
kb

Group and summarize

Count the files by extension.

find "/home/user/app" |> group by: extension |> summarize
extension

Remove duplicates

List the distinct file extensions of a project.

find "/home/user/app" |> select extension |> distinct
extension

Skip a header

Skip the first line of a file.

lines "/home/user/data.csv" |> skip 1 |> first 5
data.csv

Split lines into fields

Split lines of a colon separated file into named fields.

lines "/etc/example/passwd" |> split ":" names: ["user", "uid", "home"]
user
awk -F: '{print $1, $3}' /etc/example/passwd

Parse lines with a pattern

Extract named parts of each log line.

lines "/home/user/app/app.log" |> parse /(?P<level>\w+) (?P<message>.*)/
level

Describe the fields of records

Show the fields and the types of the records of a command.

processes |> describe
field

Evaluate an expression

Evaluate an expression without reading any file.

evaluate 40 + 2
42

Name a result and use it twice

Store the result of a search and count it.

let recent = find "/home/user/app/src" glob: "**/*.ts" |> filter modified > ago(1h)
@recent |> count
0

Hash a file

Compute the SHA-256 hash of a file.

hash "/home/user/dl/a.tar.gz" algorithm: sha256
sha256
sha256sum /home/user/dl/a.tar.gz

Filter by a pattern in a field

Keep the entries whose name matches a regular expression.

list "/home/user/app" |> filter name ~ /^test_/
test_

Sort by two fields

Sort the files by extension and, within an extension, by size.

find "/home/user/app" |> sort by: [extension, size] order: desc
extension

Take the last records

Take the five oldest files of a directory.

find "/home/user/app" |> sort modified |> first 5
/home/user/app

Keep the last lines of a file

Keep the last three lines of a file.

lines "/home/user/app/app.log" |> last 3
app.log

Filter the lines of a file

Keep the lines of a file that contain a warning.

lines "/home/user/app/app.log" |> filter text ~ /WARN/
WARN

Select the number and the text of lines

Show the number and the text of every line.

lines "/home/user/app/main.go" |> select no text
text

Count the records by a field

Count the files of each type.

find "/home/user/app" |> group by: type |> summarize
type

Sum a field

Add up the sizes of the files by extension.

find "/home/user/app" |> summarize size by: extension
size

Split a table into fields

Split lines of a semicolon separated file and keep two columns.

lines "/home/user/data.csv" |> skip 1 |> split ";" names: ["id", "name", "city"] |> select id name
id

Query a SQLite database

Read the rows of a table of a database file.

sql "/home/user/app/data.db" "select id, name from users where active = 1"
name

Query with named parameters

Bind the values of a query by name.

sql "/home/user/app/data.db" "select id from orders where total > :min and city = :city" params: {min: 100, city: "Gdansk"}
id

Change rows and review the result

Update rows of a database file and show the number of rows that changed.

sql "/home/user/app/data.db" "update users set active = 0 where last_seen < ?" params: ["2025-01-01"]
rows_changed

Source file on GitHub: docs/cookbook/records-and-text.md