Home > OS >  Fetch line starting with a specific string in powershell
Fetch line starting with a specific string in powershell

Time:05-20

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

CodePudding user response:

try this :

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

CodePudding user response:

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)'
  • Related