Posts

Learning BASH: Text Processing - HEAD & TAIL

Image
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 number…

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-Mfrom N'th to M'th (included) byte, charac…

Learn BASH with me in 5 mins

Image
I just started learning Linux bash from today. From my first impression of the language, I infer that it is a language with all the basic capabilities as of an infant high level language. May be I am right or wrong.Time will tell . We will keep going and keep discovering gradually. Let's start with the usual protocol of learning a language.

The HELLO WORLD program.
How to print things in shell. This is the first thing everyone wants to know while learning any language.

Anything that is not a variable is printable . And we print/echo it using the famous ECHO keyword

$ echo hello world hello world
Printing a number.
$ echo 1 1 Printing a string with double quotes$ echo"my name is arindam" my name is arindam Printing a string with single quotes$ echo'my name is Arindam' my name is Arindam Printing a number with quotes$ echo'1' 1
Creating  Variables and recalling them.
So how can we store things. How to recall that stored value. How to change that value.

X=9…

An early morning trek to Ghangad Fort

Image
Ghangad is a short but exciting trek offering the thrill of trekking over difficult patches in portions and some heart trembling moments when you cling on to a thin wire of hope for support.
It great for training beginners. And the view is just splendid.

Duration : 30 mins - 40 mins

Why to choose this trek:

1) Great for only morning treks like Sinhagarh. It offers a mind view of two sides of the mountain .
2) Easy to reach. The route to Ghangad offers amazing landscapes from Tamini Ghat. So it's a bonus.
3) Don't need to plan ahead. Don't need to carry much stuff if you planning to return same day.
4) In rainy season, the waterfalls in the valley area are amazing. We plan to do it this rainy season.
5) Barely any crowd. We encountered no one. Absolute silence and just the wind blowing , animal sounds from the forest . It was just a trekker's dream come true.

Our Plan :

We started at 3:30 .
Climbed and reach back home by 12 AM .
It's an hour long journey . We went via…

Python List sorting with key argument explored

Everybody uses lists as an array to store values. List provide a lot of in-build features.SortingMembershipIndexingIterationreversingAdding/RemovingPoppingCount
Lots of reasons to use lists. I think, one of the most used features is sorting. Internally , python uses Merge sort technique to sort the array items. But the sort method can be used in many other ways to have more control.

The python help says:


>>> help(list.sort) Help on method_descriptor: sort(...) L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*; cmp(x, y) ->-1, 0, 1

Things to note: The sort method doesn't return anything. It saves the changes in the list permanently. Thats why it is faster than the method sorted

SIMPLE SORT


>>> l = ['c', 'b', 'd', 'a'] >>> l.sort() >>> l ['a', 'b', 'c', 'd']
If elements are strings, they gets sorted by alphabetical order.

SORT IN REVERSE ORDER


>>> l =…

One day trip to Malshej Ghat

Image
Maharashtra offers a host of choices when it comes to hills ,valleys and forts.  Activities like trekking or rappelling are most popular here. 

Malshej is one of those places that will offer you its own specialty. 

Rocky mountains, deep gorges, Beautiful tunnels , roads snaking around. Uncountable waterfalls.Dense forest and its habitat. 

It's a power package for a day. One day escape from the daily hassles of life. Western Ghats for you ladies and gentlemen.



How to reach:
A road trip will be the best option. You don't want to miss the chance of stopping anywhere you want and click pictures or just stare in awe. 



How to plan:Start at early morning 6 AM.Reach Malshej in 3 hrs.Straight away park at the MTDC.Have break fast at the restaurant inside.Stroll behind the hotel . There is a beautiful pathway made for tourists.





Find a good waterfall. Enjoy bathing in it. It's just awesome .


 Return home before night.
Best time to go:Rainy season. June - August
Precaution:Before you plan, ch…

Writing context managers using Python

We use. We forget. This is for myself so I remember the magic called Context Manager. You already must have used the "with statement" for file handling, threads, socket and other operations. I am going to talk about creating you OWN context managers.

The magic operator to be used is : @contextmanager. It can be imported from the module "contextlib"

To create a context manager, use the decorator contextlib.contextmanager


@contextlib.contextmanagerdefgetListCounts(x): try: fromcollectionsimport Counter yield Counter(x) finally: print'No of elements in the list : %d' % len(x)

Let's now use the above context manager 

Example 1:
>>> with getListCount([10, 10, 12]) as c: print "Element occurring the most is %d" % c.most_common()[0][0] Element occurring the most is 10 No of elements in the list : 3
Example 2:
>>> with getListCount([10, 10, 12]) as c: print "Element occurring the least is %d"…