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

How can I print multiple lines in between expressions?

Status
Not open for further replies.

jaquemon

Technical User
Joined
May 22, 2007
Messages
5
Location
US
I am trying to print out lines from a file into another file based on a couple of patterns. I sort of can do it from the command line using "perl -ne '/START/ .. /END/' INfile", but I am trying to put it into a more user friendly script. Anyway I can't figure out how to do it inside a perl script. Can anyone help me?

@lines = <INFILE>;
print OUTFILE $agent;
print OUTFILE "\n";
print OUTFILE "\n";

# go through each line in the file
foreach $line (@lines)
{

# This is where I am screwed up
$temp = /$agent/ .. /^$/;
print $temp;
# $line =~ /FMULV(.*)/
# print OUTFILE $line;
# print OUTFILE "\n";
}
close (INFILE);
 
You're probably looking for something like this. The logic can obviously be made even more explicit such as using command line parameters etc. But this should at least bring you a couple more steps to making a full script.

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$infile[/blue] = [red]"[/red][purple]INfile[/purple][red]"[/red][red];[/red]
[black][b]my[/b][/black] [blue]$outfile[/blue] = [red]"[/red][purple]OUTfile[/purple][red]"[/red][red];[/red]

[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url][red]([/red]IN, [blue]$infile[/blue][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Can't open [blue]$infile[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]
[black][b]open[/b][/black][red]([/red]OUT, [red]"[/red][purple]> [blue]$outfile[/blue][/purple][red]"[/red][red])[/red] or [black][b]die[/b][/black] [red]"[/red][purple]Can't open [blue]$outfile[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]

[olive][b]while[/b][/olive] [red]([/red]<IN>[red])[/red] [red]{[/red]
	[olive][b]if[/b][/olive] [red]([/red][red]/[/red][purple]START[/purple][red]/[/red]..[red]/[/red][purple]END[/purple][red]/[/red][red])[/red] [red]{[/red]
		[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] OUT [blue]$_[/blue][red];[/red]
	[red]}[/red]
[red]}[/red]

[url=http://perldoc.perl.org/functions/close.html][black][b]close[/b][/black][/url] IN[red];[/red]
[black][b]close[/b][/black] OUT[red];[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
[/tt]

- Miller
 

if ($line=~ /FMULV(.*)/) {
print OUTFILE "$line\n";
}


something like that?
 
All that does is write the first pattern to the file. I need everything from the first time I see that pattern until the first empty line (new paragraph). I am not that great at this stuff and my brain hurts right now. Thanks for everything so far and anything in the future.
 
What MillerH wrote does exactly what you want.. assuming I guess that the START and END patterns are unique in you data.
 
I'm going through a phase, playing around with map and grep:

So I replaced MillerH's while() structure..
Code:
print OUT grep { /START/../END/ } <IN>;

Question for the Original Poster:
What's the /FMULV(.*)/ regex for ?
 
I have a file sort of like the one below. What I need to do is look for a pattern(START) and copy everything up until END. The delimeter is always a number with one decimal place and followed by a blank line. It is exactly for looking for data in a program we use for genome parsing.

ajksdhfhsdfhkj
sdjfhjhsdjkfh
jsdhjfhjksdhf
sdhfjhsdjkfh
sdjfhajkdshfjk

fjsdjfkljdkfjkjkfjkjjfjfjkjkfjsj_START_asfj
hjfjkshdfhjk
fasdjhfjks
dsjhfjkhsad
sdfhjahsd
ajdshjfs_END

ajksdhfhsdfhkj
sdjfhjhsdjkfh
jsdhjfhjksdhf
sdhfjhsdjkfh
sdjfhajkdshfjk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top