≡ Menu

10 Practical Linux Cut Command Examples to Select File Columns

Linux command cut is used for text processing. You can use this command to extract portion of text from a file by selecting columns.

This tutorial provides few practical examples of cut command that you can use in your day to day command line activities.

For most of the example, we’ll be using the following test file.

$ cat test.txt
cat command for file oriented operations.
cp command for copy files or directories.
ls command to list out files and directories with its attributes.

1. Select Column of Characters

To extract only a desired column from a file use -c option. The following example displays 2nd character from each line of a file test.txt

$ cut -c2 test.txt
a
p
s

As seen above, the characters a, p, s are the second character from each line of the test.txt file.

2. Select Column of Characters using Range

Range of characters can also be extracted from a file by specifying start and end position delimited with -. The following example extracts first 3 characters of each line from a file called test.txt

$ cut -c1-3 test.txt
cat
cp
ls

3. Select Column of Characters using either Start or End Position

Either start position or end position can be passed to cut command with -c option.

The following specifies only the start position before the ‘-‘. This example extracts from 3rd character to end of each line from test.txt file.

$ cut -c3- test.txt
t command for file oriented operations.
 command for copy files or directories.
 command to list out files and directories with its attributes.

The following specifies only the end position after the ‘-‘. This example extracts 8 characters from the beginning of each line from test.txt file.

$ cut -c-8 test.txt
cat comm
cp comma
ls comma

The entire line would get printed when you don’t specify a number before or after the ‘-‘ as shown below.

$ cut -c- test.txt
cat command for file oriented operations.
cp command for copy files or directories.
ls command to list out files and directories with its attributes.

4. Select a Specific Field from a File

Instead of selecting x number of characters, if you like to extract a whole field, you can combine option -f and -d. The option -f specifies which field you want to extract, and the option -d specifies what is the field delimiter that is used in the input file.

The following example displays only first field of each lines from /etc/passwd file using the field delimiter : (colon). In this case, the 1st field is the username. The file

$ cut -d':' -f1 /etc/passwd
root
daemon
bin
sys
sync
games
bala

5. Select Multiple Fields from a File

You can also extract more than one fields from a file or stdout. Below example displays username and home directory of users who has the login shell as “/bin/bash”.

$ grep "/bin/bash" /etc/passwd | cut -d':' -f1,6
root:/root
bala:/home/bala

To display the range of fields specify start field and end field as shown below. In this example, we are selecting field 1 through 4, 6 and 7

$ grep "/bin/bash" /etc/passwd | cut -d':' -f1-4,6,7
root:x:0:0:/root:/bin/bash
bala:x:1000:1000:/home/bala:/bin/bash

6. Select Fields Only When a Line Contains the Delimiter

In our /etc/passwd example, if you pass a different delimiter other than : (colon), cut will just display the whole line.

In the following example, we’ve specified the delimiter as | (pipe), and cut command simply displays the whole line, even when it doesn’t find any line that has | (pipe) as delimiter.

$ grep "/bin/bash" /etc/passwd | cut -d'|'  -f1
root:x:0:0:root:/root:/bin/bash
bala:x:1000:1000:bala,,,:/home/bala:/bin/bash

But, it is possible to filter and display only the lines that contains the specified delimiter using -s option.

The following example doesn’t display any output, as the cut command didn’t find any lines that has | (pipe) as delimiter in the /etc/passwd file.

$ grep "/bin/bash" /etc/passwd | cut -d'|' -s -f1

7. Select All Fields Except the Specified Fields

In order to complement the selection field list use option –complement.

The following example displays all the fields from /etc/passwd file except field 7

$ grep "/bin/bash" /etc/passwd | cut -d':' --complement -s -f7
root:x:0:0:root:/root
bala:x:1000:1000:bala,,,:/home/bala

8. Change Output Delimiter for Display

By default the output delimiter is same as input delimiter that we specify in the cut -d option.

To change the output delimiter use the option –output-delimiter as shown below. In this example, the input delimiter is : (colon), but the output delimiter is # (hash).

$ grep "/bin/bash" /etc/passwd | cut -d':'  -s -f1,6,7 --output-delimiter='#'
root#/root#/bin/bash
bala#/home/bala#/bin/bash

9. Change Output Delimiter to Newline

In this example, each and every field of the cut command output is displayed in a separate line. We still used –output-delimiter, but the value is $’\n’ which indicates that we should add a newline as the output delimiter.

$ grep bala /etc/passwd | cut -d':' -f1,6,7 --output-delimiter=$'\n'
bala
/home/bala
/bin/bash

10. Combine Cut with Other Unix Command Output

The power of cut command can be realized when you combine it with the stdout of some other Unix command.

Once you master the basic usage of cut command that we’ve explained above, you can wisely use cut command to solve lot of your text manipulation requirements.

The following example indicates how you can extract only useful information from the ps command output. We also showed how we’ve filtered the output of ps command using grep and sed before the final output was given to cut command. Here, we’ve used cut option -d and -f which we’ve explained in the above examples.

$ ps axu | grep python | sed 's/\s\+/ /g' | cut -d' ' -f2,11-
2231 /usr/bin/python /usr/lib/unity-lens-video/unity-lens-video
2311 /usr/bin/python /usr/lib/unity-scope-video-remote/unity-scope-video-remote
2414 /usr/bin/python /usr/lib/ubuntuone-client/ubuntuone-syncdaemon
2463 /usr/bin/python /usr/lib/system-service/system-service-d
3274 grep --color=auto python
Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • Jalal Hajigholamali June 6, 2013, 8:48 am

    Hi,

    simple and very nice and useful article

  • Bob June 6, 2013, 9:41 am

    Great article.

  • Srinivas Jupudi June 7, 2013, 12:22 am

    Nice Article

  • niraj June 7, 2013, 12:28 am

    Nice artical

  • Erlo June 7, 2013, 1:07 pm

    Linux/Unix has lots of nice little utilities :o) I was not aware of cut, I have always used awk for these things. awk combined with sed is a kind of swiss pocket knife.

  • xlob June 7, 2013, 8:01 pm

    Thanks for article.
    @Erlo For substitution awk can use gsub internal command instead of pipe into sed ?

  • ASHOK July 4, 2013, 10:01 pm

    I wish to extract specific data from a line
    I mean
    Input : OA.jsp?page=/hello/abc/123/a23/sam/store/web/helloworld&xyz=YES
    Output: OA.jsp?page=/hello/abc/123/a23/sam/store/web/helloworld

    & should act as delimiter. The length of the link may change.
    What ever the link I pass the data before & should display.

    How it is possible?

  • John Joseph September 4, 2013, 1:11 am

    Well written and it has been useful for me. Thank you.

  • Sandeep October 23, 2013, 10:55 pm

    Hi
    Please can you do me favor. I need a script to get the information of all hosts running on network like.
    Name =Home user
    Disk usage space =
    Disk Available space =
    Disk Total space =
    IP Address =

  • Nitin November 21, 2013, 4:19 am

    Dear Ashok,

    You can use the below script for your required O/P:

    cat input.txt | cut -d’&’ -f1

  • Amani Musomba February 19, 2014, 11:39 am

    One of the best articles I have found on the cut command..
    Thanks for posting this..

  • sushil March 23, 2014, 11:17 pm

    With this command $ cut -c- test.txt
    I am not getting any out.
    Iam using fedora 19

  • Deiveegaraja March 31, 2014, 5:48 am

    Thanks..

  • Maniraja May 9, 2014, 11:51 pm

    Hi Sushil,

    we need to mention the range while using cut with -c option.
    like cut -c 1,5 test.txt.and cut -c 5- test.txt.

    In the first e.g it will print the first five characters of each line.
    In the second one, it will print the characters from 5 to the end of the line of each line.

  • Deepak Jadli October 27, 2014, 5:49 am

    It’s a proper and perfect docs 🙂
    Thanks

  • Devi Killada October 28, 2014, 11:53 am

    Just now I was trying to google the practical examples of cut, got your blog. Revised all commands at once. Thanks for posting Bala.

  • tony November 5, 2014, 5:18 pm

    I have a little problem how would i solve this? its an assignment for school

    Your current directory is sample_dir. Display cars2 sorted numerically by the 4th field:

    i understand i need to use the cut command just don’t know how

  • saurabh November 10, 2014, 8:52 am

    Simple and effective article.

  • Hafiz Muhammad Shafiq March 11, 2015, 7:14 am

    find before first space and count
    cat hdfs-info.log | grep USE_CrawlData | cut -d ‘ ‘ -f1 | awk ‘{ sum += $1 } END { print sum }’

  • venkatesh April 7, 2015, 1:00 am

    nice article,good stuff !

  • soumyak May 31, 2015, 9:22 pm

    Suppose i have a file called test.txt and within that files contents are like below
    abc123defghij
    345abckhhvds
    mnggjabc74r2
    here i want to print ony abc (like below) , how can we do this?
    abc
    abc
    abc

  • Sumit June 25, 2015, 12:51 am

    How to select one hole column?

  • MikeC August 7, 2015, 6:20 am

    Great article. I’ve been using awk when cut would often do the trick for me far more simply. Probably a simple one here but, where you set delimiter -d’x’ … I can set a space and that works fine. But what if there are one or more spaces between fields. Seems the -f varies by number of spaces it hits. Wondering if it be smart enough to delimit on ‘ ‘+ or something like that. Thanks,

  • suranjan August 27, 2015, 4:43 am

    Very useful “Thank You”

  • Alex September 3, 2015, 7:00 am

    Hi, the question is: may I pipe output of cut to another cut?
    I use UnxUtils under windows 7 and when I tries to redirect the output of the first cut to the second I get next notification
    grep -E “” file.txt | cut -d ‘ ‘ -f 2- | cut -d ‘ ‘ -f 3
    cut: only one type of list may be specified
    Try ‘cut –help’ for more information.
    Thanks

  • Vinod Jakhar December 29, 2015, 6:46 am

    So simple, So useful, Thanks

  • ashish January 2, 2016, 6:28 pm

    Hi ,

    thanks for this , this was very simple and easy to understand
    this is useful in our daily tasks which we do as admin

  • Mani January 20, 2016, 4:31 am

    Great … helped me a lot to understand the delimiter 🙂

  • Mani January 20, 2016, 6:11 am

    Great , it helped me a lot to understand specially delimiter 🙂

  • JVJ February 7, 2016, 5:45 am

    To get IP Address from ifconfig command using cut,

    /sbin/ifconfig |grep Bcast|cut -d’:’ -f2|cut -d’ ‘ -f1

  • Praylin March 28, 2016, 12:20 pm

    Hey Ramesh,
    Thanks for this article. It is much simple and helpful for understanding the cut command.

  • Niraj May 12, 2016, 1:23 am

    Help full information of cut command

  • oldefoxx May 23, 2016, 2:41 pm

    Itit possible to retain just the last field when the delimiter is space (-d’ “) and there are an unknown number of spaces before the last term? I know that awk has a last field number reference, but cut is so much simpler in my mind. I did find that repeated cuts using pipe | works, but that is the really hard way to get it done.

  • FX February 10, 2017, 5:33 pm

    Very good article. Thanks !

  • corey lawson February 15, 2017, 2:49 pm

    This would be cool to get working in Windows, using the gnuwin32 versions of those Unix command line utilities.
    But I fear that Window’s close-but-no-cigar world of the CMD shell would require all sorts of command line noddle bending to work. CMD’s quoting rules are difficult enough to wrestle with compared to Unix/Linux shells.
    Loading the bash shell for Windows 10 isn’t really an option for me right now, either.

  • Rahul April 7, 2017, 3:42 am

    –output-delimiter=’#’