Skip to main content

Learning BASH: Text Processing - HEAD & TAIL

BASH continues to surprise me with it's amazing collection of simple, yet extremely useful commands. They can give you a huge boost in speed and control while working. No doubt bash along with editors like VIM are the developer's favorite combinations. Gradually , you will feel the invention of mouse as a waste since you can pretty much control everything with just your keyboard.

Today we continue with more commands that are related to Text Processing.

HEAD & TAIL command


These commands are used to get contents of a file starting from the top and bottom. Unlike the CAT command that displays the whole content of a file, these command gives you control over how much you want to see.

syntax: HEAD filename | TAIL filename

Note: By default, HEAD | TAIL shows 1st/last 10 lines of a file.

Lets say I have a text file like this.

$ cat numbers.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

Let's run head command without any arguments other than the filename.

$ head numbers.txt
1
2
3
4
5
6
7
8
9
10

Problem: Display first 5 lines of the file provided.

$ head numbers.txt -n 5
1
2
3
4
5

$ tail numbers.txt -n 5
16
17
18
19
20


The -n argument lets us specify the number of lines I want to see/grab , starting from the first line.

Problem: Display the first 20 characters of a file/some text line.


$ echo "This is a test line with many characters" | head -c 12
This is a te

$ echo "This is a test line with many characters" | tail -c 12
 characters


Here, the -c argument is for character count. As the bash help says,

  -c, --bytes=[-]NUM       print the first NUM bytes of each file with the leading '-', print all but the
                                         last NUM bytes of each file


Note: HEAD/TAIL commands do not accept a range. So you can't display lines starting from n1 to n2. say 5-10th line. You can do that, but using a mixture of tail and head command. We will see that later.

TIP:

One of the most popular use of the tail command is to monitor changes to a file. Example a log file that logs each activity in a software or a website.

$ tail -f log.txt

The -f is for file input.

Problem: Display the lines from the given file between line 10 and 15.

Solution: So head gives us lines from first line i.e 1 to n . And tail gives us n lines , counting from the bottom . Our answer is expected as 11,12,13,14 as I said , I want numbers IN BETWEEN 10 and 15.

In plain words, this might be our plan.


  • First we get us the all numbers till (15 - 1). i.e 1 to 14. 
  • Then we remove 1 to 10 from this and the rest is our answer. In pictures, I can put it like this.






$ head numbers.txt -n 15 | tail -n 5
11
12
13
14
15

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 …