Perl programming

I am back again.

I just been thinking about a programming language that could be useful for a network guy…. and  finally I´m with Perl . I don´t know if it is the best one or not… but it remind me my ages when I was studing. This language is quite similar to C.

The purpose of the script is modify a given file. I did it because at work  I was doing a repetitive task so many times and I decided to use the scripting. Because we are doing more and more repetitive task, I will keep my perl skills up to date. Then, let´s read a file with Perl 😉

#!/usr/bin/perl -w

use strict;

use warnings;
# read initial file to modify

my $file=”file.txt”;

# final file

my $final =”final.txt”;

#text or characters deleted

my $bin=”bin.txt”;

my $line;

my $i=0;

#check if files can be created or oppened

open (FILE,”<$file”) || die “ERROR: File $file not found\n”;

open (FINAL,”>$final”) || die “ERROR: File $final not found\n”;

open (BIN,”>$bin”) || die “ERROR: File $bin not found\n”;

#read the file

while ($line=<FILE>){

if ($line =~ /COMMENT/) {   # if line contains the chain “COMMENT”, send the line to the bin file.

print BIN $line;

}

else{

if ($line =~ /\}/) {     # if line contains the chain “}”, send the line to the bin file.

print BIN $line;

}

else{

if ($line =~ /;$/){   # if line is ending in “;”, send the line to the bin file.   $ is indicating that is the end of the line. It is not considering the \n

if ($line =~ /CHAIN/) {  # if line contains the chain “CHAIN”,

if ($i== 0){  # I added this counter for formating purposes

print FINAL “\n——\n”;

$i = $i + 1;

}

else{$i = 0;} # I set to 0 because the above “CHAIN” is twice on the file and I only need to add the lines(for formating purposes) only once.

}

print FINAL substr($line, 1,-2).”\n”;  # with substr function I am taking the whole line except untill the last 2 possitions. Last one is “\n” and previous one was “;”

}

}

}

}

# Closing files

close (FILE);

close (FINAL);

close (BIN);

 

An important function in the script is substr. You can use it like this:

print substr($line, 1,-2);

We are stracting a string from another one. In our case, we are reading the line ($line), taking from position “1”, till position “-2”. With the negative symbol, we indicating that should take from the end and not from the start.

I will explain the basic syntax for substr:
EXPR – string expression from which the substring will be extracted.
OFFSET – an index from where the substring to be extracted starts.
LENGTH – the length of the substring to extract.

Advertisements

Posted on 19 August 2013, in elguber, programming and tagged , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: