This tutorial explains Linux “cut” command, options and its usage with examples.
Print selected parts of lines from each FILE to standard output.
cut [OPTION]… [FILE]…
output only these bytes
output only these characters
use DELIM instead of TAB for field delimiter
output only these fields; also print any line that contains no delimiter character, unless the -s option is specified
with -b: don’t split multibyte characters
do not print lines not containing delimiters
use STRING as the output delimiter the default is to use the input delimiter
display this help and exit
output version information and exit
Use one, and only one of -b, -c or -f. Each LIST is made up of one range, or many ranges separated by commas. Each range is one of:
N’th byte, character or field, counted from 1
from N’th byte, character or field, to end of line
from N’th to M’th (included) byte, character or field
from first to M’th (included) byte, character or field
With no FILE, or when FILE is -, read standard input.
For most of the example, we’ll be using the following test file.
$ cat test.txt cat command for file oriented operations. cp command for copy files or directories. ls command to list out files and directories with its attributes.
1. Select Column of Characters
To extract only a desired column from a file use -c option.
$ cut -c5 test.txt c o o
2. Select Column of Characters using Range
$ cut -c2-4 test.txt at p c s c
3. Select Column of Characters using either Start or End Position
$ cut -c2- test.txt at command for file oriented operations. p command for copy files or directories. s command to list out files and directories with its attributes.
This example extracts from 2nd character to end of each line from test.txt file.
$ cut -c-2 test.txt ca cp ls
This example extracts 2 characters from the beginning of each line from test.txt file.
4. Specifying delimiter
The following example displays only first field of each lines from /etc/passwd file using the field delimiter : (colon). In this case, the 1st field is the username.
$ cut -d':' -f1 /etc/passwd root daemon bin sys sync games man lp mail news uucp proxy www-data backup list irc gnats libuuid syslog messagebus avahi-autoipd usbmux dnsmasq whoopsie kernoops rtkit speech-dispatcher lightdm avahi colord pulse hplip saned abc nobody
5. Select Multiple Fields from a File
Below example displays username and home directory of users who has the login shell as “/bin/bash”.
$ grep "/bin/bash" /etc/passwd | cut -d':' -f1,6 root:/root abc:/home/abc
6. Select All Fields Except the Specified Fields
The following example displays all the fields from /etc/passwd file except field 7
$ grep "/bin/bash" /etc/passwd | cut -d':' --complement -s -f7 root:x:0:0:root:/root abc:x:1000:1000:abc,,,:/home/abc
7. Change Output Delimiter for Display
To change the output delimiter use the option –output-delimiter as shown below. In this example, the input delimiter is : (colon), but the output delimiter is # (hash).
$ grep "/bin/bash" /etc/passwd | cut -d':' -s -f1,6,7 --output-delimiter='#' root#/root#/bin/bash abc#/home/abc#/bin/bash
If output delimiter is new line, then output for a single user will be
$ grep abc /etc/passwd | cut -d':' -f1,6,7 --output-delimiter=$'\n' abc /home/abc /bin/bash
8. Select Fields Only When a Line Contains the Delimiter
In our /etc/passwd example, if you pass a different delimiter other than : (colon), cut will just display the whole line.
In the following example, we’ve specified the delimiter as | (pipe), and cut command simply displays the whole line, even when it doesn’t find any line that has | (pipe) as delimiter.
$ grep "/bin/bash" /etc/passwd | cut -d'|' -f1 root:x:0:0:root:/root:/bin/bash abc:x:1000:1000:abc,,,:/home/abc:/bin/bash
But, it is possible to filter and display only the lines that contains the specified delimiter using -s option.
The following example doesn’t display any output, as the cut command didn’t find any lines that has | (pipe) as delimiter in the /etc/passwd file.
$ grep "/bin/bash" /etc/passwd | cut -d'|' -s -f1
9. Create a new file that contained just the username (field 1) and the user ID (UID)
$ cut -d: -f 1,3 /etc/passwd > uidpid.txt $cat uidpid.txt root:0 daemon:1 bin:2 sys:3 sync:4 games:5 man:6 lp:7 mail:8 news:9 uucp:10 proxy:13 www-data:33 backup:34 list:38 irc:39 gnats:41 libuuid:100 syslog:101 messagebus:102 avahi-autoipd:103 usbmux:104 dnsmasq:105 whoopsie:106 kernoops:107 rtkit:108 speech-dispatcher:109 lightdm:110 avahi:111 colord:112 pulse:113 hplip:114 saned:115 abc:1000 nobody:65534
Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.