paste: gluing lines together, side by side or in a row
Columns, without awk
In the seq post, paste fixed a portability problem in one line and got a single sentence of explanation. It deserves more, and a post of its own.
It shipped with UNIX/32V in 1979, written at Bell Labs by Gottfried Luderer, the author of cut. One takes columns out of a file, the other puts them back together. paste does so in two directions, and both replace loops seen too often.
Side by side
Three small files to play with:
printf 'alice\nbob\ncarol\n' > names.txt
printf '31\n27\n45\n' > ages.txt
printf 'paris\nlyon\n' > cities.txtGive paste two of them and it glues their lines, first with first, second with second:
$ paste names.txt ages.txt
alice 31
bob 27
carol 45
$ paste -d, names.txt ages.txt
alice,31
bob,27
carol,45The separator is a tab unless -d says otherwise. paste is the counterpart of cut: build the file with one, take it apart with the other.
$ paste -d, names.txt ages.txt > people.csv
$ cut -d, -f1 people.csv
alice
bob
carolWhen a file is shorter than the others, paste does not stop. It leaves the field empty:
$ paste -d, names.txt ages.txt cities.txt
alice,31,paris
bob,27,lyon
carol,45,In a row
With -s, for serial, it works the other way and joins the lines of each file into one:
$ paste -sd, names.txt
alice,bob,carol-sd, is -s and -d, written together. It is the answer to “how do I join lines with a comma”, a question often solved with tr, then a sed to remove the trailing comma.
It also makes a sum with no awk in sight: + as the separator, then bc to compute:
$ cut -d, -f2 people.csv | paste -sd+ -
31+27+45
$ cut -d, -f2 people.csv | paste -sd+ - | bc
103The dash trick
The - above means standard input, and that is where paste gets clever. Name it several times and each one reads the next line of the same stream:
$ seq 1 6 | paste - - -
1 2 3
4 5 6
$ seq 1 6 | paste -d, - -
1,2
3,4
5,6Three dashes, three columns. Any list becomes a table, with no awk and no counter.
The delimiter has a trick of its own. -d takes a list, used in turn, newline included:
$ paste -d ',;' names.txt ages.txt cities.txt
alice,31;paris
bob,27;lyon
carol,45;
$ paste -d '\n' names.txt ages.txt
alice
31
bob
27
carol
45The second one interleaves two files line by line, something that usually takes a loop.
Where it differs
Debian, Alpine and macOS agreed on most of the above (Debian needs apt install bc for the sum). Two differences are worth knowing.
POSIX makes the file operand mandatory, and macOS holds you to it. GNU and BusyBox are more forgiving and fall back to standard input:
# macOS
$ seq 1 5 | paste -sd+
usage: paste [-s] [-d delimiters] file ...So write the -, as everywhere in this post, and the command works on all three.
The second difference is a bug. On Alpine, BusyBox 1.37 gets files of different lengths wrong. When a file runs out, it skips the field and its delimiter, where the others leave it empty:
# Alpine, BusyBox 1.37: the short file in the middle
$ paste -d, names.txt cities.txt ages.txt
alice,paris,31
bob,lyon,27
carol,45Debian and macOS print carol,,45. Here the age slid into the city column. With the short file last, the line is carol,45, without a final newline, and a while read loop skips it.
paste never quotes anything either: glue a field that already contains a comma and you have built a broken CSV, without a warning.
And it matches lines by position, never by content. If the two files are not in the same order, it glues the wrong things together, and the tool you want is join.
← → jump to the previous / next post