# Read fixed-width text

Acquire / file

A variant of [Read a CSV or delimited file](https://meridian.online/operations/read-a-csv-or-delimited-file).

Read text whose columns sit at fixed character positions, with no separator between them.

Page: <https://meridian.online/operations/read-a-csv-or-delimited-file/read-fixed-width-text>

## Status

Runs as a SQL step.

arcform carries it as a `sql:` step today.

## The DuckDB step

read text whose columns sit at fixed character positions

It reads the `orders` table the proofs share. Create it first:

```sql
CREATE TABLE orders AS SELECT * FROM (VALUES
  (1,  'Alice Ng',      'north', DATE '2026-01-03', 120.00, 'widget', 'a|b',   '{"channel":"web","items":[1,2]}'),
  (2,  'alice ng',      'north', DATE '2026-01-05',  80.50, 'gadget', 'b',     '{"channel":"shop","items":[3]}'),
  (3,  'Bob  Okafor',   'south', DATE '2026-01-05', 300.00, 'widget', 'a|c',   '{"channel":"web","items":[4,5,6]}'),
  (4,  'Carla Díaz',    'east',  DATE '2026-01-09',  NULL,  'gizmo',  NULL,    '{"channel":"web","items":[]}'),
  (5,  'Bob Okafor',    'south', DATE '2026-01-12',  45.25, 'gadget', 'c',     '{"channel":"phone","items":[7]}'),
  (6,  'Dev Patel',     'west',  DATE '2026-01-12', 510.00, 'widget', 'a',     '{"channel":"web","items":[8,9]}'),
  (7,  'Carla Diaz',    'east',  DATE '2026-01-20',  99.99, 'gizmo',  'b|c',   '{"channel":"shop","items":[10]}'),
  (8,  'Dev Patel',     'west',  DATE '2026-02-02', 510.00, 'widget', 'a',     '{"channel":"web","items":[8,9]}'),
  (9,  'Eve Larsen',    NULL,    DATE '2026-02-03',  15.00, 'gadget', NULL,    '{"channel":"web","items":[11]}'),
  (10, 'Alice Ng',      'north', DATE '2026-02-10', 220.00, 'gizmo',  'a|b|c', '{"channel":"phone","items":[12,13]}')
) t(order_id, customer, region, order_date, amount, product, tags, meta);
```

Then run the statement:

```sql
-- a fixed-width file: id in 1-3, customer in 4-15, amount in 16-23, the amount blank when it is missing
COPY (SELECT lpad(order_id::VARCHAR, 3, '0') || rpad(customer, 12, ' ') || coalesce(lpad(format('{:.2f}', amount::DOUBLE), 8, ' '), repeat(' ', 8)) AS line
      FROM orders ORDER BY order_id) TO '<your_scratch_dir>/fixed.txt' (FORMAT csv, HEADER false, QUOTE '', ESCAPE '', DELIMITER '~');
CREATE TEMP TABLE result AS
SELECT substr(line, 1, 3)::INTEGER AS order_id, trim(substr(line, 4, 12)) AS customer, nullif(trim(substr(line, 16, 8)), '')::DECIMAL(8, 2) AS amount
FROM (SELECT unnest(string_split_regex(regexp_replace(content, '\r?\n$', ''), '\r?\n')) AS line FROM read_text('<your_scratch_dir>/fixed.txt'));
SELECT * FROM result ORDER BY order_id;
SELECT CASE WHEN ((SELECT count(*) = 10 AND count(amount) = 9 AND sum(amount) = 1900.74 AND max(customer) FILTER (WHERE order_id = 3) = 'Bob  Okafor' AND max(customer) FILTER (WHERE order_id = 4) = 'Carla Díaz' FROM result)) THEN 'as expected' ELSE error('the fixed-width lines were not cut into 10 orders with 9 amounts totalling 1900.74') END AS verdict;
```

Replace `<your_scratch_dir>` with a directory of your own that you can write to before running the statement.

The statement writes `fixed.txt` itself, in `<your_scratch_dir>`.

It needs nothing installed beyond DuckDB itself.

## What other tools call it

Meridian's classification; an independent blind rating agreed on 95.7% of 324 rows (variant, version 3).

- Duckle: [`Fixed-width`](https://github.com/slothflowlabs/duckle/blob/f85f8960edfb6efbf4cb89d58a7b32e16df0e867/website/docs/components.html) (read 2026-09-25)
- EasyMorph: [`Import fixed width text`](https://help.easymorph.com/doku.php?id=transformations:importfixedwidthtext) (read 2026-09-27)
- KNIME Analytics Platform: [`Fixed Width File Reader`](https://api.hub.knime.com/nodes/*A0XkYudtYHPfC_2T?details=full) (read 2026-09-27)
- OpenRefine: [`Fixed-width columns`](https://openrefine.org/docs/manual/starting#create-a-project-by-importing-data) (read 2026-09-27)
- Pentaho Data Integration: [`Fixed file input`](https://docs.pentaho.com/pdia-data-integration/9.3-data-integration/pdi-transformation-steps-reference-overview) (read 2026-09-27)
- Talend: [`tFileInputPositional`](https://help.qlik.com/talend/en-US/components/8.0/positional/positional-component) (read 2026-09-27)
- Talend: [`tFileStreamInputPositional`](https://help.qlik.com/talend/en-US/components/8.0/positional/positional-component) (read 2026-09-27)
- VisiData: [`fixed (loader)`](https://github.com/saulpw/visidata/blob/7609212ec2febafa9caba7183cbebfaf49627909/visidata/loaders/fixed_width.py#L10) (read 2026-09-23)
