Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
What I'm trying to do is to replace the contents of fields 10 and 11 with those passed to the perl utility but only for the line which matches the id (the first field) whilst maintaining the rest of the record as is and the other records in the file as is.1,AB499,Joe.Bloggs@mysite.com,MY_SERVER_ENV,sales,/opt/backup/MY_SERVE +R_ENV,sales.data,1,dbase,Apr 25 2008 3:25PM,Apr 25 2008 3:30PM,comple +ted 2,AB499,none,Somebody.Admin@mysite.com,none,none,1,dbase,none,none,non +e 3,AB499,none,Somebody.Admin@mysite.com,none,none,1,dbase,none,none,non +e 4,AB499,none,Somebody.Admin@mysite.com,none,none,1,dbase,none,none,non +e 5,AB499,none,Somebody.Admin@mysite.com,none,none,1,dbase,none,none,non +e 6,AB499,none,Somebody.Admin@mysite.com,none,none,1,dbase,none,none,non +e 7,XX777,myserver,Anon.Person@mysite.com,business,/nfs/busback/upload/i +ncident,10,dbase,none,none,none 8,XX777,myserver,Anon.Person@mysite.com,business,/nfs/busback/upload/i +ncident,10,dbase,Aug 13 2010 8:30AM,Aug 13 2010 9:00AM,completed
Here is a snippet from the code .
Now the problem. An example is if I pass the utility the enddate (Aug 13 2010 9:30AM) and state (failed) for a requestid of 3 it's doing this to the file.$requestid = 0 unless $requestid; $enddate = "none" unless $enddate; $state = "none" unless $state; if ($update) { local $^I = ".bak"; # in place editing with backup fil +e @ARGV = qw(/test/info.dat); while (<>) { chomp; my @array = split /,/,$_; if ($array[0] eq $requestid) { if ($enddate) { s/$array[9]/$enddate/g + ;} if ($state) { s/$array[10]/$state/g ;} print; print "\n" ; } else { print join(",",@array); print "\n"; } } }
If I then run the utility again using the same params it then produces this the next time around.1,AB499,Joe.Bloggs@mysite.com,MY_SERVER_ENV,sales,/opt/backup/MY_SERVE +R_ENV,sales.data,1,dbase,Apr 25 2008 3:25PM,Apr 25 2 008 3:30PM,completed 2,AB499,none,Somebody.Admin@mysite.com,none,none,1,dbase,none,none,non +e 3,AB499,Aug 13 2010 9:30AM,Somebody.Admin@mysite.com,Aug 13 2010 9:30A +M,Aug 13 2010 9:30AM,1,dbase,Aug 13 2010 9:30AM,Aug 13 2010 9:30AM,Aug 13 2010 9:30AM 4,AB499,none,Somebody.Admin@mysite.com,none,none,1,dbase,none,none,non +e 5,AB499,none,Somebody.Admin@mysite.com,none,none,1,dbase,none,none,non +e 6,AB499,none,Somebody.Admin@mysite.com,none,none,1,dbase,none,none,non +e 7,XX777,myserver,Anon.Person@mysite.com,business,/nfs/busback/upload/i +ncident,10,dbase,none,none,none 8,XX777,myserver,Anon.Person@mysite.com,business,/nfs/busback/upload/i +ncident,10,dbase,Aug 13 2010 8:30AM,Aug 13 2010 9:00 AM,completed
The default for most of the fields is none. The substitute command is obviously not working the way I wanted it to.I have no idea what is happening here . I'd be grateful for any help or pointers to writing better perl code !1,AB499,Joe.Bloggs@mysite.com,MY_SERVER_ENV,sales,/opt/backup/MY_SERVE +R_ENV,sales.data,1,dbase,Apr 25 2008 3:25PM,Apr 25 2 008 3:30PM,completed 2,AB499,none,Somebody.Admin@mysite.com,none,none,1,dbase,none,none,non +e 3,AB499,failed,Somebody.Admin@mysite.com,failed,failed,1,dbase,failed, +failed,failed 4,AB499,none,Somebody.Admin@mysite.com,none,none,1,dbase,none,none,non +e 5,AB499,none,Somebody.Admin@mysite.com,none,none,1,dbase,none,none,non +e 6,AB499,none,Somebody.Admin@mysite.com,none,none,1,dbase,none,none,non +e 7,XX777,myserver,Anon.Person@mysite.com,business,/nfs/busback/upload/i +ncident,10,dbase,none,none,none 8,XX777,myserver,Anon.Person@mysite.com,business,/nfs/busback/upload/i +ncident,10,dbase,Aug 13 2010 8:30AM,Aug 13 2010 9:00 AM,completed
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Problem with inline replace
by jethro (Monsignor) on Aug 13, 2010 at 13:23 UTC | |
by Marshall (Canon) on Aug 15, 2010 at 06:45 UTC | |
by Anonymous Monk on Aug 13, 2010 at 13:39 UTC | |
by jethro (Monsignor) on Aug 13, 2010 at 15:48 UTC | |
|
Re: Problem with inline replace
by Marshall (Canon) on Aug 15, 2010 at 04:49 UTC |