1

I am executing a command for which I am getting output like below

Identifier:  3    SFP
Connector:   7    LC
Transceiver: 7004404000000000 4,8,16_Gbps M5 sw Short_dist
Encoding:    6    64B66B 
Baud Rate:   140  (units 100 megabaud)
Length 9u:   0    (units km)
Length 9u:   0    (units 100 meters)
Length 50u (OM2):  3    (units 10 meters)
Length 50u (OM3):  10   (units 10 meters)
Length 62.5u:0    (units 10 meters)
Length Cu:   0    (units 1 meter)       
Vendor OUI:  00:05:1e
Vendor PN:   57-0000088-01   
Vendor Rev:  A   
Wavelength:  850  (units nm)
Options:     003a Loss_of_Sig,Tx_Fault,Tx_Disable
BR Max:      0   
BR Min:      0   
Date Code:   180316  
DD Type:     0x68
Enh Options: 0xfa
Status/Ctrl: 0xb2
Pwr On Time: 3.52 years (30822 hours)
E-Wrap Control: 0
O-Wrap Control: 0
Alarm flags[0,1] = 0x5, 0x40
Warn Flags[0,1] = 0x5, 0x40
Temperature: 32      Centigrade 
Current:     8.082   mAmps 
Voltage:     3311.6  mVolts 
RX Power:    -5.5    dBm (280.4uW) 
TX Power:    -2.8    dBm (523.1 uW) 

I need to fetch the last 2 lines only, that is starting with RX and TX out of it.

I was trying like

#$streamOut | Select-String -Pattern "^RX" | select -ExpandProperty line
$streamOut | Select-String -Pattern "RX" | select -ExpandProperty line

This is by code

$session = New-SSHSession -ComputerName $SAN_IP -Credential $cred
$Strem = New-SSHShellStream -SSHSession $Session
$streamOut=@()
$SystemView = $Strem.WriteLine("sfpshow $port_Num")  
sleep -Seconds 5      
$streamOut = @($Strem.read())
sleep -Seconds 5
$RXTX_Data = @($streamOut | ? { $_ -match "^RX" -or  $_ -match "^TX"})      

$RXTX_Data

When I use the below solution in above code, it is returning blank. the $streamOut is array

PS C:\Windows\system32> $streamOut.GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                      
-------- -------- ----                                     --------                                                                                                      
True     True     Object[]                                 System.Array                                                                                                  

But it is returning the entire output.

Please let me know on this

2
  • $streamOut | Select-String -Pattern "^(RX|TX)" | Select-Object -ExpandProperty Line works for me, provided that $streamOut is an array of lines, such as from a call to Get-Content. Please add the code where you assign $streamOut. Commented May 19, 2022 at 9:57
  • @zett42: I m using Strem to execute the commands and $streamOut is a string. so is there any way to convert that to array or any other approch Commented May 19, 2022 at 12:03

2 Answers 2

4

As you clarified, the $streamOut variable contains a single, multiline string.

In this case you can use Select-String with -AllMatches like this:

$RXTX_Data = ($streamOut | Select-String -Pattern '(?m)^(RX|TX).*$' -AllMatches).Matches.Value
$RXTX_Data  # Output array of matching lines

Output:

RX Power:    -5.5    dBm (280.4uW)
TX Power:    -2.8    dBm (523.1 uW)

Note that it is important to use the (?m) inline modifier for multiline mode, which changes behaviour of ^ to anchor beginning of line and $ to anchor end of line, instead of beginning and end of whole input string.

An easier alternative is to split the string into lines first, so you can use the -match operator to filter for matching lines:

$streamLines = $streamOut -split '\r?\n'
$RXTX_Data = $streamLines -match '^(RX|TX)'

This works because -match and other comparison operators act as a filter when the LHS operand is a collection like an array. We don't need multiline mode, because the RegEx gets applied to each array element (line) individually.

The above two lines could even be condensed into a one-liner:

$RXTX_Data = ($streamOut -split '\r?\n') -match '^(RX|TX)'
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the response. I am getting the data of RX and TX. need one more help. how to get the numeric value out of it. like from RX Power: -5.5 dBm (280.4uW) extract -5.5
@EmptyCoder $rxValue = if('RX Power: -5.5 dBm (280.4uW)' -match ':\s*(\S+)') { $matches[1] }
1

try this :

$streamOut = Get-Content -Path "Your_Output_FilePath"
$streamOut | ? { $_ -match "^RX" -or  $_ -match "^TX"} 

3 Comments

Nice! Even shorter one: $streamOut -match '^(RX|TX)'. This works because -match and other comparison operators act as a filter when the LHS operand is an array.
@Romylussone: one more thing. this solution is working only for object[], not for String.
@zett42: one more thing. this solution is working only for object[], not for String.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.