Sumith Chalil
Sumith Chalil

Reputation: 358

Powershell -how to remove line details from selectstring output

I want to select a string "install status" from a log file by using powershell. For that i have used the command $status=selectstring -path $path -pattern "installstatus" .This cmd given an output installstatus=success with path and logtime and line details at beginning and i removed the path by adding |{$_.Line} after pattern in $status.And i want to remove line details also.how can i remove those details.I only want the pattern to be displayed .Any help will be apperciated.Thanks in advancd.

here the scenarios

$path="C:\Users\sumith\filename.log";
$status=select-string -path $path -pattern "Install_status"

output is

C:\Users\sumith\filename.log:79:ISS_LOG [14:45:41]: INSTALL_STATUS:SUCCESS

if i give

$status=select-string -path $path -pattern "Install_status" | {$_.Line}

the output will be

ISS_LOG [14:45:41]: INSTALL_STATUS:SUCCESS

now i want to remove ISS_LOG [14:45:41]: from output

Upvotes: 1

Views: 2698

Answers (3)

bashing
bashing

Reputation: 1

For console output (modified Chetan Kulkarni answer)

(Select-String -Path .\file.txt -Pattern 'abcde').line

Upvotes: 0

Chetan Kulkarni
Chetan Kulkarni

Reputation: 404

Try this,

$status=select-string -path $path -pattern "Install_status" | {$_.Line}
$status.Substring(19)OR

 $status=select-string -path $path -pattern "Install_status" | {$_.Line}
$status.Substring($status.IndexOf('IN'))

or

 $status=select-string -path $path -pattern "Install_status" | {$_.Line}
 $status.Substring($status.IndexOf(']:')+2)


    Or using Regex (Credits to LotPengs)
 $Content=Get-Content D:\creditdetails.txt
$Pattern = [regex]'INSTALL_STATUS:\s*(\w+)'
if( $Pattern.Matches($Content) -ne $null){
$Matches=$Pattern.Matches($Content)
$Matches.Captures[0].Value 
}else{
$Matches='No Match Found'
}

Assuming the log structure remains the same.

Upvotes: 0

user6811411
user6811411

Reputation:

IMO you are better of using a regular expression for this:

$File = ".\filename.log"
$Content = (Get-Content $File -raw)

$Pattern = [regex]'INSTALL_STATUS:\s*(\w+)'

"Using -Match operator"
if ($Content -Match $Pattern){
   $Matches[0]
   $Matches[1]
} Else {
  "couldn#T find a match"
}

"Using [RegEx] .Matches method"

$Matches = $Pattern.Matches($Content)

$Matches.Captures[0].Value
$Matches.Captures.Groups[1].Value

Your question and comment differ in the file content,
a space following the colon INSTALL_STATUS: SUCCESS.

Sample output :

Using -Match operator
INSTALL_STATUS: SUCCESS
SUCCESS
Using [RegEx] .Matches method
INSTALL_STATUS: SUCCESS
SUCCESS

Upvotes: 1

Related Questions