Rodolfo Bandeira

Rodolfo Bandeira

software, electronics, security, devops, thoughts

Shellscript Command Cut

With the command cut in bash you can extract some part of a string (characters or bytes). Let’s stop talking and put our hands on some examples:

rodolfo@ubuntu:/tmp$ cut -b1-6 ajax_clock_small.gif | head -n 1
 GIF89a

The command cut the first byte until the byte number six from a binary file. In this case a image file. The command pipe | on the and of the string is to extract only the first row. Yeah, the cut command goes thought all lines of the file.

If you use -c instead of -b you will got the same result. “GIF89a”

You can also use a -d option to set a delimitation character. If you are using the -d option, you can get the fields in something like a array of elements. Just using the option -f and specifying the column.

rodolfo@ubuntu:/$ cut -d: -f1,2,3 /etc/passwd | head -n 3
 root:x:0
 daemon:x:1
 bin:x:2
 rodolfo@ubuntu:/$

Last modified:

LinkedIn
WhatsApp