The cut command extracts certain portion of text from each line of input string or from a file by characters, delimiter, complement, and fields options.
I have created dummy content for you to test all the above options.
1 2 3 4 5 6 Welcome to Ubuntu 18.04.5 LTS (GNU/Linux 4.15.0-117-generic x86_64) root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/bin/sh bin:x:2:2:bin:/bin:/bin/sh sys:x:3:3:sys:/dev:/bin/sh sync:x:4:65534:sync:/bin:/bin/sync
Get options listed above and put them one by one to the following structure.
You have 3 options to run the
cut command. In this article, I will be using option 1 most of the time but you are free to choose other options as well.
1 2 3 4 5 6 7 8 # option 1 cut [options] filename.txt # option 2 cat filename.txt | cut [options] # option 3 <ANY_LINUX_COMMAND> | cut [options]
To extract specific characters only, use
-c option. The following command will return the second character of each line.
1 cut -c 2 filename.txt
To extract specific characters between 3 to 7 range can also be done with
-c option by specifying start and end position delimited with a hyphen
-. The following command extracts characters between 3 to 7 range of each line.
Note: start or end position both are optional. I will show you in a bit.
1 cut -c 3-7 filename.txt
To print everything except characters between 3 to 7 range, use
1 cut --complement -c 3-7 filename.txt
To extract from the 7th character to the end of each line can also be done with
-c option. In the following command end position is optional.
1 cut -c 7- filename.txt
To extract from the beginning till 7th characters of each line can also be done with
-c option. In the following command, start position is optional.
1 cut -c -7 filename.txt
To extract an entire field, you can combine
-fnumber of fields you want to extract.
-dwhich field delimiter you want to use to extract.
Run the following command to get the 1st field of each line by using
: (colon) as the field delimiter.
1 cut -d ':' -f 1 filename.txt
To extract multiple fields use comma-separated value
,. The following command extract 1st, 3rd, and 7th fields.
1 cut -d ':' -f 1,3,7 filename.txt
To extract a range of fields uses hyphen separated value
-. The following command extracts fields between 2 to 6 range of each line.
1 cut -d ':' -f 2-6 filename.txt
To extract multiple fields separated by comma
, + range of fields separated by a hyphen
- which will return 1 through 4, 6, and 7 fields as follows.
1 cut -d ':' -f 1-4,6,7 filename.txt
To extract from 7th field to end of each line. In the following command end position is optional.
1 cut -d ':' -f 7- filename.txt
To extract from beginning till 7th field of each line. In the following command, start position is optional.
1 cut -d ':' -f -7 filename.txt
To change the default delimiter, use
--output-delimiter option. In the following command, find
: (colon) and replace it with
# (hash) delimiter.
1 cut -d ':' -f 1,6,7 --output-delimiter='#' filename.txt
You can also replace each field with
\n newline instead of
1 cut -d ':' -f 1,6,7 --output-delimiter=$'\n' filename.txt