Linux – Using ‘cut’ command efficiently

The cut command in Linux is a utility for processing and extracting specific portions of lines from files or input streams. It is particularly useful for working with columns of data in text files or command output. Here’s a detailed explanation of how the cut command works, its options, and practical examples.

1. Basic Syntax

The basic syntax of the cut command is:

Bash
cut [OPTION]... [FILE]...
  • OPTION: Flags and arguments specifying how to process the input.
  • FILE: The file(s) to read from. If not specified, cut reads from the standard input.

2. Common Options

2.1. -b (Byte Selection)

The -b option selects specific bytes from each line.

  • Syntax: cut -b [LIST] [FILE]
  • LIST: Specifies the byte positions to be cut. Ranges can be specified using -, and multiple ranges can be separated by commas.
  • Example: Extract the first 5 bytes from each line of file.txt:
Bash
  cut -b 1-5 file.txt
  • Example: Extract bytes 1, 4, and 7 from each line:
Bash
  cut -b 1,4,7 file.txt

2.2. -c (Character Selection)

The -c option selects specific characters from each line.

  • Syntax: cut -c [LIST] [FILE]
  • LIST: Specifies the character positions to be cut. Ranges and multiple values can be specified.
  • Example: Extract characters 1 through 5 from each line:
Bash
  cut -c 1-5 file.txt
  • Example: Extract characters 1, 4, and 7:
Bash
  cut -c 1,4,7 file.txt

2.3. -d (Delimiter Specification)

The -d option specifies a delimiter to use when extracting fields based on columns.

  • Syntax: cut -d [DELIM] -f [FIELDS] [FILE]
  • DELIM: The delimiter separating fields. The default delimiter is the tab character.
  • FIELDS: Specifies the field numbers to be extracted.
  • Example: Extract the first and third fields from a comma-separated file:
Bash
  cut -d ',' -f 1,3 file.txt
  • Example: Extract the second field from a tab-separated file (default delimiter):
Bash
  cut -f 2 file.txt

2.4. -f (Field Selection)

The -f option selects specific fields from each line. This option works in conjunction with -d.

  • Syntax: cut -d [DELIM] -f [FIELDS] [FILE]
  • FIELDS: Specifies the field numbers to be cut. Ranges and multiple fields can be specified.
  • Example: Extract fields 2 through 4 from a file separated by commas:
Bash
  cut -d ',' -f 2-4 file.txt
  • Example: Extract fields 1 and 3:
Bash
  cut -d ',' -f 1,3 file.txt

2.5. --complement

The --complement option is used to output the parts of the line that are not selected.

  • Syntax: cut --complement -d [DELIM] -f [FIELDS] [FILE]
  • Example: Output all fields except the first field from a comma-separated file:
Bash
  cut --complement -d ',' -f 1 file.txt

3. Practical Examples

3.1. Extracting Columns from a CSV File

Suppose data.csv has the following content:

Bash
name,age,city
John,30,New York
Alice,25,Los Angeles
Bob,35,Chicago
  • Extract names and ages:
Bash
  cut -d ',' -f 1,2 data.csv

Output:

Bash
  name,age
  John,30
  Alice,25
  Bob,35

3.2. Extracting Specific Characters

Suppose data.txt contains:

Bash
abcdefgh
ijklmnop
qrstuvwx
  • Extract characters 3 through 6:
Bash
  cut -c 3-6 data.txt

Output:

Bash
  cdef
  lmnop
  stuv

3.3. Extracting Byte Positions

Suppose data.txt contains:

Bash
1234567890
abcdefghij
klmnopqrst
  • Extract bytes 2 through 5:
Bash
  cut -b 2-5 data.txt

Output:

Bash
  2345
  bcde
  lmn

4. Using cut with Pipes

The cut command is often used in conjunction with pipes to process output from other commands.

  • Example: List all files in a directory and extract the first 10 characters of each filename:
Bash
  ls -1 | cut -c 1-10
  • Example: Extract the second field from the output of a ps command:
Bash
  ps aux | cut -d ' ' -f 2

5. Summary

The cut command is a versatile tool for extracting specific portions of text from files or command outputs. Its options for byte, character, and field selection, combined with the ability to specify delimiters and complement selections, make it a powerful utility for processing and manipulating text data. Whether you’re working with structured data files, command outputs, or raw text, cut can help you extract and format the information you need efficiently.

Share
OpenLib .

OpenLib .

The Founder - OpenLib.io

You may also like...