Cut command : Filtering what you want

As a sequel to the previous article about Split command, thought of sharing another useful command called ‘cut’ that is available in Linux environment.

Similar to split, often we run into some situations where configuration to be read from a particular file which is delimited with some identifiers (ex: comma or colon symbol) where the cut command comes into picture. While the cut offers multiple options, for simple understanding purpose, the following syntax helps to split based on the colon as delimiter.

  1.                                 cut –d <delimiter> -f<field name>

Let us take for example cut_demo file (which is a local copy of /etc/password) file in linux which is de-limited by “:” as delimiters, which can be split as follows.

cut_screenshot_1
Fig1: Contents of /usr/password

Suppose we are only interested in the first field (that is name field) that can be obtained as follows:

  1.                                 cut –d”:” –f1 cut_demo
cut_screenshot_2
Fig2: Obtaining only username using cut command

The output of the command is nothing but the username, cut form the file /etc/password is displayed in the standard output. A sample output provided below. By re-directing it to a different file, further processing on username can be done by a user program. While the split command helps the user to partition contents at file level, the cut command helps and programmer to partition the contents from inside the file, using particular delimiters.

I hope you are able to appreciate practical usage of split and cut command in the past two blog posts. Let us explore more useful commands in future posts.

About

View all posts by

Leave a Reply

Your email address will not be published. Required fields are marked *