Reputation: 57
Can anyone assist me in filtering the banner message and extract only the text "13:12:03.539 UTC Sun Mar 6 2015" as output?
C -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- S Y S T E M A C C E S S W A R N I N G -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- You have connected to a private network. This system is to only be used by authorized personnel for authorized business purposes. All activity is monitored and logged. Unauthorized access or activity is a violation of law. If you have connected to this system by mistake, disconnect now. 13:12:03.539 UTC Sun Mar 6 2015 C -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- S Y S T E M A C C E S S W A R N I N G -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- You have connected to a private network. This system is to only be used by authorized personnel for authorized business purposes. All activity is monitored and logged. Unauthorized access or activity is a violation of law. If you have connected to this system by mistake, disconnect now. Line User Host(s) Idle Location *514 vty 0 vijay.dada idle 00:00:01 X.X.X.X Interface User Mode Idle Peer Address C -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- S Y S T E M A C C E S S W A R N I N G -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- You have connected to a private network. This system is to only be used by authorized personnel for authorized business purposes. All activity is monitored and logged. Unauthorized access or activity is a violation of law. If you have connected to this system by mistake, disconnect now.
I tried using the below code unable to make it work.Request to assist.
$arr = @()
$path = "yourFilePath"
$pattern = "(?<=.*C
-)\w+?(?=disconnect now.*)"
Get-Content $path | Foreach {
if ([Regex]::IsMatch($_, $pattern)) {
$arr += [Regex]::Match($_, $pattern)
}
}
$arr | Foreach {$_.Value}
Upvotes: 1
Views: 2738
Reputation: 200493
Simply grep the line you're looking for:
$path = 'C:\path\to\your.txt'
$pattern = '^\d{2}:\d{2}:\d{2}\.\d{3} .* \w{3} \d{1,2} \d{4}$'
(Get-Content $path) -match $pattern
For removing what you don't want instead of selecting what you want you need to convert the text to a single string (by default Get-Content
produces an array of strings and multiline matches don't work with that kind of input) and then replace the banner in that string, e.g. like this:
$path = 'C:\path\to\your.txt'
$banner = @'
C
-=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=-
S Y S T E M A C C E S S W A R N I N G
-=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=- -=*=-
You have connected to a private network. This system is to only be used
by authorized personnel for authorized business purposes. All activity
is monitored and logged. Unauthorized access or activity is a violation
of law.
If you have connected to this system by mistake, disconnect now.
'@ -replace "`r?`n", "`r`n"
((Get-Content $path | Out-String) -replace [regex]::Escape($banner)).Trim()
The trailing replacement in the banner definition is to ensure that the banner string has line breaks encoded as CR-LF, regardless of whether you run the code from a script or by copy/pasting it in a PowerShell console.
Upvotes: 4