Solved

extract data from text file

Posted on 2011-02-19
4
448 Views
Last Modified: 2012-05-11
Hi,

I have a large text file from which I need to extract lines as below:
time-range 02293404
 absolute end 23:59 19 September 2009
time-range 10May2010_SR405807
 absolute end 23:59 10 May 2010
time-range 11july2010
 absolute end 23:59 11 July 2010


The final output would be:
scheduler <name> start-date YYYY-MM-DD.HH:MM stop-date YYYY-MM-DD.HH:MM

For start-data we can use static date as: 2009-01-01.00:00
Stop-date would be the end date above.

Eg,
scheduler A start-date 2009-01-01.00:00 stop-date 2009-09-10.23:59
scheduler B start-date 2009-01-01.00:00 stop-date 2010-10-19.23:59
scheduler C start-date 2009-01-01.00:00 stop-date 2010-11-11.23:59

Thanks for all the help in advance.
0
Comment
Question by:dpk_wal
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 26

Expert Comment

by:wilcoxon
ID: 34935793
Where do you get scheduler names from?  Your sample text file lines don't appear to include the info.
0
 
LVL 32

Author Comment

by:dpk_wal
ID: 34937252
Sorry for the typo:
>> time-range 02293404
the word after time-range is the scheduler name; in original post the names are 02293404, 10May2010_SR405807, 11july2010.

Regards.
0
 
LVL 16

Accepted Solution

by:
sjklein42 earned 500 total points
ID: 34938314
%mm = ('Jan','01', 'Feb','02', 'Mar','03', 'Apr','04', 'May','05', 'Jun','06', 'Jul','07', 'Aug','08', 'Sep','09', 'Oct','10', 'Nov','11', 'Dec','12');

while ( <> )
{
	s/[\r\n]//g;

	if ( $_ ne '' )
	{
		# time-range 10May2010_SR405807

		if ( ! ( /^time-range / ) ) { die"*** expected time-range not found\n"; }
		$schedName = $';

		#  absolute end 23:59 10 May 2010

		$_ = <>;
		if ( ! ( /^ absolute end ([0-9]+)\:([0-9]+) ([0-9]+) ([a-z]+) ([0-9]+)/i ) )
			{ die"*** expected absolute end not found\n"; }
		($hh, $min, $dd, $mmm, $yyyy) = ($1, $2, $3, $4, $5);

		# scheduler A start-date 2009-01-01.00:00 stop-date 2009-09-10.23:59

		$mm = $mm{substr($mmm,0,3)};
		print "scheduler $schedName start-date 2009-01-01.00:00 stop-date "
			. $yyyy . "-" . $mm . "-" . $dd . "." . $hh . ":" . $min . "\n";
	}
}

Open in new window


C:\temp>perl foo.pl foo.txt
scheduler 02293404 start-date 2009-01-01.00:00 stop-date 2009-09-19.23:59
scheduler 10May2010_SR405807 start-date 2009-01-01.00:00 stop-date 2010-05-10.23:59
scheduler 11july2010 start-date 2009-01-01.00:00 stop-date 2010-07-11.23:59

Open in new window

0
 
LVL 32

Author Closing Comment

by:dpk_wal
ID: 34940262
Thank you!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
EXCHANGE, ACTIVE DIRECTORY 4 69
Best way to split and output to csv in bash 2 75
linux pull logs and export FTP 4 83
powershell code to list items in dropdown 6 102
This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question