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.