Showing posts with label files. Show all posts
Showing posts with label files. Show all posts

Jul 10, 2013

Perl Remove Special Characters From File

While reading some kind of log files as mentioned below, we need to get rid of these special characters.

Because of these special characters, it makes the Developers job tough :
To parse the content of a file
To convert the special characters from the file

Let us explain how can we get rid of these special characters with a simple example

test.log
^[[1;31mTest 1^[[0m
^[[1;31mTest 2^[[0m
^[[1;31mTest 3^[[0m
^[[1;31mTest 4^[[0m
^[[1;31mTest 5^[[0m  


In the above test.log file, first of all, what is displayed as ^[ is not ^ and [
But it is the ASCII ESC character, produced by Esc or Ctrl[ (the ^ notation means the Ctrl key).

We can use the following regular expression :

s/\e\[[\d;]*[a-zA-Z]//g;

Note: 
\e represents escape character in the above regular expression (substituting instead of ^[ )
We can shorten from [a-zA-Z] to just [mK], based on the requirement
You can make use of the above regular expression while parsing the file as well (line by line)

In case if you want to backup file (test.log.bak) instead of changing in the original file (test.log) then use the following :
perl -pi.bak -e 's/\e\[[\d;]*[a-zA-Z]//g' test.log

The following will remove the special chars in test.log
perl -pi -e 's/\e\[[\d;]*[a-zA-Z]//g' test.log

Output after removing
Test 1
Test 2
Test 3
Test 4
Test 5
  



Mar 23, 2013

Create Log File in Perl


Today we discuss about how to create a log file in Perl, Logging is always part and parcel in developing code.

Here in the below mentioned example : 
1) We are trying to read the lines from the file in the path 'C:\Perl\data.txt'
2) While reading the lines, in case of any errors, we are capturing (means we are writing to log file) to log file 'C:\Perl\log.txt'
3) You can either use write/append mode to log file based on your requirement.
4) Write (">") mode : You write the content to a new log file
5) Append (">>") mode : if Log File already exists, you append the content to the existing file

log_test.pl

#!/usr/bin/perl

use strict;
use warnings;

my $log_file_path = 'C:\Perl\log.txt';
my $read_file_path = 'C:\Perl\data.txt';

#use the ">"  symbol to write to new file (Write mode)
#use the ">>" to append to the file (Append mode)
open (LOG_FILE, ">$log_file_path"); 

open (READ_FILE, "$read_file_path"); 
#"READ_FILE" is File Handle

unless (-e $read_file_path) {
    print LOG_FILE "\n Read File Doesn't Exist!!! : "  . $read_file_path;
    exit;


#use the ">"  symbol to write to new file (Write mode)
#use the ">>" to append to the file (Append mode)
while my $each_line (<READ_FILE>) {
     print "\n Each Line: :" . $each_line;
}

print LOG_FILE "\nTesting Writing into Perl file";
print LOG_FILE "\nWriting more content 111";
print LOG_FILE "\nWriting more content 222";

close (READ_FILE); 
close (LOG_FILE);

O/P:
log.txt (In case file data.txt is present in the path)
Testing Writing into Perl file
Writing more content 111
Writing more content 222

log.txt (In case file data.txt is not present in the path)
Read File Doesn't Exist!!! : log.txt


Write to File in Perl


Now, we discuss about writing content to a file

The following code snippet explains
1) Write (">") mode : You write the content to a new file
2) Append (">>") mode : File already exists, you append the content to the existing file
3) You can either define Write or Append mode based on your requirement
4) Define a file read handle (FILE_WRITE), it's can be any user defined name
5) Write the contents to file

file_write.pl

#!/usr/bin/perl

use strict;
use warnings;

my $filename = 'C:\Perl\data_write.txt';

#use the ">"  symbol to write to new file (Write mode)
#use the ">>" to append to the file (Append mode)
open (FILE_WRITE, ">$filename");
#"FILE_WRITE" is File Handle

print FILE_WRITE "\nTesting Writing into Perl file";
print FILE_WRITE "\nWriting more content 111";
print FILE_WRITE "\nWriting more content 222";
close (FILE_WRITE); 

#use the ">"  symbol to write to new file
#use the ">>" to append to the file


Output: 
data_write.txt
Testing Writing into Perl file
Writing more content 111
Writing more content 222



Reading File in Perl


Let's discuss about reading a file in Perl with an example.

The following code snippet 
1) Checks whether the file (data_read.txt) we are going to read is present or not 
2) Define a file read handle (FILE_READ), it's can be any user defined name
3) Loop through the file handle and read line line from the file
2) Chomp each & every line (to remove the new-line characters (e.g., \n)
4) Print the contents of the file line by line

E.g., 
data_read.txt
Mother Teresa
Nelson Mandela
Abraham Lincoln


file_read.pl

#!/usr/bin/perl

use strict;
use warnings;

my $filename = 'C:\Perl\data_read.txt';

#Check if file is present in the path or not
unless (-e $filename) {
    print "\n File Doesn't Exist!" . $filename;
    exit;


#"FILE_READ" is File Handle
open (FILE_READ, $filename); 

while (my $each_line = <FILE_READ>) {
   
   #"chomp" removes the new lines characters like "\n" at the end of each line 
   chomp $each_line;   
   
   print $each_line . "\n";
}

close (FILE_READ); 



Output:
Mother Teresa
Nelson Mandela
Abraham Lincoln


Apr 26, 2012

Delete files older than X days on Linux


find /path/to/files* -mtime +5 -exec rm {} \;

This is an example, which deletes files older than 5 days from unix command line itself

Note:
1)  Apply this from unix command line itself

Jun 22, 2008

How to get all files in a directory

#!F:\Perl\bin\perl -w
use strict;

opendir(DIR, ".");
my @files = readdir(DIR);
closedir(DIR);
foreach my $file (@files) {
print "$file\n";
}