WCC logo

CIS120Linux Fundementals

cut,paste and join Commands

The cut, paste, and join commands in Unix/Linux are powerful tools for text processing, allowing users to manipulate and merge data from text files efficiently. Each command has its specific functionality and options that make it versatile for various tasks.

cut Command

The cut command is used to extract sections from each line of input files. It operates by specifying a delimiter and selecting the desired fields or columns of data. The cut command can also work with fixed-length fields.

Common Options:

Option Description
-b Selects only the bytes specified.
-c Cuts based on character positions.
-d Specifies a delimiter (default is tab).
-f Specifies the fields to be extracted.
--complement Complements the selection.
--output-delimiter Sets the output delimiter for the cut fields.

Examples:

Suppose file.txt contains the following lines:

John,Doe,30,Engineer
Jane,Smith,25,Designer
Mike,Johnson,40,Manager

Extracting the first and third fields using a comma as a delimiter:

cut -d ',' -f 1,3 file.txt

Output:

John,30
Jane,25
Mike,40

Extracting the first 5 characters of each line:

cut -c 1-5 file.txt

Output:

John,
Jane,
Mike,

Extracting the first field using a space as a delimiter (assuming fields are space-separated):

cut -d ' ' -f 1 file.txt

Extracting bytes 2 to 6 of each line:

cut -b 2-6 file.txt

Output:

ohn,D
ane,S
ike,J

Extracting all fields except the second using a tab delimiter:

cut -d $'\t' --complement -f 2 file.txt

Note: This example assumes that fields in the file are tab-separated.

paste Command

The paste command merges lines of files horizontally by outputting lines from each file side by side, separated by a delimiter. This command is useful for combining files where each line corresponds to the same logical row of data.

Common Options:

Option Description
-d Specifies a delimiter to separate pasted lines.
-s Serially paste one file at a time.

Examples:

Suppose file1.txt contains:

John
Jane
Mike

And file2.txt contains:

Doe
Smith
Johnson

Pasting two files side by side with a tab delimiter:

paste file1.txt file2.txt

Output:

John    Doe
Jane    Smith
Mike    Johnson

Suppose file1.txt contains:

John
Jane
Mike

And file2.txt contains:

Doe
Smith
Johnson

Pasting files with a comma as the delimiter:

paste -d ',' file1.txt file2.txt

Output:

John,Doe
Jane,Smith
Mike,Johnson

Suppose file1.txt contains:

John
Jane
Mike

And file2.txt contains:

Doe
Smith
Johnson

Pasting files with a space as the delimiter:

paste -d ' ' file1.txt file2.txt

Output:

John Doe
Jane Smith
Mike Johnson

Suppose file.txt contains:

John
Doe
Jane
Smith
Mike
Johnson

Serially pasting lines from a single file:

paste -s file.txt

Output:

John    Doe
Jane    Smith
Mike    Johnson

Suppose file1.txt contains:

John
Jane
Mike

And file2.txt contains:

Doe
Smith
Johnson

Pasting files with a custom delimiter:

paste -d '|' file1.txt file2.txt

Output:

John|Doe
Jane|Smith
Mike|Johnson

join Command

The join command merges lines of two sorted files based on a common field. This command is similar to the SQL join operation, combining records with matching keys.

Common Options:

Option Description
-1 Specifies the field number to join on from the first file.
-2 Specifies the field number to join on from the second file.
-t Specifies a delimiter (default is space).
-o Formats the output.
-a Prints unpairable lines from the specified file.
-e Replaces empty output fields with the specified string.

Examples:

Suppose file1.txt contains:

A John
B Jane
C Mike

And file2.txt contains:

A Doe
B Smith
C Johnson

Joining these files on the first field (default delimiter is space):

join file1.txt file2.txt

Output:

A John Doe
B Jane Smith
C Mike Johnson

Suppose file1.txt contains:

Alpha,John
Beta,Jane
Gamma,Mike

And file2.txt contains:

Alpha,Doe
Beta,Smith
Gamma,Johnson

Joining these files on the first field using a comma as the delimiter:

join -t ',' file1.txt file2.txt

Output:

Alpha,John,Doe
Beta,Jane,Smith
Gamma,Mike,Johnson

Suppose file1.txt contains:

1A John
2B Jane
3C Mike

And file2.txt contains:

1A Doe
2B Smith
3C Johnson

Joining these files on the first field:

join file1.txt file2.txt

Output:

1A John Doe
2B Jane Smith
3C Mike Johnson

In summary, cut, paste, and join are essential commands for text manipulation in Unix/Linux. They offer a variety of options for extracting, merging, and combining data efficiently, making them invaluable for tasks involving text processing. Understanding these commands and their options enables users to perform complex data manipulations with ease.