Tuesday, March 1, 2011

RegEX_Tutorial_Part_1

Hi geeks,

Today we are going to start with the RegEX features in UNIX-LIKE operating systems. Basically RegEX always POSIX compatible. Almost all programming languages supports the RegEX features. Here we start with how to manipulate the texts through BASH SHELL by using some basic commands like grep,sed,awk with RegEX.

For following illustration i am going to use the file , which contains from 1 to 1000.

For creating the file from 1 to 1000 give the following command to your terminal.

seq 1000 > thousand.txt

it will create the file which contains 1 to 1000.

1.Getting the output from based on the first string.

grep '^9' thousand.txt

The symbol "^" enables you to getting the lines which contains 9 as a first letter.


2.Getting the output from based on the last string.

grep '9$' thousand.txt

The symbol "$" enables you to getting the lines which contains 9 as a last letter.

NOTE: The "$" symbol should be placed after the string which you want to find it out.

3.Getting the output by using the neta-character (.) .

For example you want to iterate the computer that you want to manipulate the strings by any match but not at the first field. The first field always should be 9. And after 9 two string may any numbers. For that the following command will help you to search.

grep '^9..' thousand.txt

it will give you all numbers which contains 9 as the first letter and 2-nd and 3-rd numbers with any match.

grep '^9.9' thousand.txt

It will give you the numbers which contains 9 as the first number and also the last number as 9 and middle string with any match.


Likewise you can manipulate your files by inter-changing the RegEX notations. The above prototypes only will help you guys to understand what it will do while giving these input to your computer. But to manipulate the different files with different RegEX ,we have to apply rational thoughts. As this is my first article in blogs I could not able to draft it clearly. Soon I will come with new enhanced topic about RegEx with more practical explanations.

No comments:

Post a Comment