CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Extract specific data from 2 files in to a file with date as input

 



chkiran2
New User

Nov 15, 2016, 11:18 PM

Post #1 of 4 (1903 views)
Extract specific data from 2 files in to a file with date as input Can't Post

I am new to Perl and just started learning. But I need to get something done urgently. Request help of this.

Requirement: Perl script should accept input as date in MM/DD/YY format. For the input date first it should search file1.txt and extract records into an already existing file outfile.txt of only file1routine1 and fiel1exec3 and report starting and ending times. Next from file2.txt it should write only records of file2para2 and file2exec3 related records for the given input date.
In this example, if input is passed as 11/04/16, outfile.txt should contain below records on execution. On next execution for a different date, the records should be appended to the outile.txt.

file1.txt contents:

Code
  
file1routine1 Starting: 11/03/16 00:08:46
file1para2 Starting: 11/03/16 00:08:47
file1para2 Ending: 11/03/16 00:09:10
file1exec3 Starting: 11/03/16 00:09:38
file1routine1 Ending: 11/03/16 00:10:32
file1exec3 Ending: 11/03/16 00:11:27
file1routine1 Starting: 11/04/16 00:08:46
file1para2 Starting: 11/04/16 00:07:47
file1para2 Ending: 11/04/16 00:07:10
file1exec3 Starting: 11/04/16 00:08:38
file1routine1 Ending: 11/04/16 00:09:32
file1exec3 Ending: 11/04/16 00:10:27
file1routine1 Starting: 11/05/16 00:09:46
file1para2 Starting: 11/05/16 00:09:47
file1para2 Ending: 11/05/16 00:10:10
file1exec3 Starting: 11/05/16 00:10:38
file1routine1 Ending: 11/05/16 00:11:32
file1exec3 Ending: 11/05/16 00:12:27



file2.txt contents:

Code
  
file2routine1 Starting: 11/03/16 01:08:46
file2para2 Starting: 11/03/16 01:08:47
file2para2 Ending: 11/03/16 01:09:10
file2exec3 Starting: 11/03/16 01:09:38
file2routine1 Ending: 11/03/16 01:10:32
file2exec3 Ending: 11/03/16 01:11:27
file2routine1 Starting: 11/04/16 01:08:46
file2para2 Starting: 11/04/16 01:07:47
file2para2 Ending: 11/04/16 01:08:10
file2exec3 Starting: 11/04/16 01:08:38
file2routine1 Ending: 11/04/16 01:09:32
file2exec3 Ending: 11/04/16 01:10:27
file2routine1 Starting: 11/05/16 01:09:46
file2para2 Starting: 11/05/16 01:09:47
file2para2 Ending: 11/05/16 01:10:10
file2exec3 Starting: 11/05/16 01:10:38
file2routine1 Ending: 11/05/16 01:11:32
file2exec3 Ending: 11/05/16 01:12:27



In this case the output data should look like:

Outfile.txt on executing with input as 11/04/16:

Code
  
1--------10---------20---------30---------40---------50---------60---------70---------80
file1routine1 11/04/16 00:08:46 00:09:32
file1exec3 11/04/16 00:08:38 00:10:27
file2para2 11/04/16 01:07:47 01:08:10
file2exec3 11/04/16 01:08:38 01:10:27

Attachments: perl requirement.txt (2.45 KB)


Laurent_R
Veteran / Moderator

Nov 15, 2016, 11:27 PM

Post #2 of 4 (1902 views)
Re: [chkiran2] Extract specific data from 2 files in to a file with date as input [In reply to] Can't Post

Do you seriously expect us to do your work in your place? This is not how this site works.

We're ready to help you if you encounter difficulties, but you should show at least some efforts, some code that you've attempted and possibly did not work as you expected.

So, please show your code.


chkiran2
New User

Nov 15, 2016, 11:34 PM

Post #3 of 4 (1901 views)
Re: [Laurent_R] Extract specific data from 2 files in to a file with date as input [In reply to] Can't Post

Sorry. But I am new to Perl and that was the reason for posting. I will try and post my code shortly.


BillKSmith
Veteran

Nov 16, 2016, 8:11 AM

Post #4 of 4 (1890 views)
Re: [chkiran2] Extract specific data from 2 files in to a file with date as input [In reply to] Can't Post

You 'urgent' is not my 'urgent'. This should get you started. It includes all the processing for one file. The other file would be much the same.

Code
use strict; 
use warnings;

#Simulate file1.txt;
my $file1 = \<<'END_FILE1';
file1routine1 Starting: 11/03/16 00:08:46
file1para2 Starting: 11/03/16 00:08:47
file1para2 Ending: 11/03/16 00:09:10
file1exec3 Starting: 11/03/16 00:09:38
file1routine1 Ending: 11/03/16 00:10:32
file1exec3 Ending: 11/03/16 00:11:27
file1routine1 Starting: 11/04/16 00:08:46
file1para2 Starting: 11/04/16 00:07:47
file1para2 Ending: 11/04/16 00:07:10
file1exec3 Starting: 11/04/16 00:08:38
file1routine1 Ending: 11/04/16 00:09:32
file1exec3 Ending: 11/04/16 00:10:27
file1routine1 Starting: 11/05/16 00:09:46
file1para2 Starting: 11/05/16 00:09:47
file1para2 Ending: 11/05/16 00:10:10
file1exec3 Starting: 11/05/16 00:10:38
file1routine1 Ending: 11/05/16 00:11:32
file1exec3 Ending: 11/05/16 00:12:27
END_FILE1

my $date = '11/04/16';
my %data;
open my $fh, '<', $file1;
while (<$fh>) {
my ($entry, $start_end, $time)
= /
(file1routine1 | file1exec3)\s
(Starting | Ending)\:\s
$date\s
(\d\d:\d\d\:\d\d)\s*
$
/x;
next if !defined $entry;
$data{$entry}{$start_end} = $time;
}
close $fh;
print "$_ $date @{$data{$_}}{'Starting', 'Ending'}\n" foreach keys %data;

Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives