NIX Cut Command

This cut command is a wonderful time saver when you need it. AND YOU WILL NEED IT! To understand its simplest form, imagine you have a stick of butter in front of you as well as a butter knife. Unless you have an awesome metabolism, you probably don't want to consume the entire stick of butter in one sitting (although this is entirely possible when paired with a fresh loaf of sourdough bread.)

Your text / data file is your butter and the "cut" command is your butter knife. Some butter vendors actually provide marks right on the packaging itself as a measuring aide to cut the perfect chunk of butter. This is an apt metaphor for the cut command when using the -c parameter. In the first example, we're actually cutting a chunk out of the middle of the file, and of course this is entirely possible with an actual stick of butter as well, though not recommended :)

Example 1: Simple

This is a hypothetical example to demonstrate the simplest use of cut but would not always be the best approach. Let's say there is this text file containing a dump from the ls -ltr command and we wanted only the date and time extracted. A couple of ways to do it would be:

  1. Edit the file manually in vi / vim.
  2. Cat and pipe it through "awk" printing only fields 6, 7, and 8.
  3. Use the cut command explicitly or pipe it through the cut command.

Went with 3 of course and using cut explicitly :)

The first command shows the contents of files.txt. It appears that the date and timestamp appears on a fixed column boundary so this would be a piece of cake with cut. For this example we'll be cutting on exact column boundaries, so either count the columns or guestimate. Either way, we'll need to slice out from columns 31 to 42 and we told cut to do this by speccing the command line parameter -c31-42. Yes. It's that simple!

      jtpresta.com> cat files.txt
-rw-rw-r-- 1 owner group 3284 May 30 02:39 animal-alphabets-2016b.yaml
-rw-rw-r-- 1 owner group 3022 May 31 12:17 deal-with-the-reality-on-the-canvas.yaml
-rw-rw-r-- 1 owner group 1526 May 31 12:17 discovery.yaml

jtpresta.com> cut -c31-42 files.txt
May 30 02:39
May 31 12:17
May 31 12:17

TODO: Add more examples!

Cheers!