Monday, July 26, 2021

10 Examples of CUT command in UNIX and Linux

The cut command in UNIX is a nice utility program that allows you to cut data from a text file. The Linux cut command allows you to cut data by character, by field, or by column. if used correctly along with sed, find, or grep in UNIX, the cut can do lots of reporting stuff. For example, you can extract columns from a comma-separated file or a pipe or colon-delimited file using cut command. For example, if you are only interested in the first two columns you can show them using this command.  In this Linux cut command tutorial we will see different options of cut command, different examples of Linux cut command, and some important points about cut in UNIX.

In order to demonstrate the power of cut command through various examples, we will following the colon-delimited text file as input. This text file contains details of popular smartphones in 2011. The file contains 5 columns i.e. model, company, price, camera, and 4G in the same order.

Here is the content of the file for your reference

Original File
trader@asia:~/perl cat list-of-smartphones-2014.txt

How to use the CUT command in Linux

Now, let's see some of the frequently used examples of cut command in Linux. You will learn about how to extract columns from a text file in Linux, extracting columns by characters, using the cut command with any delimiter e.g. colon or pipe, extracting columns by the tab character, how to display the first column, and how to display multiple columns from the text file using the cut command in Linux.

1.How to Cut Text by Columns in UNIX

Cutting by Column is easy, you first need to decide a delimiter, default is tab then you need to specify column number with -f option,  f stands for a field, for example, cut -f1 file will display first column data. Since our file is colon-delimited, we can use a colon as a delimiter and then extract columns as shown in the following example:

$ cut -d: -f1 list-of-smartphones-2011.txt

You can see that only data from the first column is displayed.

2. Unix Cut by Characters

In the following example of the cut command in UNIX will cut by characters from 1st to 9th character. you can check each line of the output is exactly 9 characters long.

$ cut -c 1-9 list-of-smartphones-2011.txt

3. Unix Cut by a delimiter

The tab character is the default delimiter for cut command. and "-f" option is used to cut by a delimiter. You can override delimiter by providing the "-d" option. Following UNIX or Linux cut command example will show you how to split a line by delimiter in UNIX.

$ cut -d: -f2 list-of-smartphones-2011.txt

In this example delimiter is a colon i.e. ":" character, specified just after "-d" and we have displayed values from the second column. If you want to display values from the 3rd column then just give 3 after -f e.g. -f3, similarly for nth column just give -fn. See these best Linux books if you want to learn more about this option.

10 Examples of CUT command in UNIX and Linux

4. UNIX cut +sed command example with a tab delimiter

To show you an example of the cut command with tab delimiter, we need to first change our delimiter from ":" to tab, for that we can use the sed command, which will replace all colon with \t or tab character. After that, we can use, and then we will apply the cut command of Linux to extract the first column. 

Here is the exact command you can use to do that

$ sed 's/:/\t/g' list-of-smartphones-2011.txt | cut -f 1

5. How to Cut the First Character of a Line in UNIX

Following is an example of the cut in UNIX that will display the first character of each line from the input file.

$ cut -c 1 list-of-smartphones-2011.txt

You can see that only the first character from each line is displayed.

6. How to display the first column from a delimited file

Sometimes, we need the value from the first column from a comma-separated or colon-separated file. that's very easy with UNIX/Linux cut command. define a delimiter and specify a column number. here is an example of UNIX to cut the first column.

$ cut -d: -f1 list-of-smartphones-2011.txt

7. Displaying multiple columns on output using the cut command in Linux

You can show more than one column using Linux cut command. The following example will show both first and second columns from a file.

$ cut -d: -f '1 2' list-of-smartphones-2011.txt

You can see that both column 1 and column 2 are displayed together.

Important points on cut command in UNIX and Linux

Let's revisit some important things about cut command in *NIX operating system. It's one of your helpful mates when awk command is not available.

1) The cut command is used to display selected parts of file content in UNIX.

2) The default delimiter in cut command is "tab", you can change the delimiter with the option "-d" in the cut command.

3) The cut command in Linux allows you to select the part of the content by bytes, by character, and by field or column.

4) The cut command in UNIX or Linux can work with files or you can pipe it with the output of other UNIX/Linux commands.

5) In UNIX, cut -d command is used to cut by a delimiter.

6) The cut -c command option is used to get line segments by characters.

Here is a summary of important cut command examples in Linux:

That's all about cut command in UNIX and Linux. I love this command for its simplicity in power, clever use of the cut command is capable of parsing log files, extracting the details you really need in one column, and they you can use the power of Excel to filter and sort that as per your need. In UNIX you can combine the output of the cut command to uniq and sort to filter duplicates and print them in sorted order.

Other Linux command tutorials you may like
  • How to use the netstat command to find which process is listening on a port? (example)
  • How does the nslookup command work in UNIX? (answer)
  • 10 examples of lsof command in Linux? (examples)
  • How to send HTTP requests to call a web service from the command line? (example)
  • 10 books to learn essential commands of Linux or UNIX? (list)
  • How Linux works? What Every SuperUser should know? (book)
  • 10 Examples of cURL command in Linux? (examples)
  • How to convert an IP address to the hostname in Linux? (command)
  • How to exit from the telnet command in UNIX? (example)
  • A Practical Guide to Linux Commands, Editors, and Shell Programming (guide)

Thanks for reading this article, if you like this article then please share it with your friends and colleagues. If you have any suggestions or feedback then please drop a comment. If you want to learn more about powerful Linux command utilities then read this book. 

No comments :

Post a Comment