Using the Linux cut command to grab portions of lines from files (2024)

The cut command offers many ways to extract portions of each line from a text file. It's similar to awk in some ways, but it has its own advantages and quirks.

Using the Linux cut command to grab portions of lines from files (1)

Credit: Thinkstock

One surprisingly easy command for grabbing a portion of every line in a text file on a Linux system is cut. It works something like awk in that it allows you to select only what you want to see from files, enabling you to pull fields (regardless of the delimiter used), characters or bytes. To check on cut, you can ask about its version like this:

$ cut --versioncut (GNU coreutils) 8.32Copyright (C) 2020 Free Software Foundation, Inc.License GPLv3+: GNU GPL version 3 or later .This is free software: you are free to change and redistribute it.There is NO WARRANTY, to the extent permitted by law.Written by David M. Ihnat, David MacKenzie, and Jim Meyering.

Selecting by field

To illustrate how the cut command works, we’ll first run commands using a sample “cities” file that contains details of the largest cities in the US in a tab-separated format. The lines in this file look something like what is shown below:

$ head -5 citiesRank Name State 2021 Pop. 2010 Census Change Density (mi²) Area (mi²)1 New York City New York 8,230,290 8,190,210 4,083 3002 Los Angeles California 3,983,540 3,795,510 1,266 4693 Chicago Illinois 2,679,080 2,697,480 1,756 2274 Houston Texas 2,323,660 2,100,280 541 640

To select a particular field from this file, you might use a command like this that shows the 4th field:

$ cut -f 4 cities | head -112021 Pop.8,230,2903,983,5402,679,0802,323,6601,733,6301,585,0101,581,7301,427,7201,347,1201,011,790

To add the city names to your selection, you would select the 2nd and 4th fields. Since the tab character is the default delimiter for the cut command, it easily extracts these fields.

$ cut -f2,4 cities | head -11Name 2021 Pop.New York City 8,230,290Los Angeles 3,983,540Chicago 2,679,080Houston 2,323,660Phoenix 1,733,630Philadelphia 1,585,010San Antonio 1,581,730San Diego 1,427,720Dallas 1,347,120Austin 1,011,790

The string -f1-4 would display the first four fields in the file:

$ cut -f1-4 cities | head -5Rank Name State 2021 Pop.1 New York City New York 8,230,2902 Los Angeles California 3,983,5403 Chicago Illinois 2,679,0804 Houston Texas 2,323,660

To specify a different delimiter, you could add the -d option and use a command like this one, which pulls usernames from the /etc/passwd file:

$ cut -d: -f 1 /etc/passwd | head -10rootbindaemonadmlpsyncshutdownhaltmailoperator

To select to see both login names and assigned shells, try this:

$ cut -d: -f 1,7 /etc/passwd | head -10root:/bin/bashbin:/sbin/nologindaemon:/sbin/nologinadm:/sbin/nologinlp:/sbin/nologinsync:/bin/syncshutdown:/sbin/shutdownhalt:/sbin/haltmail:/sbin/nologinoperator:/sbin/nologin

The command above selects the 1st and 7th fields.

To count how many accounts use each of the shells, use a command like this:

$ cut -d: -f 7 /etc/passwd | sort | uniq -c 17 /bin/bash 1 /bin/sync 1 /sbin/halt 44 /sbin/nologin 1 /sbin/shutdown 1 /usr/sbin/nologin

Notice how many accounts cannot log in because they’re assigned the /sbin/nologin shell. These are, of course, accounts associated with system services.

Selecting by words

You can also use the cut command to select single and multiple words or strings from a file. Just remember that you need to specify the delimiter if the words or strings are not separated by tabs. The two command below show different amounts of each line. The first (delimited by blanks) displays the first field. The second (delimited by commas) displays all of the text up to the first comma.

$ cut -d' ' -f1 addresses7610680310893833$ cut -d, -f1 addresses7610 West Park Drive6803 Gravel Branch Rd1089 Plymouth Drive3833 Abingdon Circle

If we asked for the first field without specifying a delimiter, we would see entire lines in any file that is not delimited by tabs.

$ cut -f1 addresses7610 West Park Drive, Hyattsville, MD 207836803 Gravel Branch Rd, Hurlock, MD 216431089 Plymouth Drive, Rahway, NJ 070653833 Abingdon Circle, Norfolk, VA 23513

Selecting by characters

To select lines using character ranges, you can do something like this:

$ cut -c1-3 weekdaysSunMonTueWedThuFriSat

This displays the first three letters of each line of a file that lists the days of the week.

Selecting by bytes

You can ask cut to select by bytes. Unless your data file includes characters that occupy more than a single byte, you would not see any differences. In this example, we might see a difference simply because the £ sign occupies two bytes.

$ cut -b1-23 costThat biscuit cost me 2▒$ cut -c1-23 costThat biscuit cost me 2£

In the first command above, the response shows show a block of dots because it’s looking only at the first byte of the £ sign. In the second, we select by character, so it uses both bytes. We could also have just done this and added one more byte:

$ cut -b1-24 costThat biscuit cost me 2£

Reversing your request

You can also select an option to reverse the output from your cut request. This doesn’t mean displaying it in reverse order, but means “doing the opposite”. Selecting the first four characters from a file is one thing. Select everything but those characters is its “complement”. Here’s an example:

$ cut -b1-4 addresses7610680310893833$ cut --complement -b1-4 addresses West Park Drive, Hyattsville, MD 20783 Gravel Branch Rd, Hurlock, MD 21643 Plymouth Drive, Rahway, NJ 07065 Abingdon Circle, Norfolk, VA 23513

Wrap-up

The cut command offers a lot of flexibility for selecting portions of each line in a file. Consult the man page for more information on its many options.

Using the Linux cut command to grab portions of lines from files (2024)
Top Articles
Latest Posts
Article information

Author: Horacio Brakus JD

Last Updated:

Views: 5675

Rating: 4 / 5 (51 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Horacio Brakus JD

Birthday: 1999-08-21

Address: Apt. 524 43384 Minnie Prairie, South Edda, MA 62804

Phone: +5931039998219

Job: Sales Strategist

Hobby: Sculling, Kitesurfing, Orienteering, Painting, Computer programming, Creative writing, Scuba diving

Introduction: My name is Horacio Brakus JD, I am a lively, splendid, jolly, vivacious, vast, cheerful, agreeable person who loves writing and wants to share my knowledge and understanding with you.