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:
Syntax
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
#!/bin/bash
echo "alice,30,engineer" | cut -d, -f1
echo "alice,30,engineer" | cut -d, -f2,3
Login to try C/C++/Java/PHP code in the editor
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
- Using
cut -dwith a multi-character delimiter, which is not allowed. - Using
-ffields on space separated data with multiple spaces, whencuttreats each space as a delimiter. - Forgetting the delimiter, so the default tab is used.
🔒
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: