I am executing "porterrshow" command on switches using below code
$cred = Get-Credential
$SAN_S = @(Get-Content -Path "E:\switches.txt")
foreach($SANs in $SAN_S)
{
$session = New-SSHSession -ComputerName $SANs -Credential $cred
$Strem = New-SSHShellStream -SSHSession $Session
sleep -Seconds 10
$SystemView = $Strem.WriteLine("porterrshow")
$streamOut = $Strem.read()
$streamOut | Out-File -FilePath "E:\$SANs.txt" -Force
}
below is the output I am getting
frames enc crc crc too too bad enc disc link loss loss frjt fbsy c3timeout pcs uncor
tx rx in err g_eof shrt long eof out c3 fail sync sig tx rx err err
0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1: 2.5g 3.6g 0 0 0 0 0 0 0 2 0 0 1 0 0 0 0 0 0
2: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
3: 3.3g 1.5g 0 0 0 0 0 0 0 3 0 0 1 0 0 0 0 0 0
I want to extract the rows which are having any value which is not "0". in above line 2 and 4.
Please let me know how to do that
and I am executing this on multiple switches, so is it possible to put some filter in the porterrshow command itself which will show only the non-zero rows?
Output based on @Theo code
frames enc crc crc too too bad enc disc link loss loss frjt fbsy c3timeout pcs uncor
tx rx in err g_eof shrt long eof out c3 fail sync sig tx rx err err
2: 615.8m 1.8g 0 0 0 0 0 0 0 2 0 0 1 0 0 0 0 0 0
3: 2.1g 1.2g 0 0 0 0 0 0 0 2 0 0 1 0 0 0 0 0 0
4: 1.4g 2.9g 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0 0
5: 258.0m 623.6m 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
6: 1.7g 1.1g 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
7: 512.5m 73.1m 0 0 0 0 0 0 0 17 0 0 0 0 0 0 0 0 0
8: 49.6m 2.4g 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
One of the output file for reference
frames enc crc crc too too bad enc disc link loss loss frjt fbsy c3timeout pcs uncor
tx rx in err g_e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 0 149 0
expected output for the above
frames enc crc crc too too bad enc disc link loss loss frjt fbsy c3timeout pcs uncor
tx rx in err g_eof shrt long eof out c3 fail sync sig tx rx err err
2: 615.8m 1.8g 0 0 0 0 0 0 0 2 0 0 1 0 0 0 0 0 0
3: 2.1g 1.2g 0 0 0 0 0 0 0 2 0 0 1 0 0 0 0 0 0
4: 1.4g 2.9g 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0 0
7: 512.5m 73.1m 0 0 0 0 0 0 0 17 0 0 0 0 0 0 0 0 0
9: 599.8m 1.7g 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0 0
10: 2.8g 254.9m 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0 0
12: 2.5g 556.1m 0 0 0 0 0 0 0 384 0 0 1 0 0 0 0 0 0
13: 2.9g 3.5g 0 0 0 0 0 0 0 2 0 0 1 0 0 0 0 0 0
14: 1.7g 3.6g 0 0 0 0 0 0 0 12 0 0 0 0 0 0 0 0 0
16: 777.7m 3.1g 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0 0
23: 4.0g 2.9g 0 0 0 0 0 0 0 83 0 0 0 0 0 0 0 0 0
25: 1.2g 2.3g 0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0 0
28: 838.1m 3.9g 0 0 0 0 0 0 0 258 0 0 1 0 0 0 0 0 0
29: 2.5g 843.1m 0 0 0 0 0 0 0 3 0 0 1 0 0 0 0 0 0
30: 2.4g 927.7m 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0
37: 1.8g 672.6m 0 0 0 0 0 0 0 12 0 0 0 0 0 0 0 0 0
40: 3.5g 2.2g 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0 0
53: 568.2m 1.4g 0 0 0 0 0 0 0 3 0 0 1 0 0 0 0 0 0
54: 3.7g 3.7g 0 0 0 0 0 0 0 4 0 0 1 0 0 0 0 0 0
67: 4.0g 1.6g 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 149 0
CodePudding user response:
I wouldn't know of any switches for porterrshow, but you can remove the lines with only zero values in the saved text file like below:
$result = switch -Regex -File 'D:\Test\PortErr.txt' {
'^\s*\d :(. )$' {
$values = $matches[1] -replace '\s'
if ($values -ne ('0' * $values.Length)) { $_ }
}
default { $_ }
}
# show on screen
$result
# write out to new file ?
$result | Set-Content -Path 'D:\Test\NewPortErr.txt'
Output using your example:
frames enc crc crc too too bad enc disc link loss loss frjt fbsy c3timeout pcs uncor
tx rx in err g_eof shrt long eof out c3 fail sync sig tx rx err err
1: 2.5g 3.6g 0 0 0 0 0 0 0 2 0 0 1 0 0 0 0 0 0
3: 3.3g 1.5g 0 0 0 0 0 0 0 3 0 0 1 0 0 0 0 0 0
From your comment, I gather that the code needs to filter out lines with only zero values, not counting the first two columns tx
and rx
For that, do:
$result = switch -Regex -File 'D:\Test\PortErr.txt' {
'^\s*\d :(. )$' {
$values = ($matches[1].Trim() -split '\s ' | Select-Object -Skip 2) -join ''
if ($values -ne ('0' * $values.Length)) { $_ }
}
default { $_ }
}
# show on screen
$result
# write out to new file ?
$result | Set-Content -Path 'D:\Test\NewPortErr.txt'
Using your new example, the output will be:
frames enc crc crc too too bad enc disc link loss loss frjt fbsy c3timeout pcs uncor
tx rx in err g_eof shrt long eof out c3 fail sync sig tx rx err err
2: 615.8m 1.8g 0 0 0 0 0 0 0 2 0 0 1 0 0 0 0 0 0
3: 2.1g 1.2g 0 0 0 0 0 0 0 2 0 0 1 0 0 0 0 0 0
4: 1.4g 2.9g 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0 0
7: 512.5m 73.1m 0 0 0 0 0 0 0 17 0 0 0 0 0 0 0 0 0
9: 599.8m 1.7g 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0 0
10: 2.8g 254.9m 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0 0
12: 2.5g 556.1m 0 0 0 0 0 0 0 384 0 0 1 0 0 0 0 0 0
13: 2.9g 3.5g 0 0 0 0 0 0 0 2 0 0 1 0 0 0 0 0 0
14: 1.7g 3.6g 0 0 0 0 0 0 0 12 0 0 0 0 0 0 0 0 0
16: 777.7m 3.1g 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0 0
23: 4.0g 2.9g 0 0 0 0 0 0 0 83 0 0 0 0 0 0 0 0 0
25: 1.2g 2.3g 0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0 0
28: 838.1m 3.9g 0 0 0 0 0 0 0 258 0 0 1 0 0 0 0 0 0
29: 2.5g 843.1m 0 0 0 0 0 0 0 3 0 0 1 0 0 0 0 0 0
30: 2.4g 927.7m 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0
37: 1.8g 672.6m 0 0 0 0 0 0 0 12 0 0 0 0 0 0 0 0 0
40: 3.5g 2.2g 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0 0
53: 568.2m 1.4g 0 0 0 0 0 0 0 3 0 0 1 0 0 0 0 0 0
54: 3.7g 3.7g 0 0 0 0 0 0 0 4 0 0 1 0 0 0 0 0 0
67: 4.0g 1.6g 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 149 0