How to use Linux cut Command

Linux cut Command with 10 Helpful Examples

The cut command in Linux is a handy tool for extracting specific parts of text from files.

It acts like scissors, letting you snip out sections of each line based on characters, bytes, or field separators (like commas or tabs).

This makes it useful for working with formatted text files and extracting the needed data.

Prerequisites to Use Linux cut Command

Provide the options below to let this tutorial work correctly:

  • A Server running Linux VPS.
  • A non-root user with sudo privileges.
  • Access to a terminal.

Syntax of cut Command

Here’s the basic syntax of the cut command:

cut [option] [file]

The cut command requires a [option] to work, otherwise, it throws an error.

For files, specify their names. Leaving it blank makes ”cut” read from your current command stream, acting as a filter. With multiple files, cut merges the desired content from each.

For files” directly addresses the file input.

Leaving it blank” emphasizes the empty argument for standard input.

Current command stream” replaces “standard input” for better understanding.

Acting as a filter” clarifies the behavior when used in pipes.

Merges the desired content” explains the behavior with multiple files.

Practical Examples to Use cut Command in Linux

Linux boasts a treasure trove of command-line tools; cut is a shining example. This handy tool lets you carve out specific data chunks from text files.

Let’s review the examples below to learn How to use Linux cut command.

1. Using cut to work with Characters

The cut command in Linux can extract specific characters from each file line using the -c (or --characters) option.

Here’s how to use it:

  • Syntax:
cut -c LIST [FILE]...
  • Explanation:

-c (or --characters): This option tells cut to work with character positions.

LIST: This is a comma-separated list that specifies which characters you want to extract. It can include:

  • Individual character positions (starting from 1)
  • Ranges of characters (e.g., 3-7)
  • Examples of cut command with Characters:

Extract the first character:

cut -c 1 filename.txt

To extract characters from positions 5 to 10:

cut -c 5-10 filename.txt

Extract every other character by running:

cut -c 1,3,5,7... filename.txt  # You can continue listing positions
  • Consider:

Be mindful of character encoding. Multi-byte characters (like some emojis) might not be extracted as expected with cut -c.

The cut command with -c treats tabs and spaces as characters.

2. Using cut to work with Bytes

The cut command in Linux allows you to extract specific bytes from each line of a file using the -b (or --bytes) option.

Here’s how to use it:

  • Syntax:
cut -b LIST [FILE]...
  • Explanation:

-b (or --bytes): This option tells cut to work with byte positions.

LIST: This is a comma-separated list that specifies which bytes you want to extract. It can include:

  • Individual byte positions (starting from 1)
  • Ranges of bytes (e.g., 3-7)
  • Examples of cut command with Bytes:

To extract the first byte:

cut -b 1 filename.txt

Extract bytes from positions 5 to 10 by running:

cut -b 5-10 filename.txt

Also, you can run the following command to extract every other byte (be cautious with character encoding):

cut -b 1,3,5,7... filename.txt  # You can continue listing positions
  • Consider:

Byte extraction is highly dependent on character encoding.

Unlike characters, a single byte might not represent a full character, especially with UTF-8 encoding. This can lead to unexpected results.

The cut command with -b treats all characters, including spaces and tabs, as sequences of bytes.

Byte extraction might not be suitable for human-readable text files due to encoding issues. Consider using -c (characters) for such scenarios.

If you need more control over byte manipulation or complex extraction based on byte values, tools like dd or custom scripts might be a better choice.

3. Using cut to work with Delimiter

The cut command in Linux shines when working with files containing data separated by delimiters like commas, tabs, or colons.

Here’s how to use it:

  • Syntax:
cut -d DELIMITER -f FIELD_NUMBER(S) [FILE]...
  • Explanation:

-d DELIMITER: This option specifies the delimiter character (or characters) that separates fields in your file. Replace DELIMITER with the actual delimiter used (e.g., -d "," for comma-separated files).

-f FIELD_NUMBER(S): This tells cut which field(s) to extract. You can specify:

  • Individual field number (starting from 1)
  • A range of fields (e.g., -f 2-4)
  • Multiple comma-separated field numbers (e.g., -f 1,3,5)
  • Examples of cut command with Delimiter: 

To extract the second field (comma-separated):

cut -d "," -f 2 filename.csv

Extract the first and third fields (tab-separated) by running:

cut -d "\t" -f 1,3 data.txt

Use the command below to extract all fields from the third field onwards (colon-separated):

cut -d ":" -f 3- filename.log
  • Consider: 

Use quotes around the delimiter if it contains special characters (e.g., -d ":").

cut operates on each line independently. It doesn’t process the entire file as a whole.

The default delimiter for cut is the tab character (\t).

  • Using Delimiters in cut:

When working with cut to extract specific fields from text files, delimiters play a crucial role.

They define the characters that separate the data within each line. Common delimiters include commas (,), tabs (\t), colons (:), or spaces.

  • Specifying the Input Delimiter (-d): 

The -d option in cut allows you to define the delimiter character used in your file.

You replace the DELIMITER in -d DELIMITER with the actual character separating the fields.

For example, -d ',' specifies commas as the delimiter.

  • Specifying the Output Delimiter (--output-delimiter):

By default, cut uses the input delimiter to separate the extracted fields in its output.

However, you can control this behavior using the --output-delimiter option (sometimes abbreviated as -O in some versions).

This option allows you to specify a different delimiter for the final output.

  • Syntax:
cut -d INPUT_DELIMITER -f FIELD_NUMBER(S) --output-delimiter OUTPUT_DELIMITER [FILE]...
  • Explanation:

-d INPUT_DELIMITER: Defines the delimiter used in the input file (e.g., , for commas).

-f FIELD_NUMBER(S): Specifies the field(s) to extract as usual.

--output-delimiter OUTPUT_DELIMITER: Defines the delimiter to be used for separating extracted fields in the output (e.g., - for hyphens).

  • Example:

Imagine a file named data.csv with comma-separated fields:

name,age,city

Alice,30,New York

Bob,25,London

To extract the second field (age) and separate them with semicolons (;) in the output, you can use:

cut -d ',' -f 2 --output-delimiter ';' data.csv

This will print:

30;25;
  • Benefits of Specifying an Output Delimiter:

Customization: You can tailor the output format to your needs, regardless of the input file’s delimiter.

Clarity: Using a specific delimiter can improve readability when combining data from different sources.

Integration: It allows seamless integration of cut‘s output with other tools that expect specific delimiters.

  • Consider:

By understanding both the input and output delimiters in cut, you gain greater control over how you extract and present data from your text files.

4. Using cut to work with Fields

In cut, fields refer to the sections of data separated by a delimiter character within each line of a file.

These delimiters can be commas, tabs, colons, spaces (depending on the file format), or any other character used to organize the data.

Using cut with Fields:

The cut command works with fields using the following options:

-d DELIMITER: This option specifies the delimiter character that separates the fields.

You replace DELIMITER with the actual character (e.g., -d "," for comma-separated files).

-f FIELD_NUMBER(S): This tells cut which field(s) to extract. You can specify:

  • Individual field number (starting from 1)
  • A range of fields (e.g., -f 2-4)
  • Multiple comma-separated field numbers (e.g., -f 1,3,5)
  • Examples of cut command with Fields

To extract the second field (comma-separated):

cut -d "," -f 2 filename.csv  # Extracts the second field from each line in filename.csv

Extract the first and third fields (tab-separated) by running:

cut -d "\t" -f 1,3 data.txt  # Extracts the first and third fields from each line in data.txt

Run the command below to extract all fields from the third field onwards (colon-separated):

cut -d ":" -f 3- filename.log  # Extracts all fields from the third field onwards in filename.log
  • Consider:

By specifying the delimiter and field number(s), you control which part of the data cut extracts from each line.

Remember, cut works independently on each line, not processing the entire file as a single unit.

The default delimiter for cut is the tab character (\t). However, you can use any character relevant to your file format.

By understanding fields and delimiters, you can effectively use cut to extract specific data from various formatted text files in Linux.

5. Using cut to work with Complement

The cut command in Linux offers a powerful option called --complement (or sometimes abbreviated as -C in some versions) that allows you to extract all except the specified fields from a file.

Here’s how to use it:

  • Syntax:
cut --complement -d DELIMITER -f FIELD_NUMBER(S) [FILE]...
  • Explanation:

--complement: This option tells cut to operate in complement mode.

-d DELIMITER: Similar to working with fields, this defines the delimiter character separating the data.

Replace DELIMITER with the actual character used in your file.

-f FIELD_NUMBER(S): This specifies the field(s) to exclude. You can use the same options as with regular field selection:

  • Individual field number (starting from 1)
  • A range of fields (e.g., -f 2-4)
  • Multiple comma-separated field numbers (e.g., -f 1,3,5)
  • Examples of cut command with Complement:

To extract all but the second field (comma-separated):

cut --complement -d "," -f 2 data.csv

This will print everything in each line except the data in the second field (assuming commas separate the fields).

Extract everything except fields 1 and 3 (tab-separated) by running:

cut --complement -d "\t" -f 1,3 user_info.txt

This will print all data in each line, excluding the content in the first and third fields (assuming tabs separate the fields).

Run the command below to extract only the first field (colon-separated, with a twist):

While you can achieve this with regular selection (cut -d ":" -f 1 filename.log), here’s how --complement can be used creatively:

cut --complement -d ":" -f 2- filename.log

This excludes all fields from the second field onwards (which essentially leaves only the first field)

  • Consider:

--complement is particularly useful when you know which fields you want to discard and want the remaining data combined.

Make sure the delimiter and field selections are accurate to avoid unexpected results.

By mastering cut with the --complement option, you can efficiently extract specific data patterns from your text files in Linux.

6. Using cut to extract lines containing a specific pattern with –complement

While cut is typically used for extracting specific fields, the --complement option can be used creatively to filter lines based on absent patterns.

Imagine you have a website access log file. You want to identify all requests that did not result in a successful response (status code 200).

Here’s how you can achieve this using cut with --complement:

grep -v ' 200$' access.log | cut --complement -d ' ' -f 9  # Assuming space is delimiter and status code is 9th field
  • Explanation:
grep -v ' 200$' access.log

This part uses grep to filter out lines containing ” 200$” (the success status code) from the access log.

|: The pipe symbol (|) sends the output of grep (lines without ” 200$ code”) to cut as input.

cut --complement -d ' ' -f 9

--complement: Instructs cut to operate in complement mode, extracting everything except the specified field.

-d ' ': Specifies the space () as the delimiter between fields.

-f 9: Tells cut to exclude the ninth field (assuming the status code is in the ninth position).

This will effectively print all lines from the access log that don’t have a status code of 200, essentially identifying requests that weren’t successful.

7. Using cut to work with Piping

The linux cut command excels at working within the powerful concept of piping in the shell.

Piping allows you to send the output of one command as the input for another, creating a data processing chain.

Here’s how cut integrates with piping:

Imagine you have a file named system_info.txt containing lines like:

user:name1 CPU:2.5GHz RAM:16GB
user:name2 CPU:3.2GHz RAM:8GB

You want to extract just the RAM values from each line.

  • Solution with Piping:

Use ps -eo pid,user,%cpu,%mem (replace with your actual command to get system info) to get process information:

ps -eo pid,user,%cpu,%mem

Pipe the output to cut to extract the fourth field (assuming %mem is the fourth column):

ps -eo pid,user,%cpu,%mem | cut -d ' ' -f 4
  • Explanation:

ps -eo pid,user,%cpu,%mem generates output with space delimiters.

The pipe (|) symbol sends this output to cut as its input.

cut -d ' ' specifies the space () as the delimiter.

-f 4 tells cut to extract the fourth field (containing RAM values).

  • Benefits of Piping with cut:

Efficiency: You can chain multiple commands with pipes for complex data processing.

Flexibility: cut can be used within pipes to extract specific data from various command outputs.

Modular Design: Each command focuses on a specific task, making the process easier to understand and maintain.

  • Consider: 

Ensure the delimiter (-d) used in cut matches the actual delimiter in the piped output.

By mastering piping with cut, you can unlock powerful data extraction capabilities in your Linux shell environment.

8.  Using cut with skip (-s):

The -s option allows you to specify the number of leading fields to skip before starting extraction.

This can be useful when you want to extract data from a specific position onwards, regardless of the number of preceding fields.

For example, to extract everything from the third field onwards in a comma-separated file, you could use:

cut -d ',' -s 2 filename.csv

9. Using cut with range specifiers (-m and -n):

The -m and -n options allow you to specify the character range to extract. While -c works with byte positions, -m and -n operate on character positions.

-m: This option extracts a maximum of m characters from the beginning of the line.

-n: This option extracts exactly n characters from the beginning of the line.

For instance, to extract the first 10 characters of each line in a file, you could use:

cut -c -n 10 filename.txt

10. Combining options:

The power of cut lies in its ability to combine different options.

For example, you can use -d and -f with -m or -n to extract specific data within a particular field.

Suppose you have a log file with timestamps followed by data separated by spaces. You can extract only the date portion (first 10 characters) from the timestamp using:

cut -d ' ' -f 1 -c -n 10 filename.log

Conclusion

The cut command offers a powerful and versatile toolbox for manipulating and extracting data from text files in Linux.

From working with characters, bytes, and delimiters to filtering lines based on specific patterns, cut empowers you to efficiently process information and tailor the output to your needs.

By incorporating the various options and combining them with piping, you can unlock a wide range of possibilities for data manipulation tasks within your Linux environment.

Leave a Reply

Your email address will not be published. Required fields are marked.