Merge Lines Sequentially with Linux Paste Command

The Linux paste command is a core utility designed to join lines from one or more files and display the result to standard output. While it defaults to merging lines horizontally across multiple files, it can also process text sequentially—combining all lines within a single file into a single line or joining consecutive lines in series. This article explains the internal mechanics of the paste utility, how it handles sequential processing using specific flags, and practical methods for controlling delimiters and stream inputs.

The Mechanics of the paste Utility

By default, the paste command reads corresponding lines from each specified file, replaces the trailing newline character of each line (except the last file) with a delimiter, and prints the joined result.

When you instruct paste to merge lines sequentially, it alters its reading pattern. Instead of reading the first line from File A, the first line from File B, and so on, it processes an entire file from start to finish before moving to the next.

Sequential Merging with the -s Flag

The primary method for sequential merging is the -s (or --serial) option. When this flag is passed, paste reads through a single file, replacing every newline character with a delimiter, effectively turning a vertical column of text into a single horizontal row.

Given a file named items.txt:

apple
banana
cherry
date

Running the sequential paste command:

paste -s items.txt

Output:

apple   banana  cherry  date

Behind the scenes, the Linux operating system reads the input stream character by character (or buffer by buffer), detects the \n (newline) control character, and replaces it with the default tab character (\t). The trailing newline of the final line remains intact to ensure standard POSIX formatting on standard output.

Customizing Delimiters with -d

By default, paste uses a tab (\t) as the joining character. The -d (or --delimiters) flag allows you to specify a single character or a list of characters to replace the newline.

To join lines sequentially with a comma and space:

paste -s -d, items.txt

Output:

apple,banana,cherry,date

Rotating Delimiters Sequentially

The paste command can also cycle through a list of delimiters sequentially. If you provide multiple characters to -d, paste alternates through the list for each merged line until it reaches the end of the file:

paste -s -d':,' items.txt

Output:

apple:banana,cherry:date

In this execution, the utility replaces the first newline with a colon (:), the second with a comma (,), the third with a colon (:), and terminates the line with the original trailing newline.

Merging Consecutive Line Pairs Using Standard Input

Beyond the -s flag, the paste command can merge a set number of consecutive lines sequentially from a single file by passing standard input (-) multiple times.

When paste receives - as an argument, it reads one line from standard input. If you provide - twice, it reads the first line for the first argument, the second line for the second argument, and joins them:

paste - - < items.txt

Output:

apple   banana
cherry  date

For every - provided, paste consumes one sequential line from the stream per output row. This allows for grouping data into pairs, triplets, or arbitrary column counts without requiring complex text-processing scripts like awk or sed.