cut and grep are two of the most useful and common commands for a CentOS Administrator. The cut is extremely useful for dealing with delimited files such as Linux configuration files, Linux preference files, and CSV files.
Switch | Action |
-b | Select only these bytes |
-c | Select only these characters |
-d | Use DELIM instead of TAB for the field delimiter |
-s | Only print delimited lines |
Most times, cut will be used to extract specific rows out of text files. Previously, we have used cut to get a listing of all users from /etc/passwd −
[root@centosLocal centos]# cut -d”:” -f1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
Above is a digested list of system users from /etc/passwd.
Some Linux utilities and applications actually save the output with the functionality of cut in mind. Following is an example of nmap output.
[root@centosLocal centos]# grep open ./http_scans.txt
Host: 10.58.52.67 () Ports: 80/open/tcp//http///
Host: 10.58.52.132 () Ports: 80/open/tcp//http///
Host: 10.58.52.133 () Ports: 80/open/tcp//http///
Host: 10.58.52.56 () Ports: 80/open/tcp//http///
Host: 10.58.52.71 () Ports: 80/open/tcp//http///
Host: 10.58.52.132 () Ports: 80/open/tcp//http///
With the cut, we can quickly generate a list of internal systems with port 80 listenings for outside requests.
[root@centosLocal centos]# grep open ./http_scans.txt | cut -d” ” -f2 >
open_http_servers.txt
[root@centosLocal centos]# head open_http_servers.txt
10.58.52.17
10.58.52.29
10.58.52.30
10.58.52.36
10.58.52.59
10.58.53.89
10.58.53.100
10.58.54.103
10.58.54.148
10.58.54.152
[root@centosLocal centos]#
Cut can also be used by character count.
[root@centosLocal centos]# cut -c 1,2,3,4,5,6,7,8 lanIP-range.txt
10.58.52
10.58.52
10.58.52
10.58.52
10.58.52
10.58.52
10.58.53
10.58.53
10.58.53
10.58.53
10.58.53
10.58.54
10.58.54
10.58.54
10.58.54
10.58.54
10.58.54
10.58.54
10.58.54
10.58.54
10.58.54
10.58.54
10.58.54
10.58.54
10.58.54
10.58.54
10.58.54
[root@centosLocal centos]#
The cut is a command that will be used almost daily by a CentOS Administrator. It is a lifesaver for parsing text and some binary files.