GNU cut Cheatsheet
#
GNU cut
is a command-line utility for cutting sections from each line of files.
Basic Usage
#
Command/Option | Example | Description |
---|
cut -f1 -d',' file.txt | cut -f1 -d',' data.csv | Extract the first field of a comma-separated file |
cut -c1-5 file.txt | cut -c1-5 textfile.txt | Extract the first 5 characters of each line |
cut -d':' -f2 file.txt | cut -d':' -f2 /etc/passwd | Extract the second field from a colon-separated file |
Field Separator
#
Command/Option | Example | Description |
---|
cut -d',' -f1 file.csv | cut -d',' -f1 data.csv | Set comma as the field separator and extract the first field |
cut -d' ' -f2 file.txt | cut -d' ' -f2 textfile.txt | Set space as the field separator and extract the second field |
Character Positions
#
Command/Option | Example | Description |
---|
cut -c1-10 file.txt | cut -c1-10 textfile.txt | Extract characters from position 1 to 10 |
cut -c5- file.txt | cut -c5- textfile.txt | Extract characters from position 5 to the end of the line |
Complement
#
Command/Option | Example | Description |
---|
cut -d',' --complement -f2 file.csv | cut -d',' --complement -f2 data.csv | Extract all fields except the second one |
This cheatsheet provides essential GNU cut
commands for extracting specific parts of text files.