Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hello monks,
I am new to perl and trying to do the following , I have an output file that looks like this :
Server: win1 ID: D: Space: 6496911360 Server: ux22 ID: D: Space: 7973826560 Server: del2 ID: U: Space: 7973228544
I need to open and read that file and then verify the space , if the space is less than 4 Gig , then I need to modify the file by putting a flag next to that line somthing like a warning message:

I am trying the following right now:
open (INPUT, "data") || die "couldn't open the file!"; foreach $line (<INPUT>) { ($SERVER, $Drive, $SPACE) = split(/:/,$line); } close(INPUT);
How do I compare space, and modify it within the file , I need to skip the one above 4 Gig?
thanks

Replies are listed 'Best First'.
Re: compare size
by holli (Abbot) on Feb 15, 2006 at 19:43 UTC
    perl -pi.bak -aF/:/ -e "s/$/ * warning */ if $F[4]<4*(1024**3)" yourfi +le


    holli, /regexed monk/
Re: compare size
by GrandFather (Saint) on Feb 15, 2006 at 19:48 UTC

    You can generate sample code as shown below to create a small sample file and then demonstrate the issue or solution.

    use strict; use warnings; # First generate a sample data file open (outFile, '>', "delme.txt") or die "Can't create test file: $!"; print outFile <<DATA; Server: win1 ID: D: Space: 6496911360 Server: ux22 ID: D: Space: 7973826560 Server: del2 ID: U: Space: 7973228544 Server: del3 ID: U: Space: 3973228544 DATA close outFile; #Now the solution code local $^I = '.bak'; local @ARGV = ('delme.txt'); #Omit this if the file is provided on the + command line while (<>) { chomp; print; #Print the original line my ($size) = /(\d+)$/; print " <--- too small" if $size < 4_000_000_000; print "\n"; }

    Modifies the file to:

    Server: win1 ID: D: Space: 6496911360 Server: ux22 ID: D: Space: 7973826560 Server: del2 ID: U: Space: 7973228544 Server: del3 ID: U: Space: 3973228544 <--- too small

    Setting $^I and using while (<>) enables in place editing of the files passed on the command line. In this case a list of files is provided by setting $ARGV.


    DWIM is Perl's answer to Gödel
Re: compare size
by ikegami (Patriarch) on Feb 15, 2006 at 19:25 UTC
    my $text; { open(local *INPUT, '<', "data") or die "Unable to open input file: $!\n"; local $/; # Read whole file at once. $text = <INPUT>; } $text =~ s/(\d+)$/ $1 >= 4_000_000_000 ? $1 : "$1 <---- LOW!" ; /emg; { open(local *OUTPUT, '>', "data") or die "Unable to open output file: $!\n"; print OUTPUT ($text; }
    or if that's the entire script, you could use
    #!/usr/bin/perl -pi s/(\d+)$/ $1 >= 4_000_000_000 ? $1 : "$1 <---- LOW!" ; /;
Re: compare size
by Roy Johnson (Monsignor) on Feb 15, 2006 at 19:30 UTC
    I think you will find it convenient to use Tie::File to edit your file in-place. Something like
    use strict; use warnings; use Tie::File; tie my @lines, 'Tie::File', filename or die ...; for (@lines) { s/(?=(\d+)$)/ 'Warning! ' if $1 < 4_000_000_000/e; }

    Caution: Contents may have been coded under pressure.