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

Get specific line from file 1

Status
Not open for further replies.

mte0910

Programmer
Apr 20, 2005
232
US
1st - Total Newby to Perl.
2nd - Using Perl on Windows 2003

3rd - I have a txt file that looks like this...
Junk blah, blah, blah
Name: App1
Junk blah, blah, blah
Source Revision: 101
Installed Revision: 101
Junk blah, blah, blah
Installed: Yes
Junk blah, blah, blah
Junk blah, blah, blah
Junk blah, blah, blah
Name: App2
Junk blah, blah, blah
Source Revision: 99
Installed Revision: 98
Junk blah, blah, blah
Installed: No
Junk blah, blah, blah
Junk blah, blah, blah
etc.

I need to get the entire lines that match the following
Name:
Source Revision:
Installed Revision:
Installed:

Over and over again, until EOF, then copy this infomation to another file. It would be even better if I could copy them into the next file as...
Name:App1 <TAB> Source Revision:101 <TAB> Installed Revision:101 <TAB> Installed:Yes <NEW LINE>
Name:App2 <TAB> Source Revision:99 <TAB> Installed Revision:98 <TAB> Installed:No <NEW LINE>
etc....

Hope this makes sense

 
What have you tried so far?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
open (IN, 'c:\tmp01.txt');
open (STDOUT,'>c:\tmp00.txt');
while (<IN>) {
chomp;
if(/^Name:/^Source Revision:/^Installed Revision:/^Installed://);
print;
}
close(IN);
close STDOUT;
*****************************************
tmp01.txt has the raw data
tmp00.txt will contain the end result

 
Looks like it should work, does it not? It will print as one line though because of the chomp.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
syntax error at EWFAudit.pl line 63, near "Source Revision:"
Execution of EWFAudit.pl aborted due to compilation errors.
 
ahh, yea, right here (see red text):

Code:
if(/^Name:[red]/[/red]^Source Revision:[red]/[/red]^Installed Revision:[red]/[/red]^Installed:[red]//[/red])[red];[/red]
print;
}

should be:

Code:
if(/^(Name[red]|[/red]Source Revision[red]|[/red]Installed Revision[red]|[/red]Installed):[red]/[/red])[red]{[/red]
print;
}

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
It's compiling fine, but there are no results in the final file.
I tried to modify it a little more to sort the records one per line, but that had no effect either....

open (IN, 'c:\tmp01.txt');
open (STDOUT,'>c:\tmp00.txt');
my $line2;
while ( $line2 = <IN>) {
chomp $line2;
if(/^(Name|Source Revision|Installed Revision|Installed):/){
printf STDOUT "$line2\n";
}
}
close(IN);
close STDOUT;
 
well now you need to match $line2:

Code:
if($line2 =~ /^(Name|Source Revision|Installed Revision|Installed):/){

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
You don't need to use "printf", just use "print".

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
1st-Thank you very much for your patience with me on this.

2nd-Here is what the code looks like now...
open (IN, 'c:\tmp01.txt');
open (STDOUT,'>c:\tmp00.txt');
my $line2;
while ( $line2 = <IN>) {
chomp $line2;
if($line2 =~ /^(Name|Source Revision|Installed Revision|Installed):/){
print STDOUT "$line2\n";
}
}
close(IN);
close STDOUT;

3rd-Here is the result...
Name: 2.00_AnswerCall
Source Revision: 101
Installed Revision: 101
Installed: Yes
Name: 2.01_CallRouter_Utility
Source Revision: 105
Installed Revision: 105
Installed: Yes
Name: 2.0800_MAIN
Source Revision: 537
Installed Revision: 537
Installed: Yes
etc., etc., etc...

4th-While this is totally fine, can I get the info on one line per Name:?
Not a deal killer.
 
assuming "Installed" is always last in order in the file:

Code:
   if($line2 =~ /^(Name|Source Revision|Installed Revision|Installed):/){
     print "$line2 ";
     print "\n" if ($1 eq 'Installed');
   }


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top