Wednesday 7 December 2016

Common file operations in perl part 2

In the previous article I demonstrated the usage of some common file manipulation operations we can perform in perl using a single perl script. Keeping in line with that in this article The functions I'll use in this article will be glob, sysopen, link, symlink & grep.

Given below is a short description for each of these functions before getting to the actual script:

Glob: This function is used to insert the * wildcard character with file names etc.

Sysopen: We can use the open function for opening files for reading & writting but we might risking clobbering some existing files if we use the open function. The sysopen function allows us to indicate conditions such that the new file will be created only if it does not exist & also allows us to set permissions during file creation.

Link & symlink: These functions are used for creating hard links & soft links respectively.

Grep: This is the perl variant of the grep command available in UNIX/Linux platforms.

So, here's the perl script illustrating the usage of the aforementioned functions.


root@buntu:~# cat file2op.pl
#!/usr/bin/perl -w

use Fcntl;

##testing system and glob functions##

my $test_var = system ("/bin/ls -l /tmp");

my $test_var2 = qx{ls -l /} ;

my $test_glob = system ('ls' ,'-l', glob('/root/test*') ) ;


print qq{$test_var} ;
print "$test_var2" ;
print "$test_glob" ;

##demonstrating sysopen##

#umask 0022;
umask 0000;

sysopen (newfile, "newfile", O_WRONLY|O_CREAT|O_EXCL, 0777) || die "could not create file" ;

print newfile "this is to demonstrate sysopen function usage";


##demonstrating creation of file links##

my $file_name = "testfile" ;
my $soft_link = "testfile_soft" ;
my $hard_link = "testfile_hard" ;

link $file_name, $hard_link ;

symlink $file_name, $soft_link ;


##demonstrating perl grep function usage##

@file_list = `ls -l /root` ;

print grep /test/, @file_list ;

##second example using perl grep##

@art = ("1", "4", "10", "14", "7") ;

@filter =  grep  ($_ > 4 , @art)  ;

print "@filter", "\n" ;


The qq function is analogous to double quotes & the qx{} function is analogous to backticks used for command substitution.

There is one more function that I need to mention before concluding this article & that is the map function. The map function takes a list or an array as its input & transforms that list element by element into a new list or array. This is in a way functioanlly similar to writting a foreach loop iterating through each element of the array & performing some actions on it. The map function operates on the _ variable.

Here is an example of using the map function:

root@buntu:~# cat map.pl
#!/usr/bin/perl

use File::Copy;


open (t1, "afile") || die "could not open file $!";

open (t2, ">> afile.bkp") || die "could not open file $!";

my @array = <t1> ;

my $var = `hostname` ;

my @new = map {$_ =~ s/server/$var/g ; $_ } @array ;

print t2 @new ;

my $f1 = "afile" ;
my $f2 = "afile.bkp" ;

move ("$f2", "$f1") ;


This script opens a file named afile & assigns the corresponding file handle to an array. We then change each occurance of the word server & replace it with the hostname of our system & apply the changes to a new array. We then copy the contents of a new array to a file & finally replace the original with the newly created file.


I hope this has been a good read.

No comments:

Post a Comment

Using capture groups in grep in Linux

Introduction Let me start by saying that this article isn't about capture groups in grep per se. What we are going to do here with gr...