How to use the Linux cut command with a field and delimiter

Should you ever run into a situation where you want to use the Linux cut command by specifying both a field number and field delimiter, I hope this example is helpful for you.

I was just working on a problem where I wanted to list all the fonts on a Mac OS X (Unix) system, and needed to use the cut command this way. A straight listing of all the filenames in the Mac font directory gave me a long list of names like this:

$ ls -1

AScore.ttf.11904_0.ATSD
AScore.ttf.11904_0.fontinfo
AScoreParts.ttf.122A0_0.ATSD
AScoreParts.ttf.122A0_0.fontinfo
Abadi MT Condensed Extra Bold.0_11005.ATSD
Abadi MT Condensed Extra Bold.0_11005.fontinfo

(There are actually 761 files on my current system, so I omitted a lot of output.)

For my needs, I just wanted the first part of each filename, i.e., the text string to the left of the first decimal in each filename, like this:

AScore
AScoreParts
Abadi MT Condensed Extra Bold

In short, to create this list, I used the following Unix cut command, specifying the desired field number and field delimiter:

$ ls -1 | cut -f1 -d'.'

This command can be read like this:

  • Create a single column list of all files in the current directory.
  • From that list, only print the first field of each filename, where the field delimiter is the "." character.

Running this command gave me the output I wanted:

AScore
AScoreParts
Abadi MT Condensed Extra Bold

More cut command examples

As a few more quick cut command examples, had I wanted to print the second field of each filename, I would have used this command:

$ ls -1 | cut -f2 -d'.'

If it made sense to use a space as the field delimiter, my cut command would have looked like this:

$ ls -1 | cut -f2 -d' '

And for something a little different, if I wanted to print all the usernames out of the /etc/passwd file on my Unix system, I can use this cut command:

$ cut -f1 -d: /etc/passwd

In that example, I'm again printing field one, but this time I'm using the ":" character as the field delimiter, and I'm reading directly from a file named /etc/passwd, instead of reading from standard input.

In summary, I hope these cut command examples have been helpful. As usual, if you have any questions or comments, just leave a note in the Comments section below.