Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extract data from files using pattern matching

Status
Not open for further replies.

milage

Programmer
Jul 13, 2001
58
0
0
US
Hi,

I have to extract some information using perl from some files and I assume will deal with pattern matching which I am not very experienced with so am in need of a bit of help. Below is a sample of the format of these files, although of course these files are not totally uniform and can have some irregularities.

<-- Sample File Starts Here (this is not part of the file -->

/**
* file description
**/

/**
* A number of comments like this
**/

create table table_name (
timestamp,
attribute1 char(3) not null, /* Description 1 */
attribute2 float not null, /* Description 2 */
attribute3 float not null,
attribute4 float not null, /* Description 3 */

) on data_seg

<-- Sample ends here -->

What I would like to do is to extract into a 2 by X array the fieldname eg timestamp, attribute1 etc and the description of that fieldname in the comments at the end of the line eg Description 1 (I don't want the comment markers). However, some lines do not have comments so I need the description of these to be left blank.

I hope this is enough info and that someone can help.

Cheers

Rob
 
I took a stab at figuring this one out, but my regexing still needs work. I was able to get this:

Code:
/^\s+(attribute[0-9]{1,2}).*\*\s+(.*?)\*/;

I was able to get it to grab the attribute and description...but that's about it. The other things I messed around with aren't working very well [wink]

The attibute and description values are stored in $1 and $2, respectively.

Anyway, HTH. Notorious P.I.G.
 
Using PerlIsGood expression, try

#!/user/local/bin/perl

use diagnostics;

# I stuck your file example here
$log_file = &quot;e:/perl practice/regexp/tek_tip_file.txt&quot;;

open(INFO, $log_file) or die &quot;could not open tek_tip_file.txt: #!&quot;;
@lines = <INFO>;
close(INFO);


foreach $lines(@lines)
{
if ($lines =~ /create table\s+(\w+)/)
{
print &quot;$1\n&quot;;
}
if ($lines =~ /^\s+(attribute[0-9]{1,2}).*\*\s+(.*?)\*/)
{
print &quot;$1\n&quot;;
print &quot;$2\n&quot;;
}
}

print &quot;finished\n&quot;;

HTH,

Parke
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top