Cut Command in UNIX is used to cut out the desired output from the file and display the result. In UNIX language, cut is used to cut out certain section from each file. You can cut out either field, character or a delimeter or bytes.
cut OPTION… [FILE]…
Anything that is written inside the square brackets is optional. So, you can or cannot provide the option and may or may not provide the file name. In case, you don’t provide the file name, you will be asked to give the input and the operation will be then done on the input provided.
Let us understand some of the most common used option with cut.
1. -c — This option cuts out the specified character or the range of character and gives you the output.
cut -c 2 (enter)
This command will take you to the input terminal, wherein you will be asked to give the input. Based on the input that you have provided, the command gives as output only the second character of the input. For example –
cut -c 2 (enter)
avantika joshi (input)
v (output)
cut -c 2-10 (enter)
avantika joshi is awesome. (input)
vantika jo (output)
This command outputs the characters from 2 to 10.
___________________________________________________________
The fact is that when performming operations in real life, you require a file as most of the operations are done on file. So, cut allows you to specify the file name.
cut -c2-13 filename
This command prints the characters 2 to 13 from each line of the file.
Note – There is no fuss about the space between c and the number. So, you can write, -c 2 or -c2, does not matter.
2. -d and -f options — The reason we are looking these two options together, is that they don’t work independently. So, the -d option specifies the point using which the data in the file or text will get divided and the fields are the ones in which the data gets devided. Confused? Let us see.
We have a file practice 1. The contents are as –
1. avantika
2.anamikc
3. avinash
4.abhijeet
5.aniket
6.akansh
7.abhilasha
8.animesh
9.arun
10.arpita
Now, I want the data that is after the ‘.’. What should I do?
I will say please cut the data from . (specify the delimeter) and I want the data that is after . (so you are specifying the field)
cut -d “.” -f 2 practice1
This command will first divide the data based on . and each division is known as field. Here, anything before . is field 1 and the one after . is field2. Since I want the data after the . I will specify the field to be 2.
THE ISSUE
The issue with specifying only the delimeter of the field is that, in the first case, you are only providng the delimeter and nothing else. So, it does not knows what to fetch and in the second case you have specified that you need the data from the second field but you have not specified on what basis you are separatig the field.
cut -d “.” filename
The shell says what?? What am I supposed to do?
cut -f 1 filename
Which field are you talking about?
NOTE- You can even specify more than one fields.
cut -d “.” -f 1,2 filename
This command will divide the data based on the “.” and prints field 1 and 2.
The order of writing the option does not matters.
space is the default delimeter.
Happy Learning 🙂

Leave a comment