Friday, 21 April 2017

10 Examples of CUT command in UNIX and Linux ~ GNIITHELP

The cut command in UNIX is a nice utility program which 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 sedfindor 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 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 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
Model:Company:Price:Camera:4G
IPhone4:Apple:1000$:Yes:Yes
Galaxy:Samsung:900$:Yes:Yes
Optimus:LG:800$:Yes:Yes
Sensation:HTC:400$:Yes:Yes
IPhone4S:Apple:1100:Yes:Yes
N9:Nokia:400:Yes:Yes


HOW TO USE 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 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 cut command in Linux.


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
Model
IPhone4
Galaxy
Optimus
Sensation
IPhone4S
N9
You can see that only data from first column is displayed. 


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
Model:Com
IPhone4:A
Galaxy:Sa
Optimus:L
Sensation
IPhone4S:
N9:Nokia:


Unix Cut by delimiter

The tab character is 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
Company
Apple
Samsung
LG
HTC
Apple
Nokia

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 3rd column then just give 3 after -f e.g. -f3, similarly for nth column just give -fn. See Linux Command Line: A complete Introduction if you want to learn more about this option.

10 Examples of CUT command in UNIX and Linux



UNIX cut +sed command example with 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
Model
IPhone4
Galaxy
Optimus
Sensation
IPhone4S
N9



How to Cut the First Character of a Line in UNIX

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

$ cut -c 1 list-of-smartphones-2011.txt
M
I
G
O
S
I
N

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

How to display the first column from a delimited file

Sometimes, we need 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 cut the first column.

$ cut -d: -f1 list-of-smartphones-2011.txt
Model
IPhone4
Galaxy
Optimus
Sensation
IPhone4S
N9



Displaying multiple columns on output using cut command in Linux

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

$ cut -d: -f '1 2' list-of-smartphones-2011.txt
Model:Company
IPhone4:Apple
Galaxy:Samsung
Optimus:LG
Sensation:HTC
IPhone4S:Apple
N9:Nokia

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 part of file content in UNIX.

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

3) The cut command in Linux allows you to select part of 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 command.

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 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 duplicate 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 nslookup command work in UNIX? (answer)
  • 10 examples of lsof command in Linux? (examples)
  • How to sent HTTP request to call a web service from the command line in Linux? (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 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)

No comments:

Post a Comment