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


word boundary in grep shell

In the case of the perl we use "\b" to specify the word boundary.i.e. in the case of perl
if we want to match a word hello we can use /\bhello\b/ which matches word hello as a separate word or when it occurs as the first word without being combined with someother word.

In Shell if we want to match a word in a word boundary using grep we can use "-w" option of grep which matches the word in a word context.
Using
grep -w "hello" filename 

is same as

grep "\<hello\>" filename

\<  - starts a word boundary.
\>  - ends a word boundary.

grep "\<hello" filename will search for hello,hellobuddy,helloname everyname starting with hello but not Maahello since it starts with a word boundary.

Sunday, July 31, 2011

Extensions in perl

Extensions significantly add to the power of regular expressions instead of adding lot of metacharacters.

Commonly used extensions in perl are:
1)(?#TEXT)    - it is used for adding comments in the regular expressions.

2)(?:...)       - it is used in case when you want paranthesis to be used without saving
the matched pattern in $n

3)(?=...)    -  it is used for matching without using the specific value in the $& variable.

4)(?!...)  - it is used when you don't want the pattern in the paranthesis to follow the word before or after.

Example:
1)(?#TEXT)-
it can be used simply used it just adds a clarity to the people viewing this pattern.
$dd =~ m/\w(?#it matches a single character)\s(it matches a single character)/;

2)(?:.....)-


$dd="hello how are you";
$dd=~ m/(?:\w*)/;
print $1;

if you see the example above the $1 is supposed to match hello since "?:" is specified in parantheses it is empty.

3)(?=...)-
it can be used in the case when you want to match a pattern based on the value in the paranthesis but it should not get included in $& variable in that case this will be useful.

$dd="hello how are you";
$dd=~ m/\w*\s+(?=how)/;
print $&;

Output:
hello

In the example specified i am trying to match a word followed by "how".If i have
parathesis without "?=" the output will be "hello how".Using "?=" has done the trick to print only hello.

4)(?!....)
It is very useful extension which can be used in case where you donot want a word to follow or precede the match you are looking for.
For example: m/red(?!carpet)/ will match redsea or redfly but not redcarpet.
The detailed example can be seen in the url:
http://linux-forum-karthik.blogspot.com/2011/07/to-match-pattern-not-having-word.html



Saturday, July 30, 2011

map vs grep in perl


grep function in perl:
grep function is normally used to select the matched pattern and return it to the array

grep(EXPRESSION,@array);
grep(BLOCK,@array);

Perl file:

$"="\n";
$\="\n";
@array=(0..3,"karthik");
print "@array";
@result=grep($_=~ s/0/9/,@array);
print "array:\n@array";
print "result:\n@result";

output:

G:\perl_programs>perl perl_map.pl
0
1
2
3
karthik
array:
9
1
2
3
karthik
result:
9

G:\perl_programs>

Analysis:
In this case if you see the grep function tries to substitute 0 with 9 by moving through each element of the array.When it finds a element with value 0 it
modifies the original array and returns the replaced value to the result array.
So as you may see from the output the result array has only one value "9".

Map function in perl:
Map function is used to evaluate the given expression and return t
he result value to the array.


$"="\n";
$\="\n";
@array=(0..3,"karthik");
print "@array";
@result=map($_=~ s/0/9/,@array);
print "array:\n@array";
print "result:\n@result";


Output:

G:\perl_programs>perl perl_map.pl
0
1
2
3
karthik
array:
9
1
2
3
karthik
result:
1






G:\perl_programs>

Analysis:
In this case if you see the map function tries to substitute 0 with 9 by moving through each element of the array similar as in grep.But When it does not 
find the match it returns empty string to the result array and when it finds the match it modifies the original array with the substituted value and returns the "1" to the result array which is the return value for pass or fail for the substitution.
So as you may see from the output the result array has only one value "1" followed
by space for the values which did not match.

finding the count of a certain character in a line perl


Perl Program:

$_="great,awesome.wonderful  wonder";
$dd=tr/w//;
print "count of character w is :$dd";

Output:

G:\perl_programs>perl perl_tr.pl
count of character w is :3

G:\perl_programs>
Analysis:
Here i have used tr operator to find the count of character 'w'.tr operator is normally used to translate the characters.But in this case i have translatted 'w' into
nothing as per the pattern "tr/w//".So it just returns the no of character matched in the line.