← Back to Bash Course | Chapter 11: Process Management | Lesson 5 of 12

cut

cut is like using scissors to snip out one column from a table of text. You tell it what separates the columns and which column number you want to keep.

In this page:

  1. cut
Syntax
bash
cut -d'delimiter' -f field_number
cut -c start-end

cut

cut extracts specific columns or character ranges from each line of input, using -d to set a delimiter and -f to choose fields.

It's a lighter-weight alternative to awk when you only need to pull out one or two fields. cut -c extracts by character position instead of delimiter-separated fields.

Note: Use cut -d, -f2 to extract the second comma-separated field from CSV-style lines.

Example: cut

bash
#!/bin/bash
echo "alice,30,engineer" | cut -d, -f1
echo "alice,30,engineer" | cut -d, -f2,3
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using cut -d with a multi-character delimiter, which is not allowed.
  2. Using -f fields on space separated data with multiple spaces, when cut treats each space as a delimiter.
  3. Forgetting the delimiter, so the default tab is used.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.