Friday, December 30, 2011

How do Oracle process Hierarchical queries

Not yet published. Sorry for the inconvenience caused.

Oracle Hierarchical queries


It was really confusing when I was going through the documentation of Oracle Hierarchical queries for the first time. I red it many times to understand it well,  practiced many times to save it in my memory and put it down in my own words so that it stays there for a long time in the way I understood it. And I hope you too can understand it well.

When we talk about hierarchical queries, then it is very well clear that we are dealing with hierarchical data. Hope you know what a hierarchical data refers to. It can be a parent child relation, boss to employee relation, manager to reportee etc. So whatever such data we have in a table we need to select the data suitable to our convenience.

Understanding Hierarchical query clause:
When we are trying to fetch data from our hierarchical table, it is very obvious that we need to know two things.
  1. From which level of hierarchy we need to start? That is we need a starting point.
  2. What kind of relationship are we looking in this hierarchy? That is we need a connection between the data.
Hope you understood above two parts, if then, you wont forget your hierarchical query clause hereafter. For our hierarchical query clause we need both the above "start with" and "connect" keywords.

START WITH condition CONNECT BY condition

Hence START WITH specifies the root row of the hierarchy and CONNECT BY specifies the relationship between the parent rows and child rows of the hierarchy.

Initially while I was going through the examples of hierarchical query the operation PRIOR was so confusing for me and I was not able to understand this code well enough. Then I learned in a different method and now it become easy for me. Let me try to explain it in the below example.

--Create one table to hold our hierarchical data

SQL*Plus: Release 10.2.0.1.0 - Production on Fri Dec 30 17:52:02 2011
Copyright (c) 1982, 2005, Oracle.  All rights reserved.

Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> create table hierarchy_test
  2  (
  3  emp_id number(10),
  4  l_name varchar2(10),
  5  m_name varchar2(10),
  6  f_name varchar2(10),
  7  manager_id number(10),
  8  constraint hierarchy_test_pk primary key (emp_id)
  9  );
Table created.
SQL>

--Now insert the hierarchical data into this table

 SQL> INSERT INTO hierarchy_test VALUES (100,'King','','','');
1 row created.
SQL> INSERT INTO hierarchy_test VALUES (101,'Scott','','','100');
1 row created.
SQL> INSERT INTO hierarchy_test VALUES (102,'King','','',101);
1 row created.
SQL> INSERT INTO hierarchy_test VALUES (103,'King','','',101);
1 row created.
SQL> INSERT INTO hierarchy_test VALUES (104,'King','','',101);
1 row created.
SQL> INSERT INTO hierarchy_test VALUES (105,'King','','',103);
1 row created.
SQL> INSERT INTO hierarchy_test VALUES (106,'King','','',103);

--Example for hierarchical query
SQL> select emp_id, manager_id, level
  2  from hierarchy_test
  3  start with emp_id=100
  4  connect by  PRIOR emp_id =  manager_id
  5  order by level;


    EMP_ID MANAGER_ID      LEVEL
---------- ---------- ----------
       100                     1
       101        100          2
       104        101          3
       102        101          3
       103        101          3
       106        103          4
       105        103          4
7 rows selected.

You can use PRIOR keyword on either side of the operator '='. But I got confused why? What difference does it make. Also I was not able to understand why this keyword is used here.

Let me read this clause as below so that I make sure that I understood it well.

connect by  PRIOR emp_id =  manager_id
as
connect by previous emp_id = manager_id of the current row.

So, here in the example, first row emp_id=100, manager_id=null.
Now to get the current row get all the records for which the manager_id is equal to the employee id of the previous record. Now you go back and look into the result, you will be clear on why we use PRIOR in our code.

You can rewrite the above code as below with same result, as per my above sentence.

 SQL> select emp_id, manager_id, level
  2  from hierarchy_test
  3  start with emp_id=100
  4  connect by   manager_id =PRIOR emp_id
  5  order by level;
    EMP_ID MANAGER_ID      LEVEL
---------- ---------- ----------
       100                     1
       101        100          2
       104        101          3
       102        101          3
       103        101          3
       106        103          4
       105        103          4
7 rows selected.
SQL>

What if you give the PRIOR keyword just opposite as below for our data?

SQL> select emp_id, manager_id, level
  2  from hierarchy_test
  3  start with emp_id=100
  4  connect by   emp_id =  PRIOR manager_id
  5  order by level;
    EMP_ID MANAGER_ID      LEVEL
---------- ---------- ----------
       100                     1
SQL>

We get only one result set right?
Here what happens is we started with emp_id 100 and for the next row we said that, get me the row for which the employee id is the manager id of the previous record!!

Ooops!!
Kind don't have a manager and hence we have that column value as null. Hence oracle doesn't find any way to proceed further. Hence we got only one record.

Hope you got my point.

Now, what if I am not using the start by keyword. Your code will work but result is bad and not as expected.

SQL> select emp_id, manager_id, level
  2  from hierarchy_test
  3  connect by  PRIOR emp_id =  manager_id

    EMP_ID MANAGER_ID      LEVEL
---------- ---------- ----------
       104        101          1
       106        103          1
       101        100          1
       103        101          1
       105        103          1
       100                     1
       102        101          1
       106        103          2
       105        103          2
       104        101          2
       103        101          2
    EMP_ID MANAGER_ID      LEVEL
---------- ---------- ----------
       102        101          2
       101        100          2
       104        101          3
       102        101          3
       103        101          3
       105        103          3
       106        103          3
       106        103          4
       105        103          4
20 rows selected.
SQL>


Level 4 considers only 105 and 106 whereas level 3 consider 105,106, 102, 103 and 104 and it goes like that till level 1. Hence give the start with keyword with your hierarchical query.

Related Articles:
How do Oracle process Hierarchical queries.


****
Hope this is useful. Thanks Phoenix.


Wednesday, December 28, 2011

UNIX interview questions

Q: What is the difference between vi and emacs editor?
Ans: vi is a modaleditor whereas emacs is not. That is in vi editor you have edit mode and command mode whereas in emacs only one mode. For more details visit vi editor.

Q: UNIX is case sensitive. To avoid this issue with case sensitive what option you can use while searching for a pattern?
Ans:
grep 'Phoenix' *.txt may give you output, whereas grep 'PHOENIX' *.txt may not.
To avoid this case sensitivity use the option 'i'
grep -i 'PHOENIX' *.txt

Q: What is meant by a filter in UNIX?
A filter is a program which can receive a flow of data from std input, process (or filter) it and send the result
to the std output.

Q: What is the significance of tee command?
Ans:
A powerful command that reads the standard input and sends it to standard output while redirecting a copy to a file specified. It is just re-route the pipline with tee.

Q: What is the significance of awk command?
Ans:
Helps you grab specific columns of information, modify text as it follow past, and swap the order of column information in a file.

Helps in analyzing and manipulating text files. Another alternative for this is the sed command which is less powerful compared to awk command.

Q: What does the command “ $who | sort –logfile > newfile” do?
Ans: This example explains the tricky use of hyphen (-). Above code gives the output from who command as input for sort command, meanwhile sort command will open the file called logfile and the content of this file is sorted togehter with the output of who and moving that to the file called newfile.

Q: What does the command “$ls | wc –l > file1” do?
Ans: Here ls is the input for the command wc and takes the count of line from ls and the count of lines is stored into file1 instead of displaying in the monitor.

Q: Which of the following commands is not a filter man , (b) cat , (c) pg , (d) head
Ans:
Ans: man
A filter is a program which can receive a flow of data from std input, process (or filter) it and send the result
to the std output.
Q: What is the difference between the redirection operators > and >>?
Ans:
When redirection operator > overwrite the content of file >> operator appends to file.

Q: Explain the steps that a shell follows while processing a command.
Ans:
Below are the steps followed by shell

Parsing: Shell first breaks up the command line into words, using spaces and delimiters, unless quoted. All consecutive spaces or tabs are replaces with single space.

Variable Evaluation: All words preceded by a $ are evaluated as variables, unless quoted or escaped.

Command Substitution:Any commands surrounded by backquotes is executed by the shell which then replaces the standard output of the command into the command line.

Wild Card Interpretation: The shell finally scans the command line for the wildcard characters (*,.,?,[,]). Any word containing a wild card is replaced by a sorted list of fienames that match the pattern. The list of this filenames then become the argument to the command.

PATH Evaluation:
It finally looks for the PATH variable to determine the sequence of directories it has to search in order to hunt for the command.

Q: What difference between cmp and diff commands?
Ans:
cmp - Compares two files byte by byte and displays the first mismatch
diff - tells the changes to be made to make the files identical

Q: What is the difference between cat and more command?
Ans:
Cat displays file contents. If the file is large the contents scroll off the screen before we view it. So command more is like a pager which displays the contents page by page.

Q: Write a command to kill the last background job?
Ans:
Kill $!

Q: Which command is used to delete all files in current directory and all its sub-directories?
Ans:
rm -r *

Q: Write a command to display a file’s contents in various formats?
Ans:
$od -cbd file_name

c - character, b - binary (octal), d-decimal, od=Octal Dump.

Q: What will the following command do/
$echo *
Ans;
It is similar to ls command, list down all the files in the current directory.

Q: Is it possible to create a new file system in UNIX?
Ans: Yes, 'mkfs' is used to create a new file system.

Q: Is it possible to restrict the incoming message?
Ans: Yes, using the 'mesg' command

Q:What is the use of the command "ls -x chapter[1-5]"?
ls stands for list; so it displays the list of the files that starts with 'chapter' with suffix '1' to '5', chapter1, chapter2, and so on.

Q: Is ‘du’ a command? If so, what is its use?
Ans:
Yes, it stands for ‘disk usage’. With the help of this command you can find the disk capacity and free space
of the disk.
Q: Is it possible to count number char, line in a file; if so, How?
Ans:
Yes, wc-stands for word count.
wc -c for counting number of characters in a file.
wc -l for counting lines in a file.

Q: Name the data structure used to maintain file identification?
Ans:
inode, each file has a separate inode and a unique inode number.

Q: How many prompts are available in a UNIX system?
Ans:
Two prompts, PS1 (Primary Prompt), PS2 (Secondary Prompt).

Q. How does the kernel differentiate device files and ordinary files?
Kernel checks 'type' field in the file's inode structure.


***
Hope this is helpful. Thanks Phoenix.

Thursday, December 22, 2011

Oracle sql statement processing steps.

Whenever a SQL statement is submitted to execute, Oracle goes through the below mentioned steps

  1. Check for identical statements in memory to avoid performance overhead due to parsing.
  2. Allocate memory in shared memory area.
  3. Evaluate the syntax of the statement to check whether all the oracle key words are spell correctly.
  4. Do semantic check, where all the objects (tables or columns) are validated and check for the user's privilege to access these objects.
  5. Form an execution plan to execute the statement.
****
Hope this is useful. Thanks Phoenix

Wednesday, December 21, 2011

Oracle Autonomous Transaction



Let me try to explain Oracle Autonomous Transaction in a little different way so that you can remember it very easily.

How many different types of plsql blocks can you write?

Ooops!!

Procedures and Functions are the pls/ql blocks written uniquely with or without parameters. What else could I do with this? What are all these different types available? Let us try it out.

1. We use to write a Procedure/Function without giving it a name for adhock purpose as we don't want to store that in the database to use it later. - ANONYMOUS PROC/FUNC

2. We use to use this anonymous items in declaration section of plsql as well - LOCAL PROC/FUNC

3. When we need this proc/func to be stored in database to use frequently, we store it with a name tagged to it- STAND ALONE PROC/FUNC or say stored Procedure or Function.

4. To logically group our procs/funcs we can place these procedures or functions inside a package - PACKAGED PROC/FUNC

So now we know there are 4 different ways we can use procedures and functions.
We can use all the above mentioned different ways of proc/fncs for autonomous transactions.

Autonomous Transactions?????

I had heard about Autonomous Institutes we're they function independently.
PL/SQL autonomous transaction also work exactly the same. They function independently. It helps you leave the calling transaction and performs an individual transaction and resume to the calling transaction without affecting the calling transaction.

Confused? No need to worry. You will get it by the time you complete this.

Here calling transaction refers to your main PL/SQL block and the autonomous transaction is called from this block. The purpose of your main PL/SQL block and autonomous PL/SQL block is entirely different. Both behave as transaction done is seperate sessions. There is no link between both these type of transactions, hence only committed data can be shared among them.

Autonomous transaction code block is nothing but plsql blocks. Syntax is as below,

Example for anonymous:

DECLARE
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
Statement;
COMMIT;
END;
/

Example for stored PROC:
CREATE OR REPLACE PROCEDURE PROCEDURE_NAME AS
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
statement;
COMMIT;
END;
/

We have 5 different type of plsql blocks which can be used for this purpose. Four of them are those discussed above and the fifth one is "TYPE methods".

Let us try to understand autonomous transaction a little more deep by going through few scenarios.

1. You need to insert two records into an empty table.

INSERT INTO temp_table (id, name) VALUES (1, 'Scott');
INSERT INTO temp_table (id, name) VALUES (2, 'Peter');

Next without committing this transaction you want to execute an autonomous transaction, say an individual transaction.

Before executing the autonomous transaction, select count(*) give you result: 2

SELECT count(*) FROM temp_table;

Execute autonomous transaction which inserts 5 records to the same table and have a commit statement within this autonomous transaction code block.

DECLARE
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
FOR i IN 3 .. 5 LOOP
INSERT INTO temp_table (id, name)
VALUES (i, 'Name ' || i);
END LOOP;
COMMIT;
END;
/

Now select count(*) give you result as expected:7

Now execute a rollback.

Rollback;

Select count(*) gives you only 5 as the result.

The records inserted in current transaction got rolled back. The commit affected only the autonomous transaction block.

Hope now, you are clear with how to user autonomous transaction.

Now the question is why should we use autonomous transaction?
It is most popularly used for error logging. In applications particularly like banking, you can't commit a transaction until it is successfully completed. Hence to capture any error that may happen in between any transaction we use autonomous transaction.

How can we use this?
Create a proc as autonomous which logs data into one table. Call this proc in the exceptional section of your plsql block before you rollback.The error data is committed successfully by the autonomous transaction and your calling block actions are rolled back

Restrict the use of autonomous transaction only for error logging, else it will be a disaster.

Hope you understood well. Regards Shiyas..



Tuesday, December 13, 2011

How to retrieve accidentally overwritten content in my blog

Necessity is the father of invention!!

Today I feel, I tried something out of the box for a solution I need and here I share it with my readers.

I worked hard on my third blog BUS#356 V, edited and edited, red many times and just before posting in social networks called up one of my close friend to review. Upon receiving nice feedback, I posted and was doing my final touch up. I was working on my friend's brand new Apple laptop the functionality of which is awesome but sophisticated as well. I ended up in deleting the content of my blog without any backup.

My friends started calling me up to tell me that my blog is blank. I go broke on realizing that all my work is gone within a second. I was getting more hits into my site and everyone could see only blank page. Only option I have is to write it again but still it cant be in its original form as I have spent too much time on that.

I started googling for any options to retrieve my overwritten data. I can find only for deleted one, but not for overwritten files in blog site. Then I started looking into the history. But that too goes in vain. The browsing history has all the urls, but as it says "All Roads Lead To Venice", all the history urls upon clicking on that will take me to my blogsite where I can see only blank content which was latest update saved in the web.

I sat for some time not knowing what to do. I start setting up my mind for writing the entire thing once again, then work and rework on it. I made up my mind.

Suddenly the thought came to mind, what if I hit the history URL which I havent touched and dont allow the system to get the data from web. Will I get the page which I visited?

Didnt waited for a second thought, I pulled out the network cable hastly. Searched for the list of history. Selected the one which I havent touched after this data loss and hit.

I got my data which need only slight changes which I have done after my friends review.


In future, if this can be useful to atleast one, I will be happy.

Please don't forget to leave me a comment, if you are reading this for a solution.

Friday, December 9, 2011

Oracle Interview Questions Part 1: SQL

Q: What is the difference between delete and truncate?
Ans:
1. Delete can be rolled back before commiting the transaction whereas a truncate cannot be rolled back
2. Delete is a DML whereas Truncate is DDL
3. Truncate deletes the entire data at once whereas delete does it row by row.
4. Truncate is faster and performance benefit when compated to delete, if the requirement is to flush the table completely.

Q: What is the syntax of DECODE function and what is the use?
Ans: It has the same functionality IF-THEN-ELSE statement.
syntax: decode(expression,search[,result,search,result..][,default])

Example:
select decode (department, 1, 'Finance',2,'Accounts', 'Miscellaneous') from dual;

Q: What does COALESE function does?
Ans:
COALESCE function goes through the given list of values/expressions and returns the first non null expression. If all the values/expressions are null then it returns null. COALESCE is similar to IF-THEN-ELSE statement.

syntax: coalesce( expr1, expr2, ... expr_n )
SELECT coalesce( firstname, middlename, lastname) result
FROM employees;

Its equivalent IF-THEN-ELSE is as below.

IF firstname is not null
THEN
result:=firstname;
ELSIF middlename is not null
THEN
result:=midddlename;
ELSIF lastname is not null
THEN
result:=lastname
ELSE
result:=null;
end if;

Q: A given column has values with NULL data. On sorting the column in ascending order, where does the null value comes? First or Last?

Ans: Last

Q: What is the default sort order in Oracle?

Ans: ascending


Q: Provided a list of numbers or characters or dates. What function helps you to retrieve the highes value from the list?
Ans: greatest(x,y,..)
and for the least it is least(x,y,..)

Q: The column firstname is of datatype varchar2 which has null values as well. What is the output of below function for a null value?
NVL(firstname,0)
Ans: NVL returns the passed value if the expression is null else returns the erpression.
Here the function fails as the column is of datatype varchar2 and the specified value is of type number. Hence it fails.

For NVL to work, the data type of both column and passing value should be same.

Q: How NVL2 works?
Ans: syntax: NVL2(X,Y,Z)
NVL2 returns Y if X is not null, else returns Z

Q: What is the syntax for NULLIF?
Ans: NULLIF(x,y)
function returns NULL if x=y else returns X

Q: Write an SQL query to fetch all the managers from an employee table whose JOB_ID ends with either '_MAN' or '_MGR'

Ans:
SELECT FIRST_NAME, LAST_NAME, JOB_ID
FROM EMPLOYEES
WHERE REGEXP_LIKE(JOB_ID, '(_m[an|gr])', 'i');

[parameter i indicates case-insensitive]

Q: Select every employee whose last name has a double vowel
(two adjacent occurrences of the same vowel).
Ans:
SELECT FIRST_NAME, LAST_NAME
FROM EMPLOYEES
WHERE REGEXP_LIKE(LAST_NAME, '([AEIOU])\1', 'i');

Q: Exmaple for using REGEXP_REPLACE

Ans:
SELECT PHONE_NUMBER "Old Format",
REGEXP_REPLACE(PHONE_NUMBER,
'([[:digit:]]{3})\.([[:digit:]]{3})\.([[:digit:]]{4})',
'(\1) \2-\3') "New Format"
FROM EMPLOYEES
WHERE DEPARTMENT_ID = 90;



Old Format                                              New Format
-----------------------------------------------------------------
515.123.4567 (515) 123-4567
515.123.4568 (515) 123-4568
515.123.4569 (515) 123-4569

The search pattern has three regular expressions, each of which is enclosed in parentheses. The metacharacter [[:digit:]] represents a digit, the metacharacter {n} specifies n occurrences, and the metacharacter \ is an escape character. The character immediately after an escape character is interpreted as a literal. Without the escape character, the metacharacter . represents any character. The replace string uses \1, \2, and \3 to represent the first, second, and third regular expressions in the search pattern, respectively. (In the replace string, \ is not an escape character.)


Q: Example to extract Street number including hyphen from the given street address.
Ans:
select street_address address, REGEXP_SUBSTR(street_address,'[[:digit:]-]+') "Number" from locations

Address                                   Number
-----------------------------------------------
2007              Zagora St 2007
2004              Charade Rd 2004
147                Spadina Ave 147
6092              Boxwood St 6092
40-5-12         Laogianggen 40-5-12



Q: Can we use a oracle keyword as alias in any select statement?
select 1 number from dual;

ORA-00923: FROM keyword not found where expected
00923. 00000 -  "FROM keyword not found where expected"
*Cause:   
*Action:
Error at Line: 1 Column: 9

solution: Use the keyword in double quotes

select 1 "number" from dual;

Output:
number
---------
1

Q: Example to count number of spaces in a street address
Ans:

Q: What is the result of below query? Why?
select 10000 + null from dual;

Ans: null. In oracle any scalar operation with null always results in null.

Wednesday, December 7, 2011

How to type in French charcters from your keyboard

Search for French Characters in your key board ends here!!


Fortunately or Unfortunately I came across a requirement to type in French characters in my system. I started searching for those odd characters in my key board and the end result was failure. I couldn't find it at all..

Before explaining how to type in French Characters let us have a basic understanding about French alphabets.

French alphabet consist of 26 letters, lowercase and uppercase, with five diacritics and two orthographic ligatures. The latter part, diacritics and ligatures, is what we are inerested about and need to know how we type it in.

Below listed down are the Diacritics and Ligatures:

Àà, Ââ, Ææ
Çç
Éé, Èè, Êê, Ëë
Îî, Ïï
Ôô, Œœ
Ùù, Ûû, Üü
Ÿÿ

Getting confused with the special characteres?
Lets us make it much more simple. For this let us look into Diacritics and Ligatures seperately.

Diacritics:
All the special characters that you have seen in the above table on top of each characters is called Diacritic Marks. The possible diacritic marks are the acute(´), grave(`), and cicumflex (^) accents, the diaresis  (¨)  and the cedilla ( ¸ ).

Let us not go much deep into how these are combined with our alphabets and how it is pronounced.

Ligatures:
There are two ligatures œ and æ which are treated like the sequences oe and ae.

Now, I hope you got a basic idea about the French characters and so moving to check on how to type in those characters from our keyboard.

The idea of typing in French characters works out with alt key and some 3 or 4 digit numbers. What you have to do is to press down the alt key and enter the 3 or 4 digit number and on releasing the alt key, your required French character will appear.

Below are the list of combinations and now its time for you to test it.
Grave AccentAccute AccentCircumflexTrémaLigatureCedilla
aALT + 133 (à)NAALT + 131 (â) ALT + 132 (ä)ALT + 145 (æ)NA
AALT + 0192 (À)NAALT + 0194 (Â)ALT + 142 (Ä)ALT + 146 (Æ)NA
eALT + 138 (è)ALT + 130 (é)ALT + 136 (ê)ALT + 137 (ë)NANA
EALT + 0200 (È)ALT + 144 (É)ALT + 0202 (Ê)ALT + 0203 (Ë)NANA
iNANAALT + 140 (î)ALT + 139 (ï)NANA
INANAALT + 0206 (Î)ALT + 0207 (Ï)NANA
oNANAALT + 147 (ô)NAALT + 0156 (œ)NA
ONANAALT + 0212 (Ô)NAALT + 0140 (Œ)NA
uALT + 151 (ù)NAALT + 150 (û)ALT + 129 (ü)NANA
UALT + 0217 (Ù)NAALT + 0219 (Û)ALT + 154 (Ü)NANA
CNANANANANAALT + 135 (ç)
CNANANANANAALT + 128 (Ç)


Found it useful? Please leave a comment.

Monday, December 5, 2011

Replace funtion in Javascript

Replace function description:
The replace function, replace(), looks for a substring in the given string and replaces that with new value provided.

The syntax for using this is:

string.replace(substring,newstring)

Example:
Assume variable string has values assigned as string ="AtoZ on Tech"
Using replace functin on this, provide the substring and newstring to replace
i.e
<script type="text/javascript">
var string="AtoZ on Tech";
string=string.replace("to","2");
document.write(string);
</script>


Output:
A2Z on Tech

Done a slight modification in susbstring. Will this work?

<script type="text/javascript">
var string="AtoZ on Tech";
string=string.replace(/to/,"2");
document.write(string);
</script>


Yes, this works same exactly as the above code.

Will the below code work?
<script type="text/javascript">
var string="AtoZ on Tech";
string=string.replace("To","2");
document.write(string);
</script>


No, as here "T" is in uppercase. So, how can we have a case-insensitive search and replace?
<script type="text/javascript">
var string="AtoZ on Tech";
string=string.replace(/To/i,"2");
document.write(string);
</script>


Output: A2Z on Tech

Now, let me extend our string variable. as below.
<script type="text/javascript">
var string="AtoZ on Tech";
string= string + " is a blogsite discussing all about technology from A to Z";
string=string.replace(/To/i,"2");
document.write(string);
</script>


Output:A2Z on Tech is a blogsite discussing all about technology from A to Z
The second "A to Z" is as it is. Here only the first occurance is replaced. How to replace all the occurences of search string?

Then you should give the option for global represented by "g.

<script type="text/javascript">
var string="AtoZ on Tech";
string= string + " is a blogsite discussing all about technology from A to Z";
string=string.replace(/To/ig,"2");
document.write(string);
</script>


Now you know that we have used "i" for case-insensitive matching and "g" for global matching. These are called modifiers. Modifiers will specify what kind of search we have to do. There is one more modifier used in javascript, ie "m" for multiline matching. This we will be discussin in our later blogs.


Ok. So far is fine. What if you dont have a specific substring and you have only a pattern to be replaced?

Then you have to go for Regular Expression.

What is Regular Expression?
Regular Expression gives a pattern of characters like a-z for chars, 0-9 for int and can have some special character pattern as well.

Using regular expression we search for any pattern defined in the regular expression(RegExp) and replace it with the required string/value.

<script type="text/javascript">
var string="A2Z on Tech";
string=string.replace(/[^a-z,0-9]/ig,"-");
document.write(string);
</script>


The above code looks for a pattern match (any character other than a-z, A-Z and number 0-9) and is replaced by -. (This is one which we generally use to generate SEO friendly URL)

Friday, December 2, 2011

Find the number of occurence of a character in a given string in Oracle

Scenario: Need to find out the number of occurrence of a particular character in the given string.

Example: Find number of "e"s in the given string ("Oracle is an interesting thing to learn.")

Solution:
select length('Oracle is an interesting thing to learn') - length(replace('Oracle is an interesting thing to learn','e',''))
from dual;
4

Explained:
59-55 = 4, Hence four occurrence.

Another one Use Case:
Get number of comma separated values in a given column where the values are 100,123,124,145.

Use above method to find number of occurrence of character , (comma) in the givens string and add one to that gives you the number of comma separated values.


In Oracle 11g we have new function to save our time and shorten our code length.

REGEXP_COUNT

select regexp_count('EXPERIENCE','E') FROM DUAL;

This will give you the result 4.

Thursday, December 1, 2011

Understanding UNIX hierarchichal file system

In UNIX the fundamental basic principle is that anything that is not a folder is file, whether it be a normal file, I/O connections anything.

Using calculator in UNIX

UNIX provides both INFIX and POSTFIX calculators.

INFIX is when the operations are embedded within the operators
12*2

POSTFIX is when the operators are listed down and then followed by operations.
12 2 *

So, now to use infix calculator type the command "bc"
bash-3.00$ bc
Nothing happend. Now type in what you need to calculate and the system gives you output
1*2
2
1*2+3
5
2+3*2
8

Ok, now how to get out of this? Its simple. "quit"
.
.
2+3*2
8
quit
bash-3.00$

Various powerful options with bc command:

Find square root?

bash-3.00$ bc
sqrt(16)
4
similarly use below:


Postfix calculator:

user command "dc"

bash-3.00$ dc
2
3
*
p
6
2 3 *
p
6
2 3 * 4 +
p
10
Use "p" to print the result of your calculation.
sqrt(n) Square root of n
% Remainder
^ To the power of (3^5 is 3 to the power of 5)
s(n) Sine(n)
c(n) Cosine(n)
e(n) Exponential(n)
l(n) Log(n)

How to find the date and time in an UNIX system

What I would like to know is the current date and time, so just type in command "date"

bash-3.00$ date
Fri Dec  2 11:02:42 IST 2011


What if I need to look into the calendar? Here it is..


bash-3.00$ cal
   December 2011
 S  M Tu  W Th  F  S
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

This list down only current month. What if I need the entire year?

Its very simple. With the command "cal" mention the year you are interested in

bash-3.00$ cal 2011

                                2011
         Jan                    Feb                    Mar
 S  M Tu  W Th  F  S    S  M Tu  W Th  F  S    S  M Tu  W Th  F  S
                   1          1  2  3  4  5          1  2  3  4  5
 2  3  4  5  6  7  8    6  7  8  9 10 11 12    6  7  8  9 10 11 12
 9 10 11 12 13 14 15   13 14 15 16 17 18 19   13 14 15 16 17 18 19
16 17 18 19 20 21 22   20 21 22 23 24 25 26   20 21 22 23 24 25 26
23 24 25 26 27 28 29   27 28                  27 28 29 30 31
30 31
         Apr                    May                    Jun
 S  M Tu  W Th  F  S    S  M Tu  W Th  F  S    S  M Tu  W Th  F  S
                1  2    1  2  3  4  5  6  7             1  2  3  4
 3  4  5  6  7  8  9    8  9 10 11 12 13 14    5  6  7  8  9 10 11
10 11 12 13 14 15 16   15 16 17 18 19 20 21   12 13 14 15 16 17 18
17 18 19 20 21 22 23   22 23 24 25 26 27 28   19 20 21 22 23 24 25
24 25 26 27 28 29 30   29 30 31               26 27 28 29 30

         Jul                    Aug                    Sep
 S  M Tu  W Th  F  S    S  M Tu  W Th  F  S    S  M Tu  W Th  F  S
                1  2       1  2  3  4  5  6                1  2  3
 3  4  5  6  7  8  9    7  8  9 10 11 12 13    4  5  6  7  8  9 10
10 11 12 13 14 15 16   14 15 16 17 18 19 20   11 12 13 14 15 16 17
17 18 19 20 21 22 23   21 22 23 24 25 26 27   18 19 20 21 22 23 24
24 25 26 27 28 29 30   28 29 30 31            25 26 27 28 29 30
31
         Oct                    Nov                    Dec
 S  M Tu  W Th  F  S    S  M Tu  W Th  F  S    S  M Tu  W Th  F  S
                   1          1  2  3  4  5                1  2  3
 2  3  4  5  6  7  8    6  7  8  9 10 11 12    4  5  6  7  8  9 10
 9 10 11 12 13 14 15   13 14 15 16 17 18 19   11 12 13 14 15 16 17
16 17 18 19 20 21 22   20 21 22 23 24 25 26   18 19 20 21 22 23 24
23 24 25 26 27 28 29   27 28 29 30            25 26 27 28 29 30 31
30 31


Ok, I agree. But what I need to look into the month of Feb for the year 1999?

bash-3.00$ cal 2 1999
   February 1999
 S  M Tu  W Th  F  S
    1  2  3  4  5  6
 7  8  9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28


Is that ok?

Be careful while specifying the year. You have to give fully year, iec 2011 not 11. See the difference below.

bash-3.00$ cal 12 2011
   December 2011
 S  M Tu  W Th  F  S
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

bash-3.00$ cal 12 11
   December 11
 S  M Tu  W Th  F  S
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

Wednesday, November 30, 2011

How to find out who is doing what in an UNIX system

OK, so you are here to learn how to find out who is doing what in your UNIX system.

Then first of all let me ask, who are you?
Now, you have a question in your mind, who am i?

type it down in your system and lets see what the system responds.

bash-3.00$ who am i
scott      pts/3        Nov 30 19:37    (10.203.54.223)

now you have your account name/user name, the communication line, date and time of login and your ip address as well.

Now you know who you are, right? Is that all that you need to know about you?

You know that UNIX is a multi user system. Hence the system you have logged in probably might have more users other than you. Rather than having a unique name, don't you think the UNIX system will have a unique id for each of the users in the system!! So let us check our what id Unix has assigned to you.

Type down what come to your mind. Nothing but id!!

bash-3.00$ id
uid=200(scott) gid=201(scott)


It give you both id and your group id as well. After all you don't need to bother much about UID and GID

Now you know who you are, and now its time for you to check who else is there in the system.

who

The only limitation here is, you can get details only on users who are currently logged in to the system.

bash-3.00$ who
scott      pts/2        Nov 30 11:31    (10.203.54.102)
taylor      pts/3        Nov 30 19:37    (10.203.54.223)
ram      pts/4        Nov 30 12:52    (10.203.54.4)
build      pts/5        Nov 24 19:15    (10.222.46.132)


Four users are logged into the same UNIX system. Their communication channel, the time they logged into the system and their system ip address is also listed down.


Now you know who all are there in the system. But do you know what they are doing? Watching some ...movies? You want to check it out?

Use the command "w", a combination of "who are they?" and "what they do?"

bash-3.00$ w
  8:02pm  up 215 day(s),  3:13,  8 users,  load average: 0.18, 0.20, 0.20
User     tty           login@      idle   JCPU   PCPU  what
scott    pts/2        11:31am    17                              bash
taylor    pts/3       7:37pm                                       bash


This command gives us info about the user name, ttyp, logged in time, how long since the user has done anything, combined CPU time of all jobs run by the user, CPU time taken by current job and last field tells you what the user is doing?


So, next time while you do something in your UNIX system, beware that other users can check it out, what you are doing!!

*All the commands explained here are tested against SunOS

How to change password for my UNIX account

Use the command "passwd" to change password of your UNIX account


bash-3.00$ passwd
passwd: Changing password for fwire
Enter existing login password:
New Password:
Re-enter new Password:
passwd: They don't match.
Please try again
New Password:


As I dont want to change my existing one, I gave it wrong :-) !!

Friday, November 25, 2011

How UNIX differs from other Operating System

UNIX is an operating system as like other OS available in market. Its does the task as any other OS does, running programmes, managing resources and communicating with other computer systems.

The uniqueness of UNIX OS is, multiple users can access the same system simultenously, hence we call UNIX a multiuser system. Multiple users can access the machine, run their programmes, do their work all in the same computer, which is not possible in a windows OS. Moreover these users can run multiple programmes from a UNIX machine, hence it is called a multitasking system.

UNIX is also called as a Multichoice system as it has three different primary command-line interface. This command line interface is called shell. Three available flavours of this command-line interface (shell) are Bourne shell, Korn shell and C shell.

UNIX has more than 250 individual commands ranging from simpler to complex ones for high speed networking, developing software and file revision management. Whereas Microsfot MS-DOS and Apple Macintosh has very fewer commands. These interfaces are easy to use which give only less controll to the user.

The way unix system stores data, ordering of files, connection to the external devices are entirely different from Windows or Macintosh OS. Unix use plain text for storing data; it has a hierarchical file system. Everything in UNIX is a file. Every programme, data, external devices everything. It treat devices and certain types of inter-process communication as files.

Tuesday, November 22, 2011

UNIX: Search for files

The grep command helps you to search with the text pattern in the file or filenames. It helps you to find the files by their content. Whereas find command helps you in searching for the files.

Examples:

1. Print all directories and filenames under the current directory
bash-3.00$ find . (dont miss the dot here)
.
./wm-dev
./wm-dev/bin
./wm-dev/bin/exec.sh
./wm-dev/config

2. Print all directories and filenames under the current directory/directory

bash-3.00$ find wm-dev
wm-dev
wm-dev/bin
wm-dev/bin/exec.sh
wm-dev/config

You can notice that the list printed starts with the searching directory.

3. Print all directories and filenames under the current directory/directory/directory
bash-3.00$ find wm-dev/bin
wm-dev/bin
wm-dev/bin/exec.sh

4. From the root directory search for a filename of the given pattern 'exec.sh'
bash-3.00$ find . | grep 'exec.sh'
./wm-dev/bin/exec.sh
./wm-cn/bin/ca7-orig/exec.sh.orig
./wm-cn/bin/exec.sh
./wm-cn/bin-old/exec.sh
./wm-cn-dev/bin/exec.sh
./wm-prod-dev-old/bin/exec.sh
./wm-prod-dev/bin/exec.sh
./wm-prod-dev/bin/exec.sh_090909

Another one alternative for the above option.
bash-3.00$ find . -name "exec.sh"
./wm-dev/bin/exec.sh
./wm-cn/bin/ca7-orig/exec.sh.orig
./wm-cn/bin/exec.sh
./wm-cn/bin-old/exec.sh
./wm-cn-dev/bin/exec.sh
./wm-prod-dev-old/bin/exec.sh
./wm-prod-dev/bin/exec.sh
./wm-prod-dev/bin/exec.sh_090909

Find files that have been modified in last 5 days (with minus)
find . -mtime -5

Find files that was modified exactly 5 days before (without minus)
find . -mtime 5

Find files that was not modified for past 5 days (with +)
find . -mtime +5

Find files in multiple directories.
find usr bin -name "*.cp"

List the directories alone.
find . -type d

List the files alone.
find . -type f


Hope this is helpful. Thanks Phoenix...

UNIX: Terminating a process

You can terminate a process using the KILL command. To KILL a process you need to have the process ID passed to the KILL command.

There are different types of signals that you pass to a process.
1    SIGHUP - Hang Up
2    SIGINT - Interrrupt
9    SIGKILL - Kill Immediate
15  SIGTERM - Terminate

SIGHUP signal is sent to every process you are running just before you log out of the system.
SIGINT signal is sent when you press ctrl+c
SIGKILL - Kills a process immediately. Temporary files are not deleted here.
SIGTERM - Immediate termination of progamme but allows cleaning up the temporary files.

The default signal with KILL command is SIGTERM.

You can specify the signals with KILL command either by their number or by their name without the SIG
eg:-
kill -9 process.sh
or
kill KILL process.sh

Basically there are 30 different signals in UNIX. The above explained are the most important ones.

Example:
Finding the jobs
bash-3.00$ jobs
[2]-  Stopped                 vi
[3]+  Stopped                 vi
Killing the job with % (here we terminates the job)
bash-3.00$ kill %2
bash-3.00$ jobs
[2]-  Done                    vi
[3]+  Stopped                 vi
Finding Jobs:
bash-3.00$ jobs
[3]+  Stopped                 vi
Killing jobs with KILL signal (-KILL is missing)
bash-3.00$ kill KILL %3
bash: kill: KILL: arguments must be process or job IDs
starting a process in the background
bash-3.00$ vi &
[1] 28236
bash-3.00$ jobs
[1]+  Stopped                 vi
Kill process with signal number (kill immediate)
bash-3.00$ kill -9 28236
bash-3.00$ jobs
[1]+  Killed                  vi
Checking for jobs
bash-3.00$ jobs
No jobs left

create new job in bg
bash-3.00$ vi &
[1] 28321

bash-3.00$ jobs
[1]+  Stopped                 vi

Kill with signal number option for termination
bash-3.00$ kill -15 28321

bash-3.00$ jobs
[1]+  Stopped                 vi
bash-3.00$ kill 28321
bash-3.00$ jobs
[1]+  Stopped                 vi
bash-3.00$ kill -KILL 28321
bash-3.00$ jobs
[1]+  Killed                  vi
Note: Inorder to run the kill command for a second time to make sure that the kill works, enter "!!"

UNIX: Find the running processes

Finding out Jobs that runs in your UNIX system is a simple task. The command, jobs, helps you to list the jobs which are either stopped or moved to background for process.


Below code automatically moves the vi process to the background
bash-3.00$ vi &
[1] 27467

On entering hte "jobs" command it listed the jobs that are stopped in the background.

bash-3.00$ jobs
[1]+  Stopped                 vi
bash-3.00$
Alternative to find the processes running in your UNIX system:
The command ps is another one alternative to find the jobs running in your UNIX system. This is a little complex. The command ps stands for processor status and it lists out the processor status of the entire computer.


bash-3.00$ ps
   PID TTY         TIME CMD
 27467 pts/15      0:00 vi
 27469 pts/15      0:00 ps
  3784 pts/15      0:01 bash
  3780 pts/15      0:00 sh

Monday, November 21, 2011

UNIX Process or Jobs: Suspend and Resume

In unix whatever command you execute from the command line initiate a process. Say for example you tried ls tol list files invokes the process ls.  In UNIX any program that's running is a process. You can have multiple process running at once.

Any program that you are running is known as current job.

States of Jobs/Process:
There are different states for the Jobs/Process in UNIX. The most important one is "running".

Stopping and restarting a Job:
A job can be stopped by pressing ctrl+z and to restart it enter fg.

bash-3.00$ ./logging.sh
Stopping the server please wait .........\n
Tue Nov 22 12:08:52 IST 2011
Invoking the PL/SQL Procedures and Bulk add command \n
Bulk Add Start Time:
Tue Nov 22 12:08:52 IST 2011
^Z
[1]+  Stopped                 ./logging.sh
bash-3.00$ fg
./logging.sh
Bulk Add End Time:
Tue Nov 22 12:09:09 IST 2011
Done with  PL/SQL Procedures and Bulk add. Starting the server. Please wait....\n
Tue Nov 22 12:09:09 IST 2011
1
bash-3.00$
Why we use ctrl+z to stop and fg to restart it later?
These commands helps us to stop a a job or process (ctrl+z) that we are doing currently and perform some other job and then resume (fg) our stopped job later.

For example if we are working on a vi editor editing our file and in between you want to execute some other programme press ctrl+z and once after completing your second job you can resume to editing your file by entering fg.

bash-3.00$ vi
I am testing stopping job and resuming it later
let me stop the job now !!
i am pressing ctrl+z!!   ^Z^Z^Z^Z^Z^Z
ooops!! Before pressing ctrl+z we should be in command mode!!
Moving to command mode and then pressing ctrl+z

~
~
~
Incomplete shell escape command - use 'shell' to get a shell
[1]+  Stopped                 vi
bash-3.00$
bash-3.00$ ls -l | tail -5
-rw-r--r--   1 fwire    fwire       1331 Apr 20  2011 publishpreview.sh
-rw-r--r--   1 fwire    fwire         11 Feb 25  2011 publishpreview_dummy.sh
-rw-r--r--   1 fwire    fwire        275 Oct 15 17:28 shiyas_new.txt
-rw-r--r--   1 fwire    fwire       1622 Oct 15 16:35 test.sh
-rw-r--r--   1 fwire    fwire        313 Oct 15 17:15 uniqtest.txt
bash-3.00$fg
 I am testing stopping job and resuming it later
let me stop the job now !!
i am pressing ctrl+z!!   ^Z^Z^Z^Z^Z^Z
ooops!! Before pressing ctrl+z we should be in command mode!!
Moving to command mode and then pressing ctrl+z


I am back !!
~

Wednesday, October 26, 2011

Shell Variables

Working with Shell variables.

As like any other programming languages shell also has variable (most people dont know we can do programming with shell commands.). The shell variables takes single values as strings, even the numeric values is also considered as string.

Declaring and assigning shell variable:
You can declare and assign shell variable as simple as below.
bash-3.00$ color=yellow

You need to be careful while doing this. There should not be any space before and after equal to sign '=', if exists throws error.


Referencing a shell variable:
Below command will display the value stored in the variable color


bash-3.00$ echo $color
yellow


 

what is the output?
bash-3.00$ echo $coloryish

As no variable is declared as coloryish the output will be null. If you are actually interested to display "yellowyish", place the variable in curly braces as below.

bash-3.00$ echo ${color}yish
yellowyish


What if instead of variable color we supplied colour, which is not defined?
bash-3.00$ echo The dress is ${colour}ish
The dress is ish

How to handle this? Fortunately shell helps us to have default values to be set in case the variable is not defined as below
bash-3.00$ echo The dress is ${colour:-green}ish
The dress is greenish


The default value will come into effect only if the variable referenced  is not declared. The syntax :- tells the shell to print the following character

Assigning Variables with read command:
We can assign values to multiple variables at a time with read commnad.

bash-3.00$ read city state message
Thazhekode Kerala Hi Mom!

The above command declares three variables city, state and message and assign values respectively.
city=Thazhekode
state=Kerala
message=Hi Mom!

bash-3.00$ read city state message
Thazhekode New Jersey Hi Mom!

city=Thazhekode
state=New
message=Jersy Hi Mom!

Read command assigns an individual word to a specified variable, and the remaining words are assigned to the last variable. To handle this situation you can use the escape character

bash-3.00$ read city state message
Thazhekode New\ Jersey Hi Mom!




Tuesday, October 25, 2011

UNIX: Shell scripts

Definition: Shell scripts are a series of shell commands added in a file in exactly the order in which they’ll be
executed. Upon giving execute permission for this file, you can execute this entire set of commands by just entering the file name as like a command.

Example:
This example script will search for a specified file in all dirctories.


UNIX: Setting custom prompts

The below command is based on ksh


$ bash
bash-3.00$ PS1='Order me$'
Order me$

UNIX Command Aliases

With alias, you can define new commands that does whatever you like to do with unix in a customized way.

Format for using aliases in
csh:- alias word command-sequence
ksh:- alias word='command'

Example:
bash-3.00$ alias shiyas='ls -CF'

If you want to see the list of aliases created, you only need to enter the word alias

Example:
bash-3.00$ alias
alias ls='ls -CF'
alias shiyas='ls -CF'

Note:
There should not be any space befor or after '=' sign.

If you want to make availability of an alias permanent add it into .chrsc if you are using csh or to .profile if you are using korn shell

C shell History Commands

CommandFunction
!!Repeat the previous command
!$Repeat the last word of the previous command.
!*Repeat all but the first word of the previous command.
^a^bReplace a with b in the previous command.
!nRepeat command n from the history list.

Friday, October 21, 2011

Identifying the type of shell

If you screen shows a % symbol, it means you are using the C shell or modified C shell (tcsh
). If your prompt contains $you could be using the Bourne shell, the Korn shell, or a variant thereof.

UNIX difference btw :q and :q!

Open a file in vi editor and modify the content of the file.

Now try entering :q

of modern UNIX is the energy of the UNIX programming community; plenty of UNIX users
decide to write a new version of a command in order to solve slightly different problems, thus
spawning many versions of a command.
C
No write since last change (:quit! overrides)


You get the error as above. In this case you should enter :q!

Hence
:q means quit vi editor
:q! quit regardless of whether any changes have occured

UNIX: Searching within the file

What is the command to go to 10th line of the given file?
10G

Will 10g works?
No. Remember in UNIX case sensitive matters.
Then what happens if you enter G alone?
The cursor will go to last line of your file.

Search for a pattern in the file
Enter '/', will take you to the end of the file and then provide the patter to search...

primary players. Each is a little different from the other.) Another contributor to the sprawl
of modern UNIX is the energy of the UNIX programming community; plenty of UNIX users
decide to write a new version of a command in order to solve slightly different problems, thus
spawning many versions of a command.
C
/



primary players. Each is a little different from the other.) Another contributor to the sprawl
of modern UNIX is the energy of the UNIX programming community; plenty of UNIX users
decide to write a new version of a command in order to solve slightly different problems, thus
spawning many versions of a command.
C
/decide



primary players. Each is a little different from the other.) Another contributor to the sprawl
of modern UNIX is the energy of the UNIX programming community; plenty of UNIX users
decide to write a new version of a command in order to solve slightly different problems, thus
spawning many versions of a command.
C
/decide



To find next occurence press "n"


Here also the case matters. If you type "DECIDE" after the search command "/" UNIX will look for "DECIDE" not "decide"


What if I need to search backward
ans:
use the command ?
primary players. Each is a little different from the other.) Another contributor to the sprawl
of modern UNIX is the energy of the UNIX programming community; plenty of UNIX users
decide to write a new version of a command in order to solve slightly different problems, thus
spawning many versions of a command.
C
?decide



Now when you enter command "n" it will search backward!!

****
Hope this is helpful. Thanks Phoenix...

Thursday, October 20, 2011

Vi Editor

When we need to add some more lines to existing file we can use "cat" commnad, if we need to replace some string we have "sed"

But when we need to create a new file or edit already existing files we have to go for screen oriented editors.
There are different types of editors vi, ed, ex and emacs of which vi is the most choosen one. vi and emacs use the entire screen to edit.

Different modes of vi:
vi editor has two different modes, hence it is also called "modaleditor". The two different modes are "insert" and "command" mode. The way UNIX interprets your input depends on which mode we are. For example when we are in commnad mode and enter "dd", it will delete one single line, whereas if we are in edit mode entering "dd" will add "dd" to the file. So whenever you are in doubt in which mode you are, just keep on pressin escape button which will take you to command mode.


Diffrence beween vi and emacs:
emacs is a modeless editor. ie whenver you enter any key it adds to the file. Command in emac is entered by holding the control key while entering the command. For example ctrl+c will delete a character.

Cursor Moverments:

In keyboard the letters H,J,K,L comes in a series.
To move the curson left in a file -H
Right - L
Down - J
Up -K

You can move to the begining of next line by pressing the return key.
Move the cursor to the begining of line - ^,0 (zero)
Move the cursor to the end of line - $
Move forward word by word - w
Move backward word by word- b
Move forward to mid of the text in screen - ctrl+d
Move backward mid of the text - ctrl+u
Page down - ctrl+f
Page up -ctrl+b

vi insert modes:
There are basically four different ways you can get into vi insert mode.
i - insert text into file
a - append text into file
o - Open up a line below current line
O - Open up a line above current line (capital o)

Difference btw i and a for edit mode:
at present the cursor is on a, and now I am going to enter edit mode by entering "i"
On entring "i" the curson doesnt move and it will be there in the same location as above and now I am entering "cursor not moved" and you can see that the text entered to the left of "a" with cursor is not moved at all from "a" and then  press escape, you will find cursor move one space back.

On entering "a" to get into the edit mode, vi behaves differently.
Assume the cursor position is on "a" same as above and press "a" to move to edit mode, you can see that the cursor moves one place forward to "l" and whatever we enter is appended after the letter "l" and press escape, you see cursor moves one space back.

Deleting text with "vi" editor

While in command mode there are few commands provided by UNIX to delete the text from the file.

<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.
do it, too.
UNIX, by contrast, is much more like a spoken language, with
command options (which you learn about later in this lesson)
more complex commands acting akin to sentences. How you do a specific task can, therefore,
(pronounce that .system five release four. or, to sound like an ace, .ess-vee-are-four.), and
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>

Try entering "dd" which deletes an entire line and shift the lines up
<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.
UNIX, by contrast, is much more like a spoken language, with
command options (which you learn about later in this lesson)
more complex commands acting akin to sentences. How you do a specific task can, therefore,
(pronounce that .system five release four. or, to sound like an ace, .ess-vee-are-four.), and
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>

"2dd" - will delete two lines
<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.
more complex commands acting akin to sentences. How you do a specific task can, therefore,
(pronounce that .system five release four. or, to sound like an ace, .ess-vee-are-four.), and
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>

 "D" - Delete the line to the right of the cursor
<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.
_

(pronounce that .system five release four. or, to sound like an ace, .ess-vee-are-four.), and
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>


<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.

pronounce that .system five release four. or, to sound like an ace, .ess-vee-are-four.), and
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>

Here after entering "D", we have
<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.

pronounce that .system_
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>


Now let us tweek a little of the "d" commnad
when "w" (word) is used with "d", dw this delete word by word


What is the command in Unix equivalent to delete and Backspace that we use in windows?
x - Deletes the text as like how we use the "delete" button in windows
X [<-] - Deletes the text as like when we use the backspace in windows.


What if you need to undo the delete?
You can undo only the last action using "u"

Example Scenario:
<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.

pronounce that .system five release four. or, to sound like an ace, .ess-vee-are-four.), and
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>

type dw

<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.

pronounce that .system release four. or, to sound like an ace, .ess-vee-are-four.), and
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>


type "D"
<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.

pronounce that .system_
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>


type "u"
<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.

pronounce that .system release four. or, to sound like an ace, .ess-vee-are-four.), and
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>


type "U"

<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.

pronounce that .system five release four. or, to sound like an ace, .ess-vee-are-four.), and
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>


You can see that entire line is replaced. This can be achieved only if you hadn't moved from the line which you are editing.

Change the case of the letter
Use the command tild (~) or "delete" button in your keyboard.

Tuesday, October 18, 2011

Power Filters and File redirection

Two powerful commands that we learn in this section:
awk:helps you get specific columns of information, modify text, swap the order of column of information in a file
tee: enable you to save a copy of the data being transmitted through a pipeline

awk
Syntax: awk '{commands}'
flags:
-f file  specifies that the instruction should be read from the file rather than from command line
-Fc indicates that the program should consider the letter c as the separator between fields of

using print with awk
bash-3.00$ whobuild      pts/2        Oct 17 11:28    (10.222.46.189)
root       pts/10       Jul 12 18:45    (10.209.188.122)
scholm  pts/12       Oct 18 23:46    (10.203.54.223)
jmadh  pts/15       Oct 19 11:06    (10.203.54.225)


bash-3.00$ who | awk '{print}'
build      pts/2        Oct 17 11:28    (10.222.46.189)
root       pts/10       Jul 12 18:45    (10.209.188.122)
scholm  pts/12       Oct 18 23:46    (10.203.54.223)
jmadh  pts/15       Oct 19 11:06    (10.203.54.225)


bash-3.00$ who | awk '{print$1}'
build
root
scholm
jmadh

bash-3.00$ who | awk '{print$2}'pts/2
pts/10
pts/12
pts/15

bash-3.00$ who | awk '{print$3}'Oct
Jul
Oct
Oct
Here what you have seen is each column is considered as field and each field is represented by $1, $2, $3 etc...for field one, field two, field three respectively.

Much better customization:
bash-3.00$  who | awk '{ print "User " $1 " is on terminal line " $2 }'User build     is on terminal line pts/2
User root     is on terminal line pts/10
User scholm is on terminal line pts/12
User jmadh is on terminal line pts/15
Note: You need to be careful to place the strings to be printed in double quotes, else throw error as below, due to conflict with single quotes of the awk command.

bash-3.00$  who | awk '{ print 'User' $1 ' is on terminal line ' $2 }'awk: syntax error near line 1
awk: illegal statement near line 1


NF Variable:
NF indicates number of fields in a line.
When we use this variable without a $ sign it give us number of fields in each line

bash-3.00$ who | awk '{print NF}'6
6
6
6

When used with $ it displays the value at last field of each line
bash-3.00$ who | awk '{print $NF}'(10.222.46.189)
(10.209.188.122)
(10.203.54.223)
(10.203.54.225)

similarly NR stands for number of records.

Sum up the file size with the awk command:
bash-3.00$ ls -ltotal 80
-rw-r--r--   1 fwire    fwire         12 Oct 18 22:40 MyWords.txt
-rw-r--r--   1 fwire    fwire       2958 Oct 19 00:02 def.txt
-rw-r--r--   1 fwire    fwire       2976 Oct 19 00:01 def_new.txt
-rwxr-xr-x   1 fwire    fwire        141 Oct 18 23:40 search
-rw-r--r--   1 fwire    fwire       2958 Oct 18 21:57 what is unix.txt

file size is the fifthe field
bash-3.00$ ls -l | awk '{print $5}'
12
2958
2976
141
2958

Now we need to sum up all these size

bash-3.00$ ls -l | awk '{total=total+$5;print total}'0
12
2970
5946
6087
9045

To get the summed up vlaue alone use command below
bash-3.00$ ls -l | awk '{total+=$5;print total}'| tail -19045

Much better way is using END command
bash-3.00$  ls -l | awk '{total+=$5} END {print total}'9045
It will be better if we can put this code into some file so that our command line will be less lengthier
% cat << EOF > script
{ totalsize += $4 } END { print “You have a total of “totalsize .”}
EOF
The above command will create a file called script in your current directory with belwo content
{ totalsize += $4 } END { print “You have a total of “totalsize .”}

bash-3.00$ ls -l | awk -f scriptYou have a size of 9150
In above command -f tells awk command to look into the file script

 
We can use awk commnad for conditional execution of statement as well, as shown in the example below
% awk -F: ‘{ if (length($1) == 2) print $0 }’ /etc/passwd | wc -l

Here the passwd file is given as input for the awk command, where it says the field seperator is ':' and if the length of first field, ic account is two character lenght, then print the entire lines from the passwd file and this output  is then supplied to wc where we added -l flag, which gives us the total number of lines.

Say we recived an output of 5, this indicates that there are totally 5 accounts which are two character length.
 
tee  command has only one option, -a, which appends the stream data/ output to a specified file.
bash-3.00$ who | tee who.outfwire      pts/10       Jul 12 18:45    (10.209.188.122)
fwire      pts/12       Oct 18 23:46    (10.203.54.223)
fwire      pts/15       Oct 19 14:33    (10.203.54.224)


bash-3.00$ cat who.outfwire      pts/10       Jul 12 18:45    (10.209.188.122)
fwire      pts/12       Oct 18 23:46    (10.203.54.223)
fwire      pts/15       Oct 19 14:33    (10.203.54.224)


Here who | tee who.out placed the output of who to who.out file, which can also be achieved by the command who > who.out. Hence, what is the difference between these two.

In the first type of command the primary thing is we are displaying who are all there in the system and in the background the contents are saved into a specified file, here who.out (you can see the output in the screen for first type of command whereas you cant see for the second type). In the second type of command, the action is writing the output to  the specified file but not been displayed in the screen.


 





tee command:
information, rather than the default of white space