Perl Tutorial For Begineers

Delete line containing a particular word from a list of files

  1. Store list of all files inside an array which contains a particular word using "grep" UNIX command.
  2. Print list of all those files to check whether it is working properly or not.
  3. Handle all files one by one by using foreach command
  4. Store the word in scalar for which line needs to be deleted
  5. Open file in read mode and put entire line into an array and then close the file.
  6. Again open same file in write mode (This will delete everything from that file).
  7. Print line one by one in that file unless a particular line appears. Ignore that line using unless command. and close the file.

Code

#!usr/bin/perl
use strict;
use warnings;
my @filename = `grep -l \"\\-nt 1\" *`; #Original command for xterm is grep -l "\-nt" * -->> This lists all files containing -nt inside it
print "@filename\n"; #Print all files that are inside filename
foreach my $filename (@filename)
{
my $name = -nt 1; #Line containing this word needs to be deleted
open(FILE, "<$filename") || die "File not found"; #Open with read only mode.
my @lines = <FILE>; #Store all lines in an array line
close(FILE);
open(FILE, ">$filename") || die "File not found"; #Open file in write mode
foreach my $line ( @lines )
{
print FILE $line unless ( $line =~ /$name/ ); #Print all line unless the line containing word assigned in $name above
}
close(FILE);
}

 

Replace word from specific file containing a particular word

Open all file from a directory which contains a particular word like "EVENT" and replace another word like "SWITCH_ENABLE" from all those files. Here we are using UNIX command also to search for a particular file from file. This is much more easier than building a complete logic for that. You can use grep command to search the file name and store them in an array "filename". Refer the below code for example. 

Code

#!usr/bin/perl
use strict;
use warnings;
my @filename = `grep -l "EVENT" * `; #Original command for xterm is grep -l "\-nt" * -->> This lists all files containing -nt inside it
print "@filename\n"; #Print all files that are inside filename
foreach my $filename (@filename)
{
open(FILE, "<$filename") || die "File not found"; #Open file in read mode
my @lines = <FILE>;
close(FILE);
foreach(@lines)
{
$_ =~ s/SWITCH_ENABLED=0/ /g; #Replace "SWITCH_ENABLED=0" with one space
}
open(FILE, ">$filename") || die "File not found"; #Open file in write mode
print FILE @lines;
close(FILE);
}

Summary of Complete File Handling in Perl

In this code you will learn how to open specific type of file and do some operation inside that. To understand this properly you need to concentrate on the below steps.

  1. Finding a specific type of file and storing all those files in an array.
  2. Inside that file we need to search for line which contains a specific word.
  3. Split that line in 3 or 4 parts to find the line number, signal name and location of another file.
  4. Use different scalar and array to store these line number and file location.
  5. Store some signal which matches with patterns specified by you inside a scalar.
  6. If signal name matches with that pattern then don't do anything otherwise replace a particular word with space or anything else.
  7. Just let me know if you have any doubt in understanding this concept.

Code

#!usr/bin/perl
use strict;
use warnings;
my @filelist = `find $ENV{'GIT_DIRECTORY'}/abc/xyz -name "*lastname.txt*" `; # An array of all these filename
foreach my $filelist (@filelist) # Do this for all files one by one

open(FH, "<$filelist") or die $!;
while(<FH>)
{
if (/SEARCH_WORD/)
{
#print "$_\n"; #Just to see that all line containing EVENT is printed
my @abc = split(' ', $_); # Split entire line with space
my @def = split('abc23', $abc[5]);
my $signal = $abc[12];
my @filelist1;
push @filelist1, $def[1]; #abc[5] is the location of path inside central root, def will split the location with ww34b
my $output = `grep \"$signal.*synchronize\" -r /location_where_to_search `;
if (/$abc[12].*synchronize/, $output)
{
print "$abc[12].synchronize\n";
}
else
{
open(FL, "<", "/half_location/@filelist1") or die $!;
my @line = <FL>;
print ($abc[15]-1);
print $line[$abc[15]-1];
my $find= "EVENT";
my $replace = "";
$line[$abc[15]-1] =~ s/$find/$replace/;
print $line[$abc[15]-1];
close (FL);
#open(FT, ">", "/half_location/@filelist1") or die $!;
#seek (FL, 0, 0);
#print FT @line;
#truncate (FL,tell(FL));
close (FT);
}
}
}
close (FH);
}
print "Done. Thank You\n";
exit;