Introduction
This Script can be used to perform many operations on a file. This Script first searches the file. Filtering can be done for the searching file. Searching for file can be done either on extension or any keyword present in the name of the file. You have to just assign proper value to variable $FileFilter defined in the script. Like if you want to look for the files with ‘.txt’ extensions then assigning will be done in following manner, $FileFilter = ‘\.txt’. ‘\’ is put before ‘.’ to escape the special meaning of it. If you want to look for the files containing word ‘Work’ in their name then assigning will be done in following manner , $FileFilter = ‘Work’.
Searching is done recursively in all the sub directories of the current directory. After the file is found operation mentioned in the script will be carried out on the file. Various operations can be performed using this script. I have written command for some of them, which are explained below:
1. Searching of a word and replacing that word with another word in files present in a particular directory or sub directory.
Suppose you want to replace word “table” with “chair” in all the .txt files present in the current directory or its sub directory.
Steps to be followed:
a) a) Copy the below script and save it in a file with .pl as extension i.e. search_replace.pl
b) b) Assign search keyword value ‘table’ to the variable $Search_pattern defined in the script. I.e. $Search_pattern = ‘table’;
c) c) Assign replace keyword value ‘chair’ to the variable $ Replace_pattern defined in the script. I.e. $Replace_pattern = ‘chair’;
d) d) Set file-filtering condition. Assign ‘.txt’ value to the variable $FileFilter defined in the script. I.e. $FileFilter = ‘\.txt’. ‘\’ is put before ‘.’ to escape the special meaning of it.
e) e) Uncomment the appropriate command. I.e. below line in the script
$command = “sed s/’$Search_pattern’/'$Replace_pattern’/g $THISDIR[$i]/$sqlfile> $THISDIR[$i]/$sqlfile.bak \n mv $THISDIR[$i]/$sqlfile.bak $THISDIR[$i]/$sqlfile”;
f) f) Execute the script. Format for execution – perl <script file name> i.e. perl search_replace.pl
2. Listing of all the files in a particular directory or sub directory in which a particular key word is present.
Suppose you want to list all the ‘.txt’ files present in the current directory or its sub directory containing the word ‘table’.
Steps to be followed:
a) a) Copy the below script and save it in a file with .pl as extension i.e. list_file.pl
b) b) Assign search word value ‘table’ to the variable $Search_pattern defined in the script. I.e. $Search_pattern = ‘table’;
c) c) Set file-filtering condition. Assign ‘.txt’ value to the variable $FileFilter defined in the script. I.e. $FileFilter = ‘\.txt’. ‘\’ is put before ‘.’ to escape the special meaning of it.
d) d) Uncomment the command. I.e. below line in the script
$command = “grep –l ‘$Search_pattern’ $THISDIR[$i]/$sqlfile”;
e) e) Execute the script. Format for execution – perl <script file name> i.e. perl list_file.pl
3. Uploading of all the *.sql or *.tab files in a database.
Suppose you want to upload all the ‘.sql’ files present in the current directory or its sub directory in database. We generally face this type of problem while migration.
Steps to be followed:
a) a) Copy the below script and save it in a file with .pl as extension i.e. upload_file.pl
b) b) Uncomment the below lines in the script.
use Sybase::DBlib;
$ENV{’USER’} = “dbaadmin”;
$ENV{’PASSWORD’} = “*****”;
$ENV{’SERVER’} = “lohit”;
$ENV{’DATABASE’} = “gsquality”;
c) c) Assign User name, Password, Server name and Database name to the variables $ENV{’USER’} , $ENV{’PASSWORD’}, $ENV{’SERVER’} and $ENV{’DATABASE’}. defined in the script. Database name is required if you are using ‘bsql’ command.
d) d) Set file-filtering condition. Assign ‘.sql’ value to the variable $FileFilter defined in the script. I.e. $FileFilter = ‘\.sql’. ‘\’ is put before ‘.’ to escape the special meaning of it.
e) e) Uncomment the command. I.e. below line in the script
For ‘isql’ command
$command = “isql -U$ENV{’USER’} -P$ENV{’PASSWORD’} -S$ENV{’SERVER’} < $THISDIR[$i]/$sqlfile”;
For ‘bsql’ command
$command = “bsql -U$ENV{’USER’} -P$ENV{’PASSWORD’} -S$ENV{’SERVER’} -D$ENV{’DATABASE’} < $THISDIR[$i]/$sqlfile”;
f) f) Execute the script. Format for execution – perl <script file name> i.e. perl upload_file.pl
4. Searching of a keyword in all *.tab or all files present in a particular directory or sub directory.
Suppose you want to search keyword “table” in all the .tab files present in the current directory or its sub directory.
Steps to be followed:
a) a) Copy the below script and save it in a file with .pl as extension i.e. search_file.pl
b) b) Assign search keyword value ‘table’ to the variable $Search_pattern defined in the script. I.e. $Search_pattern = ‘table’;
c) c) Set file-filtering condition. Assign ‘.tab’ value to the variable $FileFilter defined in the script. I.e. $FileFilter = ‘\.tab’. ‘\’ is put before ‘.’ to escape the special meaning of it.
d) d) Uncomment the command. I.e. below line in the script
$command = “grep ‘$Search_pattern’ $THISDIR[$i]/$sqlfile”;
e) e) Execute the script. Format for execution – perl <script file name> i.e. perl search_file.pl
This Script can be used for other batch operations also.
Limitations
1. 1. It can run on Unix.
2. 2. Perl has to be loaded on the System.
Script
#!/opt/local/bin/perl5
#use Sybase::DBlib;
#Change the parameters and uncomment for connecting to Database,
#if database connection is required
# $ENV{’USER’} = “dbaadmin”;
# $ENV{’PASSWORD’} = “*****”;
# $ENV{’SERVER’} = “lohit”;
# $ENV{’DATABASE’} = “gsquality”;
# For Searching Write the pattern here
$Search_pattern = ‘tableshiv’;
# For Searching Write the pattern here
$Replace_pattern = ‘tableshiv1234′;
# Set the file filtering pattern here
# Put * if you want to search for all files.
$FileFilter = ‘\.bak’;
&MainProg;
sub MainProg
{
my($sqlfile, @THISDIR);
# Filter for the search file. Here it is .tab
$THISDIR[0] = “./”;
$i = 0;
foreach (@THISDIR)
{
# If you want to print the directory, uncomment the below line.
# print “$THISDIR[$i]\n”;
if ( ! opendir (NOW, $THISDIR[$i]))
{
print “Can not open dir $THISDIR[$i]\n” ;
$i = $i+1;
next;
}
@FileList = readdir(NOW);
opendir(DIR, $THISDIR[$i]) || die “can’t opendir $THISDIR[$i]: $!”;
@dots = grep { (!(/^\./)) && -d “$THISDIR[$i]/$_” } readdir(DIR);
closedir DIR;
foreach (@dots)
{
push(@THISDIR, “$THISDIR[$i]/$_”);
}
foreach (@FileList)
{
if (/$FileFilter/) {
$sqlfile=$_;
} else {
next;
}
# Command for operating on the file to be used below.
# Below command is for searching a key word in a file
#$command = “grep ‘$Search_pattern’ $THISDIR[$i]/$sqlfile”;
# Below command is for searching a word and replacing with other word in a file
#$command = “sed s/’$Search_pattern’/'$Replace_pattern’/g $THISDIR[$i]/$sqlfile> $THISDIR[$i]/$sqlfile.bak \n mv $THISDIR[$i]/$sqlfile.bak $THISDIR[$i]/$sqlfile”;
# Lists the file containing the key word.
#$command = “grep -l ‘$Search_pattern’ $THISDIR[$i]/$sqlfile”;
# Below command is for loading a stored procedure in database,
# Depending on whether you are using isql or bsql.
# $command = “isql -U$ENV{’USER’} -P$ENV{’PASSWORD’} -S$ENV{’SERVER’} < $THISDIR[$i]/$sqlfile”;
# $command = “bsql -U$ENV{’USER’} -P$ENV{’PASSWORD’} -S$ENV{’SERVER’} -D$ENV{’DATABASE’} < $THISDIR[$i]/$sqlfile”;
#Uncomment this if you want the command to be printed.
print “$command\n”;
system ($command);
}
closedir NOW;
$i = $i+1;
}
}
Tags: coding, Perl, perl script, Perl Script for performing batch operation on files, perl scripts, technical FAQ