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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Grep 1

Status
Not open for further replies.

killahara

Programmer
Jun 24, 2003
9
DE
Want to search for a reoccuring string in a file and then copy from the string to a another given string : is this possible using perl ?

Any advice on how to code it properly cause my recent attempts are just creating errors.

Dan
 
Hi Dan,

Give us an example of what you're trying to do -- some test data.

A line that you want to match.
A line that you *don't* want to match.

And then an example of what you'd like to do with the data.

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
need to remove strings from a huge scripe made of thousands of the following :

------ Rebuild All started: Project: Types, Configuration: Debug .NET
------

Preparing resources...
Updating references...
Performing main compilation...

Build complete -- 0 errors, 0 warnings
Building satellite assemblies...


Need it to identify the start say "----- Rebuild" and the end say "assemblies"

Want to copy the code (after testing the 0 errors part) to a new file say "errorlog.txt" to reduce the time it takes to find all the indivual failings

Thanks,
 
do you need to remove all of the text, and output to a new file, from ------ Rebuild up to and including assemblies...?

Duncan
 
so, in this make-believe situation below, you would want to remove the highlighted red block and write to 'errorlog.txt' and keep the rest of the text as it is

text
more text
even more text

[red]------ Rebuild All started: Project: Types, Configuration: Debug .NET
------

Preparing resources...
Updating references...
Performing main compilation...

Build complete -- 0 errors, 0 warnings
Building satellite assemblies...[/red]

text
more text
even more text
 
Exactly : this is just one section of an output file : others are of the same format just some of the central text is differnt
 
o.k. - this does just what you ask

it works through the file, line by line, outputting all the lines to 'errorlog.txt' until it sees Rebuild All started. then it outputs the text to the screen until it sees Building satellite assemblies then it reverts back to 'errorlog.txt' again. a kind of switch.

open (INFILE, &quot;<file.txt&quot;);

open (ERRORLOG, &quot;>errorlog.txt&quot;);

select ERRORLOG;

while (<INFILE>) {

if (/Rebuild All started/) { select STDOUT; }

print &quot;$_&quot;;

if (/Building satellite assemblies/) { select ERRORLOG; }

}

close INFILE;

close ERRORLOG;


Hope this is useful
Duncan
 
Duncan this is working : just that it works backwards i.e. it copies everything beside whats between

Rebuild All started

and

Building satellite assemblies...
 
other way round...

open (INFILE, &quot;<file.txt&quot;);

open (ERRORLOG, &quot;>errorlog.txt&quot;);

select STDOUT;

while (<INFILE>) {

if (/Rebuild All started/) { select ERRORLOG; }

print &quot;$_&quot;;

if (/Building satellite assemblies/) { select STDOUT; }

}

close INFILE;

close ERRORLOG;

# ------ Rebuild All started: Project: Types, Configuration: Debug .NET
# Building satellite assemblies...

Regards
Duncan
 
Ok that works : brilliant !!! Thanx a million ! Really got me out of a stick spot !

One final question if ur interested : what would you think would be the best way to test if the errors value is not equal to 0

i.e. only copy the sections which have errors


e.g.

copy :

------ Rebuild All started: Project: Types, Configuration: Debug .NET
------

Preparing resources...
Updating references...
Performing main compilation...

Build complete -- 2 errors, 0 warnings
Building satellite assemblies...

Dont Copy :

------ Rebuild All started: Project: Types, Configuration: Debug .NET
------

Preparing resources...
Updating references...
Performing main compilation...

Build complete -- 0 errors, 0 warnings
Building satellite assemblies...


Thanks again !
 
This works...

open (INFILE, &quot;<file.txt&quot;);

open (ERRORLOG, &quot;>errorlog.txt&quot;);

select STDOUT;

while (<INFILE>) {

if (/Rebuild All started/) { select ERRORLOG; }

print &quot;$_&quot;;

if (/Build complete -- 0 errors/) { $deleteFile = &quot;Y&quot;; }

if (/Building satellite assemblies/) { select STDOUT; }

}

close INFILE;

close ERRORLOG;

if ($deleteFile eq &quot;Y&quot;) { print &quot;\n\n*** NO ERRORS - DELETING 'ERRORLOG.TXT' ***\n\n&quot;; unlink &quot;errorlog.txt&quot; }

Duncan
 
Not really working. Should ideally take >

------ Rebuild All started: Project: HiTumClientWebProxy,
Configuration: Debug .NET ------

Preparing resources...
Updating references...
Performing main compilation...

Build complete -- 2 errors, 0 warnings
Building satellite assemblies...

------ Rebuild All started: Project: BaseBusinessLogic,
Configuration: Debug .NET ------

Preparing resources...
Updating references...
Performing main compilation...

Build complete -- 0 errors, 0 warnings
Building satellite assemblies...

------ Rebuild All started: Project: ConAMCCBL, Configuration: Debug
.NET ------

Preparing resources...
Updating references...
Performing main compilation...

Build complete -- 0 errors, 0 warnings
Building satellite assemblies...

------ Rebuild All started: Project: HiTumLib, Configuration: Debug
.NET ------

Preparing resources...
Updating references...
Performing main compilation...

Build complete -- 7 errors, 0 warnings
Building satellite assemblies...

<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

and give back :

------ Rebuild All started: Project: HiTumClientWebProxy,
Configuration: Debug .NET ------

Preparing resources...
Updating references...
Performing main compilation...

Build complete -- 2 errors, 0 warnings
Building satellite assemblies...

------ Rebuild All started: Project: HiTumLib, Configuration: Debug
.NET ------

Preparing resources...
Updating references...
Performing main compilation...

Build complete -- 7 errors, 0 warnings
Building satellite assemblies...

thanks
 
This is quite a bit different than the direction you were going, but you could give this a shot:


open (INFILE, &quot;<file.txt&quot;);
$/ = undef;
$file = <INFILE>;
close INFILE;

$file =~ /(Rebuild All started.+Building satellite assemblies)/is;

open (ERRORLOG, &quot;>>c:/temp/errorlog.txt&quot;);
if ($1 =~ /Build complete -- [1-9]{1}[0-9]* errors/) {
print ERRORLOG &quot;$file&quot;;
}
close ERRORLOG;
 
the $/ = undef; will read in the whole file in one go. this gets round the problem of having to wait until getting to the error line to find '0 errors' - then all that has been written is in vain! raklet's regex can then search through all of the necessary lines before deciding to write to the file or not

Nice!

Duncan
 
Thanks Duncan,

That is a nice compliment from someone that I look up to as a mentor on this forum. Perl is my first programming language and I have only been at it for 3-4 months.

Tyler
 
Blimey! - Very kind words. I don't consider myself as adept as you at this Perl scripting! Only 3-4 months... You're doing an amazing job... keep it up!!!

Kind Regards
Duncan
 
Not to br one to complain but all this does is make an exact copy of the INFILE ever time. Maybe Im not helping amidst all the confusion : I dont want to copy the entire file if there is errors just the sections which contain errors. So if a section between one of the &quot;Rebuild All started.+Building satellite assemblies&quot; contains an error it is added to the &quot;errorlog.txt&quot; file otherwise it skips to the next &quot;Rebuild All started.+Building satellite assemblies&quot;. The script should then at the end reduce 26 pages or more down to just the one or two pieces which contain errors.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top