Skip to main content

Learning BASH: Text Processing - Cut Command


Text processing tools in Bash is a huge topic . So we will take it one command at a time.


CUT COMMAND


You might think , CUT means to remove a file from location A to location B. But as the link here says, Cut command in unix (or linux) is used to select sections of text from each line of files. You can use the cut command to select fields or columns from a line by specifying a delimiter or you can select a portion of text by specifying the range or characters. Basically the cut command slices a line and extracts the text.

The definition of CUT command in linux itself says:



Print selected parts of lines from each FILE to standard output.

I created a text file (I am on windows running Cygwin...so......) . Added a few lines.

This is the first line
This is the second
And this is not the last line
Finally we end
Good Bye

The linux help says:


 N         N'th byte, character or field, counted from 1
  N-       from N'th byte, character or field, to end of line
  N-M   from N'th to M'th (included) byte, character or field
  -M      from first to M'th (included) byte, character or field


Problem : Give me the first (1st) letters of every line.

Solution:


$ cut -c1 foo.txt
T
T
A
F
G

Analysis: -c1 means , column one (1). Or position 1. That's the N'th byte.
Note: Column numbering starts from 1. NOT zero (0).

Problem: Show me the first three characters of each line.
Soln:


$ cut -c1-3 foo.txt
Thi
Thi
And
Fin
Goo

$ cut -c-3 foo.txt
Thi
Thi
And
Fin
Goo

Two ways to do it , May be more, but these are the easiest ways I suppose.
You can specify a RANGE . We have used here -M and N-M in each example.

Problem: Get the 3rd character of each line in a file. The file is given as an input from user. 

Solution:

cut -c3 $(expr read file)

Note that there other ways to do this.

cut -c3 

Note:  cut reads from standard input if the argument is "-" or absent.


Using Delimiters



The -d option in cut command can be used to specify the delimiter and -f option is used to specify the field position.

$ cut -d$' ' -f-3 foo.txt
This is the
This is the
And this is
Finally we end
Good Bye

Note : The -d needs a delimiter to be specified. The -f tells us the position. Here I have used first to third position .

In the above example, my delimiter is a single length space. I need to see till the 3rd occurance of space.

Another example:

$ cat foo.txt
Hi:I:Am:Groot

$ cut -d$':' -f1-3 foo.txt
Hi:I:Am

Problem: Given a sentence, identify and display its fourth word. Assume that the space (' ') is the only delimiter between words.

Solution:


cut -d$' ' -f4

Same for semi colon example:


$ cat foo.txt
Hi:I:Am:Groot

$ cut -d$':' -f4 foo.txt
Groot


Problem: Given a tab delimited file with several columns (tsv format) print the fields from second fields to last field.

Solution:

cut -f2- 

Note: The default delimiter is tab. So you DON'T need to specify a delimiter at all if the problem asks for a tab delimiter.

__________________________________________________________________________________________________

Reference: I took the problems from my favorite code competition site. Hacker Rank. Visit this link to practice more problems . Solve the first 9 problems which are based on CUT command for bash. Best of luck.

Popular posts from this blog

Why should you visit Kashmir sooner?

"If there is paradise on Earth, this is it"
And paradise it was . Early winter is the perfect time to plan for a visit to Kashmir. We will suggest to keep your itinerary  short . And enjoy the best trip of your life. 

   My Visit Month : Mid November 2015

As we were flying close to Srinagar, about 20 minutes before landing , all of a sudden , every passenger in the aircraft was on his toes craning theirs necks over the tiny window to catch a glimpse of the above. A fleet of Snow Giants.


   Visit Meena Bazaar in Dal Lake
  You will find a floating village with houses , shops, and hotels on all kinds of boats.


    A night on a House Boat




Take a Shikara ride.Watch the super clear reflection of the mountain. Spot fishes and water birds.



Take a 2 day stay in Pahalgam. Visit Mini Switzerland first day on a horse ride.


Visit Sonemarg. Enjoy the snow.



On your way to Pahalgam, visit apple orchids . Taste all kind of apples. The golden ones are supposed to be the best.



The dark ominous mou…

Kerala : How to plan for a lifetime's experience

I have been planning this for months and it finally came along in the best time. A full stretch Kerala trip with Kanyakumari as an added bonus. I will not tell you what to do, but HOW to do it. How to plan your dream Kerala Trip. 

Caution: Mine was too comprehensive. Needs high energy and will be quite tiring. 

I chose the monsoon theme..I wanted to be in the middle of the action. This is the only place in India which offers monsoon as a tourism opportunity. I was lucky. I expected super heavy rains. Instead. I ended up right in between the rainy season and summer which was Perfect.

Advantages in travelling in mid AugustRainfall almost over.Waterfalls at their fullest.Greenery all over the state.Backwaters, beach sides and rivers all very clean.Abundant coconuts to enjoy.Hotel rates brought down.Less crowd everywhere.Festive Preparation going on.Shopping becomes pocket friendly.Our route map:






Day 1 Day 2 Day 3 Day 4 Day 5 Day 6

CNTLM in Office

If you are facing a proxy problem , CNTLM is a general option everybody considers. But the working of CNTLM is not always guaranteed. I myself get confused every time I need to use it.

What problem are we actually talking about?

While working in offices and restricted environments where firewalls and other invisible barriers stop your requests from going out, you sometimes need a way to temporarily jump over that barrier to get your job done . Example: You might face them while trying to install using pip in command line, trying to clone a personal git repository. 

What is CNTLM exactly?

I think this is best answered by the creator himself. Refer this. You can download it from here. For configuration, you an refer this. There are many uses of CNTLM but I am trying to solve my own problem here. So I will stick to breaking office barriers for now ;) .

Why do I need to use it :
1) Python package installations using PIP
2) Git operations : Cloning and pushing to server.

Here are a set of steps I …