LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-01-2006, 02:27 AM   #1
ohcarol
Member
 
Registered: Dec 2004
Location: Nepal
Posts: 86

Rep: Reputation: 15
perl script to parse this file


HOw can I parse the the selected value only from the list below with perl?
Like for "queu dn_def_quick" bytes 12430
and for "queue dn_usr1_def " bytes 11808199 dropped pkts: 188





queue dn_def_quick bandwidth 3.20Kb priority 6 hfsc( red ecn )
[ pkts: 0 bytes: 12430 dropped pkts: 112 bytes: 0 ]
[ qlength: 0/ 50 ]
[ measured: 0.0 packets/s, 0 b/s ]
queue dn_usr1 bandwidth 25.60Kb {dn_usr1_def, dn_usr1_web, dn_usr1_quick}
[ pkts: 0 bytes: 124450 dropped pkts: 0 bytes: 0 ]
[ qlength: 0/ 50 ]
[ measured: 0.0 packets/s, 0 b/s ]
queue dn_usr1_def bandwidth 12.80Kb priority 2 hfsc( red ecn realtime 12.80Kb )
[ pkts: 15437 bytes: 11808199 dropped pkts: 188 bytes: 226101 ]
 
Old 11-01-2006, 06:09 AM   #2
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Rep: Reputation: 52
Is this what you want?

Code:
twantrd@tdebian:~/perl_scripting$ perl /tmp/match.pl
Section Match: queue dn_def_quick
bytes: 12430

Section Match: queue dn_usr1_def
bytes: 11808199 dropped pkts: 188
twantrd@tdebian:~/perl_scripting$
My code really sucks and I hope someone could assist in either cleaning it up or rewriting a new one that makes better sense and logic. Anyhow, below is one that I wrote. I'm still learning perl .

Code:
#!/usr/bin/perl

open FH, "/tmp/file" || die "File does not exist!";
while (<FH>){
  last if (/queue dn_def_quick bandwidth/);
}

# do first section
my $section=<FH>;
my($bracket,$packet,$num,$bytes,$num1,$dropped,$packet1,$num2,$bytes1,$num,$bracket2)=split(/ /, $section);
print "Section Match: queue dn_def_quick\n";
print "$bytes $num1\n\n";

# do second section
while (<FH>){
  last if (/dn_usr1_def bandwidth/);
}
my $section=<FH>;
my ($bracket,$packet,$num,$bytes,$num1,$dropped,$packet1,$num2,$bytes1,$num,$bracket2)=split(/ /, $section);
close FH;
print "Section Match: queue dn_usr1_def\n";
print "$bytes $num1 $dropped $packet1 $num2\n";
-twantrd
 
Old 11-01-2006, 09:11 AM   #3
ohcarol
Member
 
Registered: Dec 2004
Location: Nepal
Posts: 86

Original Poster
Rep: Reputation: 15
Thanks for you quick response
But what about the white spaces which have 5-6 white spaces

queue up_def_web bandwidth 6.40Kb priority 3 hfsc( red ecn )
[ pkts: 0 bytes: 0 dropped pkts: 0 bytes: 0 ]
[ qlength: 0/ 50 ]
queue up_def_quick bandwidth 6.40Kb priority 6 hfsc( red ecn )
[ pkts: 36 bytes: 3177 dropped pkts: 0 bytes: 0 ]
[ qlength: 0/ 50 ]
queue up_usr1 bandwidth 51.20Kb {up_usr1_def, up_usr1_web, up_usr1_quick}
[ pkts: 0 bytes: 0 dropped pkts: 0 bytes: 0 ]
[ qlength: 0/ 50 ]
queue up_usr1_def bandwidth 25.60Kb priority 2 hfsc( red ecn realtime 25.60Kb )
[ pkts: 1394 bytes: 482975 dropped pkts: 0 bytes: 0 ]
[ qlength: 0/ 50 ]

Last edited by ohcarol; 11-01-2006 at 09:14 AM.
 
Old 11-01-2006, 01:52 PM   #4
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Rep: Reputation: 52
What about them? I don't see where the problem is.

-twantrd
 
Old 11-01-2006, 05:53 PM   #5
ohcarol
Member
 
Registered: Dec 2004
Location: Nepal
Posts: 86

Original Poster
Rep: Reputation: 15
May be white spaces are not shown by this forum.

Last edited by ohcarol; 11-01-2006 at 05:59 PM.
 
Old 11-01-2006, 06:00 PM   #6
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Just change the split regexp to match one or more space ?
Code:
my($..., $..., ...)=split(/ +/, $section);
 
Old 11-01-2006, 06:21 PM   #7
ohcarol
Member
 
Registered: Dec 2004
Location: Nepal
Posts: 86

Original Poster
Rep: Reputation: 15
Thanks, it really help a lot, now another sample file is

Interface Stats for rl0 IPv4 IPv6
Bytes In 178420849 0
Bytes Out 31594925 0
Packets In
Passed 276970 0
Blocked 140482 0
Packets Out
Passed 182922 0
Blocked 780 0

While doing "last if(/Packets In/)" command it only shows Passed 276970 0. What I want to do is for "Packets In" I want to get both Passed and Blocked IPv4 value i.e. for passed: 276970 and Blocked: 140482
Same for "Packets Out" Passed: 182922 Blocked: 780
 
Old 11-01-2006, 06:28 PM   #8
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
just redo a $section=<FH>; and consecutive line
 
Old 11-01-2006, 07:19 PM   #9
ohcarol
Member
 
Registered: Dec 2004
Location: Nepal
Posts: 86

Original Poster
Rep: Reputation: 15
Yeah i did that but it will only display "Passed 141190 0" line only

Packets In has two line
Packets In
Passed 4585422 0
Blocked 4550 0

Whereas Packets Out has same two line too

Packets Out
Passed 55666 0
Blocked 4454 0

What I want is "last if(/ Packets In/) will display Packets In both passed and blocked line so that I can extract the value.
 
Old 11-02-2006, 03:23 AM   #10
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Rep: Reputation: 52
Here ya go. Below is the output:

Code:
twantrd@tdebian:/tmp$ ./match.pl
Packets In
Passed 276970 0
Blocked 140482 0
Code:
#!/usr/bin/perl

open FH, "/tmp/file" || die "File does not exist!";
while (<FH>){
  last if (/Packets In/);
}
$prev1=$_;
my ($curr, $next1)=<FH>;
close (FH);
print "$prev1$curr$next1";
-twantrd
 
Old 11-02-2006, 09:50 AM   #11
ohcarol
Member
 
Registered: Dec 2004
Location: Nepal
Posts: 86

Original Poster
Rep: Reputation: 15
Thanks, that was it what I want.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ssimple shell script to parse a file ~sed or awk stevie_velvet Programming 7 07-14-2006 03:41 AM
optimizing perl parse file. eastsuse Programming 1 12-22-2004 02:49 AM
Need help with perl/bash script to parse PicBasic file cmfarley19 Programming 13 11-18-2004 05:06 PM
use php script to parse a file. blackzone Linux - Software 1 07-07-2004 04:43 AM
How to read ans parse MS word file using a Linux Shell script. Alek Linux - General 2 11-10-2003 02:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration