Monday, August 15, 2011

Unix SED & awk- File processing

http://sed.sourceforge.net/sedfaq3.html

I'd like to skip first two or three fields at the the beginning of a line and print the rest of line. Consider the following input:
This is a test
Giving back more than we take
I want my input file with the following output:
a test
more than we take
How do I printing lines from the nth field using awk under UNIX or Linux operating systems?

You can use the awk command as follows:
 
echo 'This is a test' | awk '{print substr($0, index($0,$3))}'
 
OR
 
awk '{print substr($0, index($0,$3))}' <<< 'This is a test'
 
You can also use the cut command:
 
echo 'This is a test' | cut -d ' ' -f3-
 
OR
 
cut -d ' ' -f3- <<<'This is a test'
 
Finally, process the file using bash while loop:
#!/bin/bash
_input="/path/to/file.name.txt"
while IFS= read -r line
do
    cut -d ' ' -f3- <<<"$line"
    ### same stuff with awk ###
    ### awk '{print substr($0, index($0,$3))}' <<< "$line" ###
done < "${_input}"
 
Please note that you can also use Perl, Python or other shell text processing commands to do the same thing.

Unix Commands ZIP

The syntax is as follows:
zip file.zip file1.txt file2.doc photo.jpg 

To zip resume.doc and all *.c file, enter:
zip bacup.zip resume.doc *.c
To zip data directory, enter:
zip -r backup.zip data

Saturday, July 30, 2011

Haskell_Notes_4

Using the GHC Compiler:

1. Create a file - baby.hs

2.  Open the WinGHCi

ghc> :cd D:\2011\Learning_Workspace\2011_Learnings\Haskell_Learnings

ghci>  :load "baby.hs"  ------loads the module.


  1. doubleSmallNumber x = if x > 100  
  2.                         then x  
  3.                         else x*2   


Haskell - if should always have else. both parts if & else  should return expression.

Functions - Rules


1. Cannot begin with Uppercase letter.
2. Name can be conanO'Brien = "It's a-me, Conan O'Brien!"   
3. When a function doesn't take any parameters, we usually say it's a definition (or a name).

Haskell_Notes_3

Note: 
you can do 5 + 4.0 because 5 is sneaky and can act like an integer or a floating-point number. 4.0 can't act like an integer, so 5 is the one that has to adapt


Infix functions
PostFix Functions


* is a function that takes two numbers and multiplies them.  5*4  is an infix function.   
we can think 
function * (param1, param2)
{
    return param1 * param2;
}
Most functions that aren't used with numbers are prefix functions


Pre-Defined functions available in Haskell Libraries:


succ
pred
min
max


ghci> succ 8
9
ghci> max 10,11
11


div - this function does only integer division.






Haskell_Notes_2

2. Starting Out
==========

After the installation in Windows:

C:\Program Files\Haskell Platform\2011.2.0.1\winghci\winghci.exe


double click and run.


GHCi, version 7.0.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
Prelude> 


Few Points:

1. GHCi -> Glasgow Haskell Compiler interactive mode
2. Latest version as of 30th July 2011 - 7.0.3
3. Couple of packages are loaded - ghc-prim, integer-gmp, base, ffi-1.0
4. The default prompt we get is: Prelude
-------------------------------

Prelude> :set prompt ghci
ghci :set prompt "ghci> "
"ghci> " :set prompt ghci> 
ghci>  
---------------------------------
ghci>  180

180
ghci>  1*0
0
ghci>  1/0
Infinity
ghci>  10/3
3.3333333333333335
ghci>  10/4
2.5
ghci>  1==0
False
ghci>  10000000.0000000000001==10000000.0000000000001
True
ghci>  10000000.0000000000001==10000000.0000000000000 
True
ghci>  10000000.0000000000001==10000000.0000000000000000
True
ghci>  10000000.0000000000001==10000000.0000000000000001
True
ghci>  10000000.0000000000001==10000000.0000000100000001
False
ghci>  10000000.0000000000001==10000000.0000000000001
True
ghci>  10000000.0000000000001==10000000.0000000000009
True



ghci>  10+100/20
15.0
ghci>  (10+100)/20
5.5
ghci>  5 * -3


<interactive>:1:1:
    Precedence parsing error
        cannot mix `*' [infixl 7] and prefix `-' [infixl 6] in the same infix expression


ghci>  5 * (-3)
-15
ghci>

Boolean algebra

ghci>  true


<interactive>:1:1: Not in scope: `true'


ghci>  True
True


ghci>  True & True

<interactive>:1:6: Not in scope: `&'
ghci>  True && True
True
ghci>  True &&& True

<interactive>:1:6: Not in scope: `&&&'
ghci>  True && && True

<interactive>:1:9: parse error on input `&&'
ghci>  True &&           True
True
ghci>  True &&       False    True

<interactive>:1:15:
    The function `False' is applied to one argument,
    but its type `Bool' has none
    In the second argument of `(&&)', namely `False True'
    In the expression: True && False True
    In an equation for `it': it = True && False True

True 
False
&&
||
not

Case Sensitive


Comment? 


-------------------------------

Character Type:    'a' 
string type: "hello"

Equal to and Not Equal to:
ghci> 1==1
True
ghci> 1/=1
False
ghci> 1/=2
True

Exceptions:
ghci>  5 == True

<interactive>:1:1:
    No instance for (Num Bool)
      arising from the literal `5'
    Possible fix: add an instance declaration for (Num Bool)
    In the first argument of `(==)', namely `5'
    In the expression: 5 == True
    In an equation for `it': it = 5 == True
ghci>  'a' + 'b'

<interactive>:1:5:
    No instance for (Num Char)
      arising from a use of `+'
    Possible fix: add an instance declaration for (Num Char)
    In the expression: 'a' + 'b'
    In an equation for `it': it = 'a' + 'b'
ghci>  "he" + "he"

<interactive>:1:6:
    No instance for (Num [Char])
      arising from a use of `+'
    Possible fix: add an instance declaration for (Num [Char])
    In the expression: "he" + "he"
    In an equation for `it': it = "he" + "he"




Haskell_Notes_1

1. Learning Haskell is like learning programming from Zero.  Wow, I can learn programming, without any pre-conceptions. I am in LKG, which I never studied. In fact in my college time also did not learn a programming language, the way it should have been learnt. It's great to have another chance now.

2. A programming language should affect your thinking. It should change the way you think. After leaning a programming language, I should be something new. http://norvig.com/21-days.html

Notes from LearnYouAHaskell
------------------------------------
1. Imperative Programming
2. Functional Programming
3. Haskell is great and if you're interested in programming you should really learn it even if it seems weird at first. Learning Haskell is much like learning to program for the first time — it's fun! It forces you to think differently
4. Purely Functional Programming Language
         --A function has no side effect: It does not change any of the existing thing/object/item. It does calculation using existing objects and returns the result. (like Utility static functions which gets primitive types as parameters.)
         --Referential Transparency - A function, can be called "N" number of times, with same parameters, output is always same. (Predictability/Consistency?)
          --A type(variable) in the system can be set only once. It cannot be changed. --Immutable.
          -- LAZY - Does everything only at the neck of the moment. Never does anything proactively.  How come we are using this language :) we want everything & everyone to be proactive, hyperactive whatever new terms come up :)   doubleMe(doubleMe(doubleMe(listIntegers)));  it does not do anything when this statement is executed.   When you want to see the actual result, it executes.
 3. Statically Typed - Good Type Inference system - compiler should be able to catch all the type related issues.
 4.  Elegant and Concise - A function should be of 3 to 5 lines - very short and crisp. -- this is applicable for any language.
---------------------------
Environment:


1. Programming Language: Haskell
2. IDE - Notepad++ or any text editor you like
3. Compiler - GHC - Glasgow Haskell Compiler - this comes with an interpreter: ghci
4. File extension: myFunctions.hs
5. to load/reload the functions in the interpreter: :l myFunction
6. to reload after onetime loading - :r       ---- Reloads the current script.





Haskell_Questions?

Day - 1:

1. Do Haskell Programs require more memory than the Imperative programs, due to creation of objects every time?

As all objects are immutable in haskell, it may require more objects at run time, which means more memory?

2. Haskell runtime dll/file/so name in windows/unix/Mac? (like: msvcrt or msvb.)

3. how the runtime is able to avoid calling the function when it comes across that statement? But executes it only when its really required? How CLR in .NET is able to do it? How Haskell does it?

4. 

Tuesday, July 12, 2011

http://financialhighway.com/8-tips-to-prepare-for-next-recession/

Here are eight tips to prepare you for the next recession:
1. Understand Recession
Recession is a normal part of the economic cycle, every few years we go through it and come back out. For some it may feel like it is the end of the world and everything is falling apart they forget that it’s a cycle. Understanding recession will give you some peace of mind and will take away the unknown factor.
2. Reduce Your Debt
Debt was one of the primary causes of the recession. If you have any consumer loans try to get rid of it to ensure you are still solvent. Do not get fooled by the low interest rates, it’s a trap. If you have credit cards you can call your institution to reduce your rates or transfer them to a new card with low rates, than pay it off as fast as you can.
3. Build Up Your Emergency Fund
It is astonishing to find out how many households do not have an emergency fund. We talked about emergency fund previously and how important it is. If you do not have one your first priority should be to build now. If you already have an emergency fund ensure that it is enough and add some more to it, this will give you at least some psychological support.
4. Cut Costs
Reduce your spending. Often you can reduce your bills if you call your service providers and ask for it. It may not always work but it is worth a try, in 2001 I was able to cut my cable bill by a huge amount and get extra’s permanently.
5. Balance Your Portfolio
Balance your portfolio on regular bases to stay within your asset allocation. The recession will bruise your portfolio, but do not panic. Do not be afraid of stocks if you have a long term view, recessions are the best time to purchase good long term investments.
6. Do More At Work
If you are worried about your employment make sure you work harder than you have in the past, make yourself useful to your company. Learn new skills and attend extra courses, the more you know the more valuable you will be to your employer.
7. Diversify Your Income
Diversifying your income can act as a buffer during a recession. If you haven’t already started this process, now maybe a good time to think about it. It’s basically having different income streams so in case you lose your job there is some income.
8. Continue With Your Investment Plan
If you are saving for long term goals continue to do so. Recession is a bad time to change your investment strategy, if you are a buy-and-hold investor continue to do so.


household spending was down -0.4% the first spending reduction since 1995. It is no secret that we are in the midst of a very severe global recession, one that we have not seen in decades.
Although recession is not the greatest thing, there are some good sides to a recession, but before we get into that let’s take a quick look at what causes recessions.
A recession is caused when consumers do not spend money hence become savers causing companies to cut costs including labour costs, which increases unemployment and reduces disposable income forcing more consumer to cut spending…. and the cycle goes on. Government and Central banks have some tools available to them to stimulate the economy, like cutting interest rates and introducing stimulus packages. However it takes time for these measures to work and in the mean time the economy deteriorates.
This is a very simple explanation of a recession it is a little more complicated in reality, but it makes the point.
Recession is a Good thing
Recession is not all bad, there are some good sides to it. Such as Price drops, Interest rate drops, Stock market drops.
Price Drops
During an economic boom period goods and services prices start to go up very fast causing high levels of inflation, a recession reduces the rate of inflation. Sometimes prices come down for a short period. A recession is like a big sale, you will see all types of deals and incentives from companies, you can get things for much cheaper prices then a year ago.
Interest Rate Drop
Interest rates also come down reducing borrowing costs for consumers and business this often increases disposable income. It can also be a good time to make purchases on credit or loan, like a new car or home.
Stock Market Decline
Another upside of a recession is stock market decline, some of it is justified as companies’ earnings decline so do their stock prices, but often investors just get scared and go on a selling spree bringing the companies to a great value.
[ad#vertical-for-all-possts]
How can you take advantage of a recession?
If you have a secure job and some cash on hand a recession is a great time to do either of the following:
1. Make those large purchases or renovations you wanted to do
2. Start investing in the stock market
As I mentioned earlier, recessions cause price drops and bring great deals, if you have the cash available it is a good time to look for great bargains.
With stock markets at historic lows it would also make good financial sense to start investing in very stable companies, I wouldn’t recommend lump sums investments I would rather dollar cost average, small amounts on regular bases. This strategy has worked well in the past as Canadian Capitalist points out.
Either way you would be part of the solution to the recession.
Not Enough Cash
On the other hand if you do not have a lot of cash available, you can start cutting down on some costs and save some money to take advantage of the recession. If you are in this camp you would be part of the problem.
I must admit I am part of the problem, although the concern is not job security. For me it is getting ready for the economic recovery and wedding this summer.
My cuts
I thought I share some of the expenses that we have cut back on:
Cell phone: Although my phone plan is still fairly high due to data plan, I was able to cut down about $20/mth.
Gas: We reduced driving around and we try to get as many things done as possible ones we get out, able to cut $50/mth
Cable: Got rid of some channels and return to basic cable savings of $15/mth

Internet:
Talking to customer service I was able to lower the cost $10/mth
Recreation: We decided to go out less, go to movies on Tuesdays ($4.99) and we have Scotia Scene accounts where we accumulate points for free movies savings of $50/mth
Clothing: Reducing spending on clothing saving us about $40/mth
Our total monthly savings about $195/mth
What do we do with the money?
Well most of it is sitting in Scotia Money Master Savings account and waiting to be deployed.
What camp are you in solution or problem?

Monday, July 11, 2011

Unix_Commands

For finding a file in current directory and all subdirectory

find . -name "file_name.File_to_be_found" -print



File display with line numbers:
cat -n file_name_with_full_path

disk free space:
df -h
du -h

Sunday, June 19, 2011

List Vs Linked List

http://stackoverflow.com/questions/169973/when-should-i-use-a-list-vs-a-linkedlist


Linked lists provide very fast insertion or deletion of a list member. Each member in a linked list contains a pointer to the next member in the list so to insert a member at position i:
  • update the pointer in member i-1 to point to the new member
  • set the pointer in the new member to point to member i
The disadvantage to a linked list is that random access is not possible. Accessing a member requires traversing the list until the desired member is found.

Wednesday, June 15, 2011

.NET CORE


Difference Between HashTable and Dictionary:
Simply, Dictionary<TKey,TValue> is a generic type, allowing:
  • static typing (and compile-time verification)
  • use without boxing
If you are .NET 2.0 or above, you should prefer Dictionary<TKey,TValue> (and the other generic collections)
A subtle but important difference is that Hashtable supports multiple reader threads with a single writer thread, while Dictionary offers no thread safety. If you need thread safety with a generic dictionary, you must implement your own synchronization or (in .NET 4.0) useConcurrentDictionary<TKey, TValue>.

SQL - Interview Questions

Can one retrieve only the Nth row from a table?
SELECT * FROM (
   SELECT name,ROWNUM RN FROM test_employee WHERE ROWNUM < 8 )
 WHERE  RN = 7;


http://www.orafaq.com/wiki/ROWNUM


Any other efficient way:
--returns the 7th row, assuming minimum 7 rows exists
SELECT rownum,name,id,mid FROM test_employee a
 GROUP BY rownum,name,id,mid HAVING rownum = 7;


--returns the fourth row, assuming minimum 4 rows exists
SELECT * FROM test_employee WHERE rownum=1 AND rowid NOT IN
(SELECT rowid FROM test_employee WHERE rownum <= 3);


--returns 8th row, assuming, min of 8 rows exists
SELECT * FROM test_employee t1
WHERE  rowid = (
        SELECT rowid FROM test_employee t1
        WHERE  rownum <= 8
        MINUS
        SELECT rowid FROM test_employee t1
        WHERE  rownum < 8);


--Adding a column after a specific column
ALTER TABLE tablename ADD columnname AFTER columnname;


CREATE TABLE tab1 AS SELECT 0 AS col1, col1 AS col2 FROM tab1_old; .
alter table test_employee add  dept_id number;
alter table test_employee add  sex varchar2(1);


--to find the males and females in a company
select  sum decode(sex,'M',1,0)) MALE,
             sum decode(sex,'F',1,0)) FEMALE,
             count(decode(sex,'M',1,'F',1)) TOTAL
  from test_employee;


--To find the number of males, females department wise
select dept_id, sum decode(sex,'M',1,0)) MALE,
             sum decode(sex,'F',1,0)) FEMALE,
             count(decode(sex,'M',1,'F',1)) TOTAL
  from test_employee
 group by dept_id;


--2nd max from the table
select max(id) from test_employee where id <
(select max(id) from test_employee);


Implementing IF-THEN-ELSE login in select statement:
One can use the CASE statement or functions like DECODE, NVL, NVL2, NULLIF, COALESCE, etc.