Cut command in Linux

Cut Command : Filtering What You Want

Table of Contents
    Add a header to begin generating the table of contents

    Introduction

    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.

    cut –d -f

    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.

    Fig 1 : Contents of/usr/password

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

    cut –d”:” –f1 cut_demo

    Fig 2 : 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. Like cut command in linux let us explore more useful commands in future posts.

    Share this material with your friend:

    Leave a Comment