Tuesday, February 26, 2013

Perl - What is __DATA__ ?

What is __DATA__ ? Why is it used?
    Generally, Perl reads data from a file which in the Perl program can be accessed using open and readline functions. However, for users who just want to test something for an urgent requirement, it is pretty boring to put the test data in a file and access it using the open and readline functions. The solution for this : __DATA__.

   Let us consider a sample text file:
$ cat f1.txt
AIX,1
Solaris,2
Unix,3
Linux,4

Tuesday, February 19, 2013

What is the time command in Linux for?

time command in Unix is used to find the time taken by a particular command to execute. This is very useful when we want to time a particular command or a particular script to know the amount of time it consumes. Not only the elapsed time, the time command also tells the amount of time spent in the user and kernel spaces as well.
$ time

real    0m0.000s
user    0m0.000s
sys     0m0.000s
time command output shows 3 different components:
real - The total time elapsed during the command which includes the time taken for the process.
user - The time taken for the process only in the user space. This does not include kernel calls.
sys - The time taken for the process in system space (for kernel related calls )

Tuesday, February 12, 2013

sed - 10 examples to replace / delete / print lines of CSV file

How to use sed to work with a CSV file? Or How to work with any file in which fields are separated by a delimiter?

Let us consider a sample CSV file with the following content:
cat file
Solaris,25,11
Ubuntu,31,2
Fedora,21,3
LinuxMint,45,4
RedHat,12,5

1. To remove the 1st field or column :
$ sed 's/[^,]*,//' file
25,11
31,2
21,3
45,4
12,5
   This regular expression searches for a sequence of non-comma([^,]*) characters and deletes them which results in the 1st field getting removed.

Wednesday, February 6, 2013

How to delete every nth line in a file in Linux?

How to delete or remove every nth line in a file? The requirement is to remove every 3rd line in the file.

Let us consider a file with the below content.
$ cat file
AIX
Solaris
Unix
Linux
HPUX

1. awk solution :
$ awk 'NR%3' file
AIX
Solaris
Linux
HPUX
   NR%3 will be true for any line number which is not multiple of 3, and hence the line numbers which are multiple's of 3 does not get printed.
Note: NR%3 is same as NR%3!=0

Tuesday, February 5, 2013

Perl - How to split a string into words?

How to split a string into individual words? Let us see in this article how to split a string which has 2 words separated by spaces.

1. Using the split inbuilt function:
my $str="hi  hello";
my ($var1,$var2)=split /\s+/,$str;
   \s+ stands for one or more spaces. By using \s+ as the expression for the split function, it splits everytime it encouters a series of spaces and hence the words are retrieved.

   In case multiple words are present in a string, the result can be collected in an array:
my $str="hi  hello";
my @arr=split /\s+/,$str;