Skip to content
Linux Basics: Combine Lines by Position with GNU paste

Linux Basics: Combine Lines by Position with GNU paste

October 27, 2026

Sometimes two files already have corresponding rows and need to be displayed side by side. paste combines lines by position. An uneven fixture makes the missing-row behavior visible before you rely on that alignment.

Prepare a scratch directory

Tested on Linux with GNU coreutils 9.7 and GNU Bash 5.2.37. You need the named runtime, Bash, a text editor, and the standard mktemp, rm, and rmdir utilities. The script creates a separate temporary fixture directory and removes only that directory on exit. It does not operate on your existing files.

Keep using the same terminal. Stop if setup fails, before saving the source:

start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1

Run the example

Save this as demonstrate-gnu-paste.sh:

#!/usr/bin/env bash
set -euo pipefail
export LC_ALL=C
fixture_dir="$(mktemp -d)"
trap 'rm -rf -- "$fixture_dir"' EXIT
cd -- "$fixture_dir"
printf 'Ada\nBen\nCara\n' > names.txt
printf 'ready\nwaiting\n' > states.txt
printf 'Parallel rows:\n'
paste -d '|' names.txt states.txt > combined.txt
cat combined.txt
test "$(wc -l < combined.txt)" -eq 3
printf 'One line per input file:\n'
paste -s -d ',' names.txt states.txt
printf 'Alternating delimiters:\n'
paste -d ',;' names.txt states.txt names.txt

Run it:

bash demonstrate-gnu-paste.sh

Observed output:

Parallel rows:
Ada|ready
Ben|waiting
Cara|
One line per input file:
Ada,Ben,Cara
ready,waiting
Alternating delimiters:
Ada,ready;Ada
Ben,waiting;Ben
Cara,;Cara

Read the result

Normal mode reads one line from each file and joins them with the chosen delimiter. The shorter states file contributes an empty field on the last row. Serial mode (-s) joins the lines of each input file into its own output line. A delimiter list cycles between separators; it is not one multi-character separator.

Apply it carefully

paste does not compare keys or validate that the rows describe the same entity. A missing middle row can silently misalign everything after it. Use a key-based join when row correspondence is not guaranteed. Comma output is not automatically valid CSV because quoting and escaping are not added. This ASCII fixture has no embedded separators and deliberately exposes the empty final field.

The official reference describes the API and its options. The checks above cover the specific fixture, not every possible input or environment.

Clean up

Remove the source and then the empty practice directory:

rm -- demonstrate-gnu-paste.sh
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dir

If the directory is not empty, inspect the remaining files rather than deleting unknown contents.

Acknowledgements

This post was created with assistance from AI.

Last updated on