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!

Perl read block of file 3

Status
Not open for further replies.

Hemulll

Technical User
Nov 10, 2008
25
RU
Hello Perl Gurus.

I have a question about , how to read block of text file, for example i have a text file in next format:


MR_ManagedObject
true
2
LINK-PORT-10.112.32.73/1003<->PORT-10.112.32.81/8026

Switch
10.112.32.73
Switch
10.112.32.81
TrunkCable
LINK-PORT-10.112.32.73/1003<->PORT-10.112.32.81/8026
TrunkCable
false
1
10.112.32.33
ROSPD-TVE-Vadkov20
Switch
10.112.32.33

graph: 0
{

topology:
{
node: 1
{
type: 0
INCHARGE-SA-ROSPD
INCHARGE-SA-ROSPD::Node::10.112.32.82
0
false
false
10.112.32.82
MR_ManagedObject
0
text: GEDE_UPS
geometry:
{
visible: true
x: 1224.6231150243873
y: 982.9352026187435
width: 145.5
height: 101.0
originalWidth: 145.5
originalHeight: 101.0
folder: false
connector: 2
{
geometry:
{
visible: true
movable: false
specified: false
offsetX: 0.0
offsetY: 0.0
proportionalX: 0.0
proportionalY: 0.0
}
ui:
{
name: com.tomsawyer.editor.ui.TSEDefaultConnectorUI
}
}
}
resize: 0
ui:
{
name: com.smarts.viewer.map.integration.tomsawyer.SmImageNodeUI
borderColor: 0 0 255
drawBorder: true
font: Helvetica-bold-12
textColor: 0 0 255
image: configurableMapIcons/IconMissing.gif
}
}
edge: 67
{
type: 0
INCHARGE-SA-ROSPD
INCHARGE-SA-ROSPD::Cable::LINK-IF-10.112.32.82/1<->PORT-10.112.32.9/1023
LINK-IF-10.112.32.82/1<->PORT-10.112.32.9/1023
Cable
text: LINK-IF-10.112.32.82/1<->PORT-10.112.32.9/1023
source: 1
target: 3
geometry:
{
visible: true
sourceConnector: 2
targetConnector: 4
}
ui:
{
name: com.smarts.viewer.map.integration.tomsawyer.SmPlainEdgeUI
lineColor: 255 0 0
arrowType: 0
}
}
layout:
{
style: circular
respectShapes: true
marginSpacing:
{
proportionalLeftSpacing: 0.0
proportionalRightSpacing: 0.0
proportionalBottomSpacing: 0.0
proportionalTopSpacing: 0.0
constantLeftSpacing: 20.0
constantRightSpacing: 20.0
constantBottomSpacing: 20.0
constantTopSpacing: 20.0
originalConstantLeftSpacing: 20.0
originalConstantRightSpacing: 20.0
originalConstantBottomSpacing: 20.0
originalConstantTopSpacing: 20.0
}
}
tailor:
{
key: layout.glt.orthogonal
{
com.tomsawyer.layout.glt.property.TSBooleanLayoutProperty
{
nameKey: orthogonal.keepNodeSizes
value: true
}
}
key: layout.glt.global
{
com.tomsawyer.layout.glt.property.TSIntLayoutProperty
{
nameKey: global.labelSelection
value: 3
}
}
key: layout.glt.circular
{
com.tomsawyer.layout.glt.property.TSBooleanLayoutProperty
{
nameKey: circular.disableMinNumberOfNodesInCluster
value: true
}
com.tomsawyer.layout.glt.property.TSBooleanLayoutProperty
{
nameKey: circular.disableMaxNumberOfNodesInCluster
value: true
}
com.tomsawyer.layout.glt.property.TSIntLayoutProperty
{
nameKey: circular.proportionalSubsiteClusterRadialSpacing
value: 300
}
com.tomsawyer.layout.glt.property.TSIntLayoutProperty
{
nameKey: circular.proportionalSubsiteClusterTangentialSpacing
value: 300
}
com.tomsawyer.layout.glt.property.TSIntLayoutProperty
{
nameKey: circular.proportionalNodeSpacing
value: 250
}
}
}
ui:
{
name: com.smarts.viewer.map.integration.tomsawyer.SmGraphUI
backgroundColor: 240 248 255
}
}
ZOOM=0.4248693931320715
EndOfSchema

So, have next main blocks.
1) graph
2) topology
3) node
4) edge
5) layout
etc ...

A question is: how can i change only "edge" block?

And needs to take line 3 from edge block like parameter for compare in continue.
P.s
this file have many edge blocks and many node blocks,
So, a question is , how to modify only "edge:" block , for example add lines to edge block after some data manipulation.

 
First, is this what you consider the edge block?

Code:
edge: 67
{
type: 0
INCHARGE-SA-ROSPD
INCHARGE-SA-ROSPD::Cable::LINK-IF-10.112.32.82/1<->PORT-10.112.32.9/1023
LINK-IF-10.112.32.82/1<->PORT-10.112.32.9/1023
Cable
text: LINK-IF-10.112.32.82/1<->PORT-10.112.32.9/1023
source: 1
target: 3
geometry:
    {
    visible: true
    sourceConnector: 2
    targetConnector: 4
    }
ui:
    {
    name: com.smarts.viewer.map.integration.tomsawyer.SmPlainEdgeUI
    lineColor: 255 0 0
    arrowType: 0
    }
}

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Hello Kevin!

Exactly So, each edge block in text file has 2 sub blocks, geometry and ui.
But, file has a lot of edge blocks.

edge: 1
{
type: 0
source: 1
target: 3
geometry:
{
values:
}
ui:
{
values
}
}

edge: 2
{
type: 0
source: 1
target: 3
geometry:
{
values:
}
ui:
{
values
}
}
 
Based on your example, here's one way.
Code:
while (<DATA>) {
	if (/^\s*edge:/) {
		my @record = $_;    # Store edge record
		my $count;          # count opening / closing brackets
		while (my $line = <DATA>) {
			push @record, $line;
			$count++ if $line =~ m/\{/;
			$count-- if $line =~ m/}/;
			if ($count == 0) {
				chomp @record;
				# Process edge record
				print "$_\n" for @record;
				last;
			}
		}
	} else {
		print;
	}
}
You'll need to change <DATA> to whatever you use for a file handle.
 
Hello rharsh

Thank you for response.

I have another question about edge: block, how can i add line to edge block after data manipulation?

For example in each edge: block line number 4 has ip addresses, i need to do something with this data and after data manupulation add something to edge block .

How can i realize this ?
 
Hi rharsh!

Thank you for response!

My issue is how to write this modified file to new one with same structure.
 
Just open the output file at the beginning of the outer loop:
[blue]open (OUTPUT, ">>$output_path") || die "Cannot open output file: $!";[/blue]

Replace the print statements to print to file instead of screen e.g.:
[blue]print OUTPUT "$_\n" for @record;[/blue]
and:
[blue]print OUTPUT;[/blue]

Close output file at the end of the outer loop:
[blue]close (OUTPUT);[/blue]

Chris
 
If you want to edit the file inplace, you can use the Tie::File module or perls inplace editor. The Tie::File module, which comes with perl, makes the process easy in that you treat the file just like an array.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thank you Kevin

I will try to write this functional with this module
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top