Saturday, May 15, 2010

Sort on a field

empfile
12345,Mary J,HR
34512,J Smith,Admin
34700,A Ryan,Admin
34900,B Wilson,HR
59000,C Diaz,HR

We want to sort the empfile by department (3rd field)

$sort -t, -k3 empfile
34512,J Smith,Admin
34700,A Ryan,Admin
12345,Mary J,HR
34900,B Wilson,HR
59000,C Diaz,HR

Option -t is to indicate field separator which here is comma
Option -k is the key indicating the position to sort on

Join two files based on a column

empfile
12345,Mary J,HR
34512,J Smith,Admin
34700,A Ryan,Admin
34900,B Wilson,HR
59000,C Diaz,HR

mgrfile
12345,HR
34700,Admin

So if we need to get name of the managers for each department then we would need to join the mgrfile with empfile on employee number.

$join -t , -o '2.2 2.3' mgrfile empfile
Mary J,HR
A Ryan,Admin

Option -t is to specify the field separator in the files.
Option -o is to list the fields that we need in the output. "2.3" means 3rd field of 2nd file.

By default join command joins on the first field of the files. So if we needed to get the manager for each employee, we would join the two files on department type.

To do this correctly both the files should be sorted** on the field being used for join.

$join -t, -13 -22 -o '1.2 1.3 2.1' empfilesorted mgrfilesorted
J Smith,Admin,34700
A Ryan,Admin,34700
Mary J,HR,12345
B Wilson,HR,12345
C Diaz,HR,12345

We have two new entries on the cmd above : -13 and -22. These can also be written as -1 3 and -2 2. These indicates the fields we are joining on.
-1 3 means 3rd field of 1st file

**See here on how to sort on a field.

Friday, May 14, 2010

PL /SQL Data Type - Constant

PL SQL has data type called constant. As the name implies, the field with this data type is immutable after declaration.

In the code below, un-commenting the meow line results in error.

declare
const_ruchi constant varchar2(5) :='Ruchi' ;

begin
--const_ruchi :='meow';
dbms_output.put_line('Schema Owner is ' || const_ruchi) ;
end;

Thursday, May 13, 2010

Hello PL/SQL

--Started learning PL/SQL using this simple undaunting tutorial.

declare
luckynbr number(2);
abcdate date;

begin
select sysdate into abcdate from dual;
select dbms_random.value(0,100) into luckynbr from dual;

dbms_output.put_line('Your lucky number on ' || abcdate || ' is ' || luckynbr );

end;
/

Tuesday, May 11, 2010

Unix Fold

Fold is not just a formatting tool for word wrapping but can come in handy for text editing as well.

check this code
cat apple | fold -1 | sort | sed -n '/^[aeiouAEIOU]/p'

When we pass the content of apple to fold command, it breaks the content to 1 character per line as defined by the width parameter of fold.

$cat apple
apple

$cat apple | fold -1 (use fold -w1 for ksh)
a
p
p
l
e

And when we add sort to the above command and look lines beginning for vowels in
$cat apple | fold -1 | sort | sed -n '/^[aeiouAEIOU]/p'
a
e

Isn't that neat.

Base code from here.

Saturday, May 8, 2010

Count vowels in a file and order by count descending

$ cat vowelfile
this
that
these

$ grep -io [aeiou] vowelfile | uniq -c | sort -rk1
2 e
1 i
1 a

This has three parts:

grep -io
-i ignores the case and -o prints just the part of the string it matches. So it will just list a, e, i, o or u instead of printing the complete line with vowel.

$ grep -io [aeiou] vowelfile
i
a
e
e

uniq -c
Counts the number of unique lines in the grep output.
$ grep -io [aeiou] vowelfile | uniq -c
1 i
1 a
2 e

sort -rk1
sort with option -r reverses the default sort order, which is ascending. By default sort is on the entire line. Option -k allows us to specify the field number to sort on. In the example the first field is the count and the second field is the vowel.


And to get the total counts of the vowel:
grep -io [aeiou] vowelfile | wc -l
wc with -l does the count on lines

Took the basic code from here
http://www.geekinterview.com/question_details/55489

Friday, May 7, 2010

diff and sdiff

diff can be used to compare differences between two files or two directories as well.
cat poem1
twinkle twinkle
oompa loompa

cat poem2
twinkle twinkle
little star
how I wonder
what you are.

diff poem1 poem2
2c2,4
< oompa loompa
---
> little star
> how I wonder
> what you are.


If we use the recursive option -r, just like we do with rm or find etc, we can compare content of the sub dir as well.

diff -r dir1 dir2
Only in dir1/dir11: testfile11
diff -r dir1/testfile1 dir2/testfile1
1,2d0
< this is a line in dir1 file test1
< this is another line in dir1/test1
Only in d1: t2

Without option -r
diff dir1 dir2
Common subdirectories: dir1/dir11 and dir2/dir11
diff dir1/testfile1 dir2/testfile1
1,2d0
< this is a line in dir1 file test1
< this is another line in dir1/test1
Only in d1: t2

For intensive file comparisons sdiff is better as it lets side by side comparison.
sdiff poem1 poem2
twinkle twinkle twinkle twinkle
oompa loompa | little star
> how I wonder
> what you are.