
The cut command can be used to cut certain characters of text. For example, let's cut the output of the /etc/passwd file.
~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
JohnDoe:x:1000:1000:JohnDoe:/home/JohnDoe:/bin/bash
Cut one character from each line
The -c or --characters option can be used to cut the nth character from every line. In this example, the first character from every line is cut.
~]# cat /etc/passwd | cut -c 1
r
J
Cut a range of characters
A range of characters can be cut. In this example, the first, second and third character from every line is cut.
~]# cat /etc/passwd | cut -c 1-4
root
John
The -b or --bytes option produces similar output.
~]# cat /etc/passwd | cut -b 1-4
root
John
Cut a field
The -d or --delimiter and -f or --fields options can be used to cut fields from a file. The /etc/password file is delimited with the : character and then the first field is displayed.
~]# cat /etc/passwd | cut -d ':' -f 1
root
JohnDoe
The cut command struggles to cut fields that do not have a consistent delimiter. The awk command can cut fields where the delimiter is not consistent.
[root@server1 ~]# df -h | awk '{print $5}'
Size
65%
22%
Cut lines
The awk command is better suited to cut lines than the cut command. In this example, line 1 is cut.
~]# cat /etc/passwd | awk 'NR!=1'
JohnDoe:x:1000:1000:JohnDoe:/home/JohnDoe:/bin/bash
Did you find this article helpful?
If so, consider buying me a coffee over at