Use simply
Here i have assumed the backup files to end with .bak this wildcard can be changed as per your requirement
in the perl script and execute it will delete all the files ending up .bak in the current directory
you can also execute from the command line as follows:
G:\perl_programs>perl -e "unlink(<*.bak>);"
Perl supports globbing which helps you to find files with wildcard characters.
To print all the files starting with "f" in the current directory use the script below
Perl script:
@array=; # this can be modified to your need
print "@array\n";
OUTPUT:
G:\perl_programs>perl perl_globbing.pl
function_perl.pl function_sendarray.pl
This concept can be also used to delete all the backup files ending with .bak in the current directory using the following lines:
root> cat data
while(<STDIN>)
{
print();
}
while(<>)
{
print();
}
root>
Content of the Perl File: Perl file:
root> cat kk.pl
while(<>)
{
print();
}
Execution of the perl file:
root> perl kk.pl < data
while(<STDIN>)
{
print();
}
while(<>)
{
print();}
root>
content of hello.data file before insertion:
acer@ubuntu:/tmp$ cat hello.data
hello
how are you
wonderful
great awesome
May be might be
Script file:
acer@ubuntu:/tmp$ cat dd.sh
#!/bin/bash
sed -i '5ikarthik' /tmp/hello.data
acer@ubuntu:/tmp$ ./dd.sh
Content of hello.data file after insertion:
acer@ubuntu:/tmp$ cat hello.data
hello
how are you
wonderful
great awesome
karthik
May be might be
acer@ubuntu:/tmp$
content of hello.dat file:
acer@ubuntu:/tmp$ cat hello.data
hello
how are you
wonderful
great awesome
May be might be
acer@ubuntu:/tmp$
content of script file:
acer@ubuntu:/tmp$ cat hh.sh
#!/bin/bash
while read line ;do
echo $line ;
done < /tmp/hello.data
acer@ubuntu:/tmp$
OUTPUT:
acer@ubuntu:/tmp$ ./hh.sh
hello
how are you
wonderful
great awesome
May be might be
content of perl_case.pl:
$name="HhEesssSSSSS";
#converting into uppercase
$capital=uc($name);
print "the name in uppercase is $capital.\n";
$lowercase=lc($name);
print "the name in lowercase is $lowercase.\n";
OUTPUT:
G:\PERL_P~1>perl perl_case.pl
the name in uppercase is HHEESSSSSSSS.
the name in lowercase is hheessssssss.
G:\PERL_P~1>
perl_fileout.pl:
open(FILE,">>hello_out.dat");
print FILE "hello howw are you";
print FILE "wonderful";
print FILE "awesome";
print FILE "great";
close(FILE);
G:\perl_programs>perl perl_fileout.pl
G:\perl_programs>
When you run this program hell_out.dat is created in the current directory with the following content:
hello howw are youwonderfulawesomegreat
the following program for making lines come in a new line:
open(FILE,">>hello_out.dat");
print FILE "hello howw are you"."\n";
print FILE "wonderful"."\n";
print FILE "awesome"."\n";
print FILE "great"."\n";
close(FILE);