Text Editing with Cut, Paste, and Join Commands | Online Free DevTools by Hexmos

Learn to edit text files using cut, paste, and join commands in Linux. Master text manipulation for developers with practical examples and syntax.

Text Editing with Cut, Paste, and Join Commands

21. Text Editing with cut, paste, and join

This section explores essential command-line utilities for text manipulation in Linux: cut, paste, and join. These tools are invaluable for developers and system administrators who need to process and transform text data efficiently.

21.1. The cut Command

The cut command is used to remove sections from each line of input files and write the result to standard output. It's particularly useful for extracting specific columns or character ranges from text data.

Syntax: cut OPTION [FILE]

Option Description
-c Select by character range.
-d Use a specified delimiter to separate fields.
-f Select specific fields to print.

When the -c option is used, character ranges can be specified as follows:

Range Type Description
N The Nth character.
N- From the Nth character to the end of the line.
N-M From the Nth character to the Mth character.
-M From the beginning of the line to the Mth character.

21.1.1. cut Command Examples

  • Print the first and third columns of a CSV file:
    cut -f1,3 -d"," file.csv
  • Print the first 3 characters of each line:
    cut -c -3 file.txt

21.2. The paste Command

The paste command merges lines of files. By default, it joins corresponding lines from each file, separated by a tab character. If a hyphen (-) is used instead of a filename, paste reads from standard input.

Syntax: paste [OPTION] [FILE] [FILE]...

Option Description
-d Specify the delimiter to use instead of the default tab.
-s Paste one file at a time, concatenating its lines.

21.2.1. paste Command Examples

Let's consider two files: number.txt and name.txt.

cat number.txt

1
2
3
4

cat name.txt

Alice
Bob
Charlie
David
  • Merge two files, with the first file providing the first column and the second file providing the second column (tab-delimited by default):
    paste number.txt name.txt
  • Merge two files, using a comma (,) as the delimiter:
    paste -d"," number.txt name.txt
  • Merge two files sequentially, printing all lines from the first file followed by all lines from the second file:
    paste -s number.txt name.txt

21.3. The join Command

The join command joins lines of two files based on a common field. It requires that the input files be sorted on the join field. If no field is specified, it joins on the first field.

Syntax: join [OPTIONS] FILE1 FILE2

21.3.1. join Command Examples

Let's use two files: number.txt and name.txt, formatted for joining.

cat number.txt

1 100
2 101
3 102
4 103
5 104

cat name.txt

1 Alice
2 Bob
3 Charlie
4 David
  • Join the two files based on the first column (which is common to both):
    join number.txt name.txt

    This will output lines where the first field matches, combining the remaining fields from both files.

These commands are fundamental for scripting and data processing tasks, enabling precise control over text content.