Passa al contenuto principale

3. paste

The paste command does the opposite of cut. Rather than extracting a column of text from a file, it adds one or more columns of text to a file.

To demonstrate how paste operates, we will perform some surgery on our distros.txt file to produce a chronological list of releases.

  1. First let's sort distros by date:

    head distros.txt
    sort -k 3.7nbr -k 3.1nbr -k 3.4nbr \
    distros.txt > distros-by-date.txt
    head distros-by-date.txt
  2. Next, let's use cut to extract the first two fields/columns from the file (the distro name and version):

    cut -f 1,2 distros-by-date.txt > distros-versions.txt
    head distros-versions.txt

    Let's also extract the release dates:

    cut -f 3 distros-by-date.txt > distros-dates.txt
    head distros-dates.txt
  3. To complete the process, let's use paste to put the column of dates ahead of distro names and versions, thus creating a chronological list:

    paste distros-dates.txt distros-versions.txt \
    > distros-chronological.txt
    head distros-chronological.txt
Loading asciinema cast...