We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,248 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Read , search and edit a file with perl

Hi,

I need a perl function that will check the following in a file, modify if only the first two columns match , and if not found in the file, insert the values.

The file is like below
lookup.txt

InstanceName1::DomainName1::Server1
InstanceName1::DomainName2::Server2
InstanceName2::DomainName3::Server3
InstanceName2::DomainName4::Server3
...
...

The perl function needs to match the instance name, domain name, and check if the servername is the same , else replace the server name, and after iterating through the file, if the string is not found, insert the same into the file. The stub function i have written explains the same.

sub functionname()
{
        my $lookupfile = "lookupfile";
        my($var1,$var2,$var3)=@_;
        open(MAPPING,"<$lookupfile") || die $!;

        while($key=<MAPPING>)
        {
                chomp $key;
                my @keyarray =split(/::/,$key);
                $cusName= $keyarray[0];
                $apmName= $keyarray[1];
                $host= $keyarray[2];
                $skey= $cusName."::".$apmName;
                $sskey= $var1."::".$ipName;
                if ( ! -z $cusName && ! -z $apmName )
                {
                    if ( $cusName eq $var1 )
                    {
                        if ( $skey eq $sskey )
                        {
                            if ( $host eq $var3 )
                            {
                                print "present";
                            }
                            else
                            {
                                print $cusName."::".$apmName."::".$host;
                                print $var1."::".$var2."::".$var3;
                                ## replace first with second
                                $presentflag=1;
                            }
                        }

                    }
                }
        }
        if ( $presentflag == 0 )
        {
            # insert into the file $var1,$var2,$var3 as $var1::$var2::$var3
        }

        close(MAPPING);
}
2
Contributors
3
Replies
2 Days
Discussion Span
1 Year Ago
Last Updated
4
Views
asadarun
Newbie Poster
2 posts since Apr 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

I would read and print every line from the file into a new file, changing the line that matches your first two arguments. If you read the entire file without finding a match then print your new line at the end of your new file. If the new file needs to be in ascending order you can reopen it, sort it, and rewrite it.

d5e5
Practically a Posting Shark
831 posts since Sep 2009
Reputation Points: 162
Solved Threads: 163
Skill Endorsements: 1

You can also do this by tieing your file to an array using Tie::File so any change you make to the array will get written to the file.

#!/usr/bin/perl
use warnings;
use strict;

use Tie::File;

my $filename = 'lookup.txt';
my @lookup;

die "$filename not found" unless -e $filename;
tie @lookup, 'Tie::File', $filename or die "Failed to tie $filename: $!";

edit_file('InstanceName1','DomainName2','Server5'); #Edit existing record

edit_file('InstanceName8','DomainName2','Server999'); #Add new record

untie @lookup;            # all finished

sub edit_file{
    my ($iname, $dname, $sname) = @_;
    my $found = 0;

    foreach my $rec (@lookup){
        chomp $rec;
        my @keyarray = split(/::/, $rec);
        if ($keyarray[0] . $keyarray[1] eq $iname . $dname){
            $found++;
            $rec = join('::', $iname, $dname, $sname);
        }
    }
    if ($found == 0){
        push @lookup, join('::', $iname, $dname, $sname);
    }
}
d5e5
Practically a Posting Shark
831 posts since Sep 2009
Reputation Points: 162
Solved Threads: 163
Skill Endorsements: 1

Thanks , i will try these..

asadarun
Newbie Poster
2 posts since Apr 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0903 seconds using 2.67MB