Archive for year 2010
How to do search and replace operation on a portion of a string in perl
Dec 24th
I recommend reading this article to know more about usage of substr function in perl. Basically substr function and a typical search and replace mechanism (s///g) can be combined to achieve the goal in a single statement.
Look at the following script which replaces the word “goes” to “moves” in first first 10 characters of a string variable “$str”.
#!/usr/bin/perl $str="What goes around comes around"; print $str . "\n"; substr($str,0,10) =~ s/goes/moves/g; print $str . "\n";
Output of the above script is as follows:
[neo@techpulp ~]# perl replsubstr.pl What goes around comes around What moves around comes around [neo@techpulp ~]#
You can play with More >
How to get substring in perl – substr function
Dec 23rd
In perl, the substr function can be used to access individual characters or a portion of a string. The syntax of substr function is as follows:
substr ( string, offset [, count] )
Arguments:
- string – string from which you would like to extract a sub-string
- offset – this indicates start of the sub-string you want to extract
- count – length of the sub-string to extract
A negative number also can be specified for “offset” argument that make substr to count the characters in the reverse direction (i.e from the end). If “0″ is specified as offset, sub-string is extracted from beginning of the string. If a positive More >
How to delete a file whose name starts with minus character
Dec 21st
Sometimes an untended paste operation of screen dump results in unwanted files. If a file name starts with a minus (-) character, it is not possible to delete the file using traditional command as anything that starts with a minus (-) character is treated as one of the command line options.
For example, look at the following file “-myfile.txt“.
[sara@techpulp ~]# ls -myfile.txt [sara@techpulp ~]#
Traditional way fails to remove the file.
[sara@techpulp ~]# rm -myfile.txt rm: invalid option -- 'm' Try `rm --help' for more information. [sara@techpulp ~]#
You can use “–” option to stop “rm” command to stop expecting further command More >
How to check if bash variable is a number or not
Dec 20th
The following example illustrates the simplest way to check if a variable is holding an number or not. Consider the following scripts which checks the first command line argument and tells us if it is a number or not.
[neo@techpulp ~]# cat numcheck.sh #!/bin/bash if [ "$1" -eq "$1" >& /dev/null ]; then echo "'$1' is an integer" else echo "'$1' is not an integer" fi [neo@techpulp ~]#
Basic logic behind this is to use the variable in an integer expression and find the result of the operation. Let run the above script with different arguments.
[neo@techpulp ~]# ./numcheck.sh 123 '123' is More >
How to iterate or loop through all elements of an array in a perl script
Dec 18th
Here you can see multiple ways of implementing array iterator.
Example1:
This example reads each element of the array to a variable “$item” in a loop.
@myarray = ('One', 'Two', 'Three');
foreach $item (@myarray) {
print $item;
}
Example2:
This example avoids intermediate variable “$item” shown in the above script. However if you have nested loops, this method is not suitable. Each element is available in a special variable “$_” inside the loop.
@myarray = ('One', 'Two', 'Three');
foreach (@myarray) {
print $_;
}
Example3:
This example is similar to “for” loop style of a C programmer .
@myarray = ('One', 'Two', 'Three');
for($i=0; $i <= $#myarray; More > How to pass and read function or subroutine arguments in a perl script
Dec 17th
All the arguments passed to a perl subroutine are packed in an array “@_”. The following script shows you how to access function arguments in a subroutine.
[nick@techpulp ~]# cat funargs.pl
#!/usr/bin/perl
sub myfun
{
my $i = 0;
$nargs = $#_+1;
print "Number of arguments: $nargs\n";
foreach $arg (@_) {
$i++;
print "Arg $i: $arg\n";
}
}
myfun("One", "Two");
[nick@techpulp ~]#
Let us run the above script.
[nick@techpulp ~]# ./funargs.pl Number of arguments: 2 Arg 1: One Arg 2: Two [nick@techpulp ~]#
If the subroutine is expected to take fixed number of arguments, you can simplify the script as following.
[nick@techpulp ~]# More >
How to read input line by line in a perl script
Dec 16th
The following perl script reads one line at a time from the standard input which can be from normal console or a redirected file using pipes.
[nick@techpulp ~]# cat readline.pl
#!/usr/bin/perl
$line = 0;
while (<>) {
$line++;
print "Line $line: " . $_;
}
[nick@techpulp ~]# chmod +x readline.pl
[nick@techpulp ~]#
In the above script, The “<>” reads a line from standard input file of the script and “$_” contains the last read data.
For input, let us consider following file (input.txt) which contains four lines.
[nick@techpulp ~]# cat input.txt first line second line third line fourth line [nick@techpulp ~]#
Now let More >
How to read or access command line arguments in a perl script
Dec 15th
All command line arguments passed to a perl script are stored in a array named @ARGV. So $ARGV[0] contains first argument, $ARGV[1] contains second argument and so on. As all command line arguments are packed in an array, you can use $#ARGV to get the index of last element. That means ($#ARGV+1) will give you number command line arguments passed to the script.
Look at the following example, that iterates through all command line arguments passed.
[nick@techpulp ~]# cat cmline.pl
#!/usr/bin/perl
$nargs = $#ARGV + 1;
print "you have passed $nargs command line arguments\n";
for($i=0;$i<$nargs;$i++) {
print "Arg $i: $ARGV[$i]\n";
}
[nick@techpulp More > How to reset WordPress adminstrator’s password using phpMyAdmin
Dec 13th
Its is recommended that this method of setting password is used only as last resort. Because there is a standard way of resetting password using “Lost your password?” option by which you get a mail containing newly generated random password.
WordPress saves MD5 hash of plain passwords. For thos who don’t know what is MD5, it is a digest algorithm (cryptography) that generates fixed length digest for a variable length input. So you will not see plain passwords in WordPress database. This feature ensures even the administrator user unable to know the passwords of subscribed users.
Follow the instruction given below to More >


Recent Comments