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

Finding a previous line 2

Status
Not open for further replies.

johnv20

Programmer
Sep 26, 2001
292
US
Hi,
I have a file of form
A121
bbb
ccc
A123
dd
12345
ddd
A134

and I need to be able to extract all lines between the 2 lines which start with A and contain the value 12345 between them. However there will be other data later starting with A and there may be lines containing ( but not starting with) also throughout the file

Does anybody have any ideas?
 
Well, the best thing to do is to read the file into an array, and run through the whole array with alot of check statements. The good thing about the array is that you can easily access previous and next lines. Let me know if you need further help.

wzrd
 
while (<>) {
if (/^A/) {
# print remebered lines of one of them contain 12345
foreach (@temp) { print; } if (grep /12345/, @temp);
# clear temp
@temp = ();
} else {
push @temp, $_;
}
}
 
neat... Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top