Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: perl file formatting

by perlforsgs (Initiate)
on Jan 12, 2017 at 11:26 UTC ( [id://1179435]=note: print w/replies, xml ) Need Help??


in reply to Re: perl file formatting
in thread perl file formatting

Tried something like this But couldnt get the required ,Any help APPRECIATED
#!/usr/bin/perl my $isHeader = 1; my $targetDate = 20161002; foreach (<STDIN>){ if (!$isHeader){ my ($M12,$M11,$M481,$M55,$M989) = split(' ',$_); if ($M11 <= $targetDate){ print $_; } } $isHeader = 0; }

Replies are listed 'Best First'.
Re^3: perl file formatting
by 1nickt (Canon) on Jan 12, 2017 at 11:57 UTC

    Are your input data columns really separated by a space? In the sample above it looks like it may be tabs, in which case your regexp won't match. Maybe try:

    split(/\s+/, $_);

    Hope this helps!


    The way forward always starts with a minimal test.

      The separator is comma ,yes missed that in my snippet and I also need the date to be dynamically retrieved based on the current date of processing ,rather than hardcoded date

        tybalt89 showed you how to get the current date in the YYYYMMDD format you need.

        use strict; use warnings; my $enddate = todate(); while(<DATA>) { last if ($. > 1 and $enddate < (split /,/)[1]); print $_; } sub todate { my ($year, $month, $day) = (localtime)[5,4,3]; return sprintf "%04d%02d%02d",$year + 1900, $month + 1, $day; } __DATA__ F12,F11,F481,F55,F989 8204B,20161002,1045.469,test,Y 8204,20170112,1064.848,test,Y 8204A,20170113,1064.505,test,Y 8204B,20170114,1045.469,test,Y

        $. is a built-in variable that counts lines of input. The split function implicitly operates on the current input line, producing a list (or maybe an array), and we take a slice of that to look at the only field we care about, the date in the second column.

        H:\perl>perl 1178418.pl F12,F11,F481,F55,F989 8204B,20161002,1045.469,test,Y 8204,20170112,1064.848,test,Y 8204A,20170113,1064.505,test,Y

        Updated: reduced if () {last if()} to just last if().

        But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

      Actually each fields are separated by comma
Re^3: perl file formatting
by tybalt89 (Monsignor) on Jan 14, 2017 at 16:04 UTC
    #!/usr/bin/perl # http://perlmonks.org/?node_id=1178418 use strict; use warnings; sub eightdigitdate { my @fields = localtime(shift); sprintf '%04d%02d%02d', $fields[5] + 1900, $fields[4] + 1, $fields[3 +]; } sub nextday { use Time::Local; shift() =~ /(\d{4})(\d\d)(\d\d)/; eightdigitdate( 60*60*24 + timelocal 0, 0, 12, $3, $2 - 1, $1 ); } #my $today = eightdigitdate(time); my $today = '20161002'; print scalar <STDIN>; print my @data = <STDIN>; my $more = 1; while($more) { for(@data) { my $date = (split)[1]; if( $date < $today ) { s/\b$date\b/ nextday($date) /e; print; } else { $more = 0; } } }

      F12 F11 F481 F55 F989 E1 E2 E3

      8204 20160930 1064.848 test Y 10

      8204A 20160930 1064.505 test Y 6 4

      Thanks for all the help.Now things are getting lil more from previous.We have three more columns added E1,E2 ,E3.E3 doesnt have anything as off now .Just a placeholder. Now a new logic must be bulit for as below: F481 = IF (E2 NOT NULL) then (E1 - E2) / 2 ELSE (F481) So after computations the output must be as:

      F12 F11 F481 F55 F989

      8204 20160930 1064.848 test Y

      8204A 20160930 1 test Y

      The final output record format will be as per previous format .The first record meets the else part and hence remain unchanged . The second record gets computed as above.The new columns are never part of actual output

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1179435]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2024-04-23 16:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found