ulaksh has asked for the
wisdom of the Perl Monks concerning the following question:
I am trying to get the value and put the value in proper format but it is not happening.
------------------------------------------------------------------
Port Speed Duplex Flow Ctrl Link Name
---- ----- -------- --TX-----RX-- ------ ------
1 10000 full no no down 1
2 10000 full no no down 2
3 10000 full no no down 3
4 10000 full no no down 4
5 10000 full no no down 5
6 10000 full no no down 6
7 10000 full no no down 7
8 10000 full no no down 8
9 10000 full no no down 9
10 10000 full no no down 10
11 10000 full no no down 11
12 10000 full no no down 12
13 10000 full no no down 13
14 10000 full no no down 14
15 10000 full no no down 15
16 10000 full no no down 16
17 1G/10G full no no down 17
18 1G/10G full no no down 18
19 1000 full no no up 19
20 10000 full no no down 20
And my script look like this.
open FILE,"interfacelink.txt" or die "cannot open file : $!";
#my $substring = "encap:"; #this is useful if search istring is long
while(<FILE>) {
$line ="$_\n";
$ind=$line=~ /up/;
if ($ind){
#print "$line\n";
print "port is", $port, "=", substr($line,0,4)."\n";
print "speed is", $speed, "=", substr($line,7,5)."\n";
print "link is", $link, "=", substr($line,42,4)."\n";
}
}
I am getting the output like this
port is=Port
speed is=Speed
link is=Link
port is= 19
speed is= 1000
link is= up
My expecting the output like
port is= 19
speed is= 1000
link is= up
Re: how to put the value in proper format by choroba (Prior) on Aug 29, 2012 at 07:57 UTC |
The word "Duplex" also matches /up/. Make the regex more firmrigid, as /\bup\b/, using word boundaries.
| [reply] [d/l] [select] |
|
need help How to get the index for "LINK" for the first line
----------------------------------------------------------
Port Speed Duplex Flow Ctrl Link Name
---- ----- -------- --TX-----RX-- ------ ------
| [reply] [d/l] |
Re: how to put the value in proper format by johngg (Abbot) on Aug 29, 2012 at 09:26 UTC |
If you want your output for the chosen port to be all on one line, as might be suggested by the last line of your post, don't put the line feeds (\n) in the first two print statements.
However, it does not look like the output you show was produced by that code, the equals sign is after the port/speed/link values in the print statements. Using substr here seems like a lot of hard work and is potentially fragile. I think it would be better to split the line.
$ perl -Mstrict -Mwarnings -E '
> open my $inFH, q{<}, \ <<EOF or die $!;
> ------------------------------------------------------------------
> Port Speed Duplex Flow Ctrl Link Name
> ---- ----- -------- --TX-----RX-- ------ ------
> 1 10000 full no no down 1
> 2 10000 full no no down 2
> 3 10000 full no no down 3
> 4 10000 full no no down 4
> 5 10000 full no no down 5
> 6 10000 full no no down 6
> 7 10000 full no no down 7
> 8 10000 full no no down 8
> 9 10000 full no no down 9
> 10 10000 full no no down 10
> 11 10000 full no no down 11
> 12 10000 full no no down 12
> 13 10000 full no no down 13
> 14 10000 full no no down 14
> 15 10000 full no no down 15
> 16 10000 full no no down 16
> 17 1G/10G full no no down 17
> 18 1G/10G full no no down 18
> 19 1000 full no no up 19
> 20 10000 full no no down 20
> EOF
>
> while ( <$inFH> )
> {
> next unless m{^\s*\d};
> my( $port, $speed, $link ) = ( split )[ 0, 1, 5 ];
> next unless $link eq q{up};
> say qq{port is = $port: speed is = $speed: link is = $link};
> }'
port is = 19: speed is = 1000: link is = up
$
I hope this is helpful.
| [reply] [d/l] [select] |
Re: how to put the value in proper format by ww (Chancellor) on Aug 29, 2012 at 10:20 UTC |
Re multi-line output, the code you show has you adding a newline to your data at line 4 as well as in the output lines -- the ones pointed out by johngg. And choroba is right about the regex, though I'd use "precise" rather than "firm."
But the code you show has very little to do with the output you claim or that you say you want as the vars $port, $speed and $link are neither instantiated nor assigned a value.
Downvoted, for abusing the Monastery's goodwill.
Update: Thanks jethro for msg which made me realize I'd misread the "=" in lines 8, 9 and 10 as an attempt at assignment. It isn't of course; it's merely redundant since the text includes the verb "is." However....
OP's position counts are for the header rather than the actual data... and the inclusion of commented lines that bear no relevance to the problem simply wastes the Monks' time.
#!/usr/bin/perl
use 5.014;
# 990400
# all global; too lazy to work hard on OP's code
my ($line, $ind, $port, $speed, $link);
while (<DATA>) {
$line = $_;
if ( $line =~ /\bup\b/ ) {
print "port is ", $port, substr($line,1,2). ", ";
print "speed is ", $speed, substr($line,7,5). ", ";
print "link is ", $link, substr($line,42,4)."\n";
}
}
# NB Port 10 Link status changed to "up" (and indented as in Port 19)
__DATA__
------------------------------------------------------------------
Port Speed Duplex Flow Ctrl Link Name
---- ----- -------- --TX-----RX-- ------ ------
1 10000 full no no down 1
2 10000 full no no down 2
3 10000 full no no down 3
4 10000 full no no down 4
5 10000 full no no down 5
6 10000 full no no down 6
7 10000 full no no down 7
8 10000 full no no down 8
9 10000 full no no down 9
10 10000 full no no up 10
11 10000 full no no down 11
12 10000 full no no down 12
13 10000 full no no down 13
14 10000 full no no down 14
15 10000 full no no down 15
16 10000 full no no down 16
17 1G/10G full no no down 17
18 1G/10G full no no down 18
19 1000 full no no up 19
20 10000 full no no down 20
Output:
C:\> 990400.pl
port is 10, speed is 10000, link is up
port is 19, speed is 1000, link is up
| [reply] [d/l] [select] |
|
I need one small modification if the line contains "Link" in the first line as sown below then get the same output i.e
port is 10, speed is 10000, link is up
port is 19, speed is 1000, link is up
Please let me know how to do that
-----------------------------------------------------------
Port Speed Duplex Flow Ctrl Link Name
---- ----- -------- --TX-----RX-- ------ ------
| [reply] [d/l] [select] |
Re: how to put the value in proper format by jethro (Monsignor) on Aug 29, 2012 at 10:37 UTC |
Please use "use warnings;" in your scripts. Warnings would have alerted you to the fact that $port, $speed and $link are undef
(Also please use "use strict;", while we are on the subject. It is worth it in the long run if you get used to it now)
| [reply] |
Re: how to put the value in proper format by marto (Chancellor) on Aug 29, 2012 at 10:49 UTC |
You haven't taken the advice given in response to your previous question, you're making the same mistakes.
| [reply] |
|
i think you can use unpack and get the results.
http://perldoc.perl.org/functions/unpack.html
| [reply] |
|
| [reply] [d/l] |
|
Trust me, it is much easier to split on /s+/.
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James My blog: Imperial Deltronics
| [reply] [d/l] [select] |
Re: how to put the value in proper format by CountZero (Chancellor) on Aug 29, 2012 at 19:33 UTC |
What do you think of this? use Modern::Perl;
while (<DATA>) { # get rid of header block
last if /--TX--/;
};
while (<DATA>) {
my ($port, $speed, $duplex, $TX, $RX, $link, $name) = split;
next unless $link eq 'up';
say <<EOP;
Port is $port
Speed is $speed
Link is $link
EOP
}
__DATA__
------------------------------------------------------------------
Port Speed Duplex Flow Ctrl Link Name
---- ----- -------- --TX-----RX-- ------ ------
1 10000 full no no down 1
2 10000 full no no down 2
3 10000 full no no down 3
4 10000 full no no down 4
5 10000 full no no down 5
6 10000 full no no down 6
7 10000 full no no down 7
8 10000 full no no down 8
9 10000 full no no down 9
10 10000 full no no down 10
11 10000 full no no down 11
12 10000 full no no down 12
13 10000 full no no down 13
14 10000 full no no down 14
15 10000 full no no down 15
16 10000 full no no down 16
17 1G/10G full no no down 17
18 1G/10G full no no down 18
19 1000 full no no up 19
20 10000 full no no down 20
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James My blog: Imperial Deltronics
| [reply] [d/l] |
|
here I dont have to use using Modern::Perl;
need urgent help :I dont know how to check the condition like if the line contain "Link" in the first line then check the port link is up or down and if it is up then get the same out as above.
Please let me know how to do that.
| [reply] |
|
Sorry, I am not sure if I understand your question.There is only one line in your file that contains the word "LINK" and that is in the second line of the header. Furthermore, the script already prints the information for all lines where the LINK field shows the value of "up". Whether you use Modern Perl is up to you, but if you do not use it, at least use strict; use warnings.
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James My blog: Imperial Deltronics
| [reply] [d/l] [select] |
|
|