Your Own Linux

Tech Blog

  • Home
  • About
  • Blog
  • Technology
  • Business
  • Contact Us

Your Own Linux: How to Do a Cut Command in Linux

August 21, 2022 by Skyler Leave a Comment

The cut command in Linux is great for snipping sections of a particular file or data and generating a standard result or output. Since it’s a highly powerful command used to manipulate text, you can use the cut command to process your files in various ways.

With that said, if you’d like to learn how to use this powerful command, here’s a cut command in Linux tutorial with examples below.

How the Cut Command in Linux Works

The cut command works by manipulating text in a vertical manner. Most of the time, this Linux command is combined with other commands and filters. Here is what the code generally looks like:

cut [OPTION] [FILE]

You must specify an option, or else the command will return an error. Meanwhile, the File part is the file that you want to manipulate. If you do not specify a file, the command will simply process what’s included in the standard input.

You can primarily use the cut command to cut according to a character, byte, and fields. Furthermore, you can also utilize the cut command if you’re ever complementing a section. For this quick tutorial, go ahead and refer to the capitals.txt file below for the following examples below:

Manila PH 1

Canberra AU 2

WashingtonDC US 3

Use the Cut Command to Cut According to Byte

For this scenario, you will be using the -b option to cut data according to a byte. Here is what the code should look like:

cut -b [LIST] [FILE]

LIST indicates the bytes you want to take per line from the file you’ll use for processing. Now, if you want to simply print the first byte of a file, use the following code below:

cut -b 1 capitals.txt

Output:

M

C

W

Use the Cut Command to Cut According to Character

On the other hand, you need to use the -c option to cut a file according to a character. Here is what the code looks like for this scenario:

cut -c [(a)-(b)/(a),(b)/(b)] [FILE]

This time, the LIST here indicates the characters you want to take per line from the file you’re processing. The a symbol here indicates the beginning position of the character, while b indicates the ending position if there is a “-” symbol in the middle.

Without it, the stated numbers only indicate the position you wish to print from your selected file.

Here’s an example code and its corresponding output:

cut -c 1,3,5 capitals.txt

OUTPUT:

Mnl

Cne

Wsi

As you guessed, this code printed the first, third, and fifth characters per line from the capitals.txt file.

Use the Cut Command to Cut According to Fields

This cut command is very helpful when you need to get specific information from your file. The code for this is as shown:

cut -f [LIST] [FILE]

And if you want to get the second field from capitals.txt, you should use the following code:

cut -f 2 capitals.txt

OUTPUT:

PH

AU

US

You can also use the cut command to perform specific Linux cut using multiple fields. However, the problem when doing this is that the output ends up being concatenated without a delimiter. When doing so, you will need to use the –output delimiter option.

Here is a sample below:

cut -f 1,3 capitals.txt –output-delimiter=’_’

OUTPUT:

Manila_1

Canberra_2

WashingtonDC_3

In the example above, the first and third fields are taken from the file. The output delimiter here is an underscore “_” and is used to concatenate the extracted fields from capitals.txt.

Filed Under: Blog

Leave a Reply Cancel reply

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

About Us

Hello there! I’m Skyler a professional tech guy you can count on. I’m here to share with you my broad knowledge when it comes to tech stuff. I do some configuring and coding that might be helpful to you. Read More…

Connect With Us

  • Facebook
  • Instagram
  • LinkedIn
  • Twitter

Latest Post

  • Advice For Building Team Culture In Modern Times
  • Is Your Business Infrastructure Up To Date And Is That Important?
  • How can a business degree help in succeeding businesses?
  • Digital Business Solutions You Might Not Be Taking Advantage Of
  • Field Service Reporting: Here is a Complete Guide to Field Service Reporting

NewsLetter

Featured

Advice For Building Team Culture In Modern Times

Is Your Business Infrastructure Up To Date And Is That Important?

Free Startup Meeting photo and picture

How can a business degree help in succeeding businesses?

Digital Business Solutions You Might Not Be Taking Advantage Of

Field Service Reporting: Here is a Complete Guide to Field Service Reporting

Free Woman in Red Sweater Leaning on White Table Stock Photo

5 Top MBA Concentrations to Consider

4 trending MBA specializations you can pursue

Ways To Prepare Cuttlefish

How Jobs To Be Done Helps Businesses and How It Works

Top Software Development Trends of 2023

About Us

Hello there! I’m Skyler a professional tech guy you can count on. I’m here to share with you my broad knowledge when it comes to tech stuff. I do some configuring and coding that might be helpful to you. Read More…

Latest Post

  • Advice For Building Team Culture In Modern Times
  • Is Your Business Infrastructure Up To Date And Is That Important?
  • How can a business degree help in succeeding businesses?
  • Digital Business Solutions You Might Not Be Taking Advantage Of
  • Field Service Reporting: Here is a Complete Guide to Field Service Reporting

NewsLetter

Connect With Us

  • Facebook
  • Instagram
  • LinkedIn
  • Twitter

Copyright © 2023 Your Own Linux