Sunday, August 7, 2011

Perl Program of the Day August 7th 2010

See this Place http://linux-forum-karthik.blogspot.com/ everyday to learn a perl program with explanation line by line

Today we will learn to write a simple program to substitute a word with another word in Perl

Here i am trying to sustitute the word "hello" with "Hai" in a line.

1)#!/usr/bin/perl
2)$line="hello great wonderful awesome";
3)$line =~ s/hello/hai/;
4)print $line;

Explanation:
1st line:First line starting with "#!" is important as it says the path of the interpreter it depends on the way the perl was installed on your computer.It is very necesary if you don't end the file name with ".pl"
2nd line:$line is a scalar as it starts with "$".So i am trying to store the line in the
scalar.
3nd line:Here is the main part of the program.
when you want to substitute anything in a scalar.You have to specify that scalar to the left side.Here i want to substitute in the scalar $line so i have specified it to the left.
Next part is "=~" which has to be used when you match or substitute in Perl.
s/hello/hai/
Here "s" specifies substitution
And the word to be replaced has to be given first followed by word to be replaced separated by "/".
By this line the word hello gets replaced by hai.
4nd line:It is just simply to print the line.

Hope this gives a glance of one of the powerful abilities of Perl.

Friday, August 5, 2011

copy and move in perl



use File::Copy;
copy("file1","file2") or die "Copy failed: $!";
copy("Copy.pm",\*STDOUT);
move("/dev1/fileA","/dev2/fileB");




This was refered from http://perldoc.perl.org/File/Copy.html

Thursday, August 4, 2011

Create a Unique list using perl

Consider the following example:


@a=('one','Two','Two','Four','Three','Four','Four','Five');
%seen = (); @uniqu = grep { ! $seen{$_} ++ } @a;
print "@uniqu";

Ouput:

G:\perl_programs>perl perl_grep.pl
one Two Four Three Five

Analysis:
In this case the grep returns $_ when the value is 1.Here since "!" option is used
it reverses the condition so it is 1 when the $seen{$_} value is 0 which happens when the
item comes the first time.

So it will be much more clearer when you use map in place of grep which returns the return value.


@a=('one','Two','Two','Four','Three','Four','Four','Five');
%seen = (); @uniqu = map{ ! $seen{$_} ++ } @a;
print "@uniqu";

Output:

G:\perl_programs>perl perl_grep.pl
1 1 1 1 1
G:\perl_programs>
SO as you may see 1 is returned only for unique values only when it comes first time.

To find the no of lines in the file perl

To find the no of lines in a file a one line command in Perl is sufficient

perl -ne "$. -- if /$^/;END {print $.}" filename

will display the no of lines in the file excluding the blank lines .

Analysis:
"$."  means the current record of the file

"$. --  if /$^/   means the one count will be reduced when it comes across the blank line.

END {print $.} means to print the "$." once it is completed with the file.






Wednesday, August 3, 2011

find command to exclude filename and case sensitivity


For the find command to exclude filenames in Linux use "-not" option or "!" option

For example:
find . -name "*".pl  -not -name "*".sh -type f

will list all the files ending with ".pl" and exlude all the files ending with ".sh"

In Linux we can also use -iname option if we want to exclude the case sensitivity for the searches.In that
case if we use
find . -iname "*".pl  -not -iname "*".sh -type f
will list all the files ending with ".pl" ,".PL"... and exlude all the files ending with ".sh",".SH" ....



In solaris "-not option will not work and so we should use only "!" option.

For example:
find . -name "*".pl ! -name "*".sh -type f
will list all the files ending with ".pl" and exlude all the files ending with ".sh"

In solaris -iname option also cannot be used.


Tuesday, August 2, 2011

Useful find commands

 

find . -mtime 0   # find files modified between now and 1 day ago
                  # (i.e., within the past 24 hours)
find . -mtime -1  # find files modified less than 1 day ago
                  # (i.e., within the past 24 hours, as before)
find . -mtime 1   #
# (i.e., within the past 24 hours)
find . -mtime -1  # find files modified less than 1 day ago
                  # (i.e., within the past 24 hours, as before)
find . -mtime 1   # find files modified between 24 and 48 hours ago
find . -mtime +1  # find files modified more than 48 hours ago

find . -mmin +5 -mmin -10 # find files modified between
# 6 and 9 minutes ago
 find files modified between 24 and 48 hours ago
find . -mtime +1  # find files modified more than 48 hours ago
find / -mmin -10  # files modified less than 10 minutes ago
find . -perm -o=w # to find files with certain permissions.

This was taken from http://content.hccfl.edu/pollock/unix/findcmd.htmween
                          # 6 and 9 minutes ago

xargs vs exec in find command


If you analyse the both will do the same functionality
xargs executes the command once and puts the arguments all together, whereas -exec executes the command multiple times, once per each file.

find . -type f | xargs grep "pattern"


find . -type f -exec grep pattern {}


But in the case of exec it will create a separate process for each file returned by the find.So the xargs is said to be more efficient when you are going to do for large
file as it will save time and process memory.

This example may also make you understand better
For example if you want to search a pattern in the first line of the file returned by find command.
For exec it has to be used like this:
find . -type f -perm -700 -exec awk 'NR == 1 && /perl/ {print FILENAME}' {} \;
In exec we can use NR since for each file the awk command will be efxecuted and the NR will point the first line of the file.

For xargs used like this:
find . -type f -perm -700 | xargs awk 'FNR == 1 && /perl/ {print FILENAME}'
In xargs case the find command will be executed first and then for each argument awk will be executed so NR will point to the first line only for the first file and for the second file and so on it will continue to point to the first file.So in this FNR has to be used denoting the first line in each file.

This example was referred from
http://www.linuxquestions.org/questions/showthread.php?s=71bbab884c811443dde9e2f8131bb818&p=4431861#post4431861