Extract String PowerShell

Hi,

I need to extract a particular string from a line in a text file

Say for example, I have the following line in a text file

ABC3007E 02072015 01:36:49 <11.10.14.10    /492C>   Zumb Client    --! STOPPING CURRENT PATH OF OBJECT RESOLUTION

I need to get only the following text - "--! STOPPING CURRENT PATH OF OBJECT RESOLUTION". Maybe anything that starts with --!. 

RegEx can be used here ?

July 3rd, 2015 12:37am

Yes, you can use -replace to do this, so this should do what you're after, with the resulting $short string containing your desired text :

$string="ABC3007E 02072015 01:36:49 <11.10.14.10    /492C>   Zumb Client    --! STOPPING CURRENT PATH OF OBJECT RESOLUTION"
$short = $string -replace ".*! "
$short
The .* tells it to replace anything up to the characters following it. If you wanted to remove any text AFTER it you'd use *. instead, so for instance if you wanted to keep everything except that text then you'd use "--!*."

Free Windows Admin Tool Kit Click here and download it now
July 3rd, 2015 1:53am

Hi Keith,

Thanks and that worked like a charm. 

so if i need ABC3007E, 11.10.14.10 and the string after --! then can i do that in a single replace command?

Regards,

Vinod

July 3rd, 2015 2:07am

$line = 'ABC3007E 02072015 01:36:49 <11.10.14.10    /492C>   Zumb Client    --! STOPPING CURRENT PATH OF OBJECT RESOLUTION'

# Get everything from --! to the end of the line
($line | Select-String -Pattern '--!.*$' -List).Matches.Value

# Get everything after --! to the end of the line
($line -split '--! ')[1]

Free Windows Admin Tool Kit Click here and download it now
July 3rd, 2015 2:08am

You can do multiple -replace statements within a single command, so for instance if you just wanted the client name from the middle you'd use :

$string="ABC3007E 02072015 01:36:49 <11.10.14.10    /492C>   Zumb Client    --! STOPPING CURRENT PATH OF OBJECT RESOLUTION"
$short = $string -replace "--!.*" -replace ".*>   "
$short

However in those cases it's nice and simple since you have definitive characters/strings to find and remove everything before or after. Just getting "ABC3007E, 11.10.14.10 and the string after --! " would be harder, since the other text will obviously change so you'd need to look at replacing the number of characters, and some of those strings are presumably variable length.

You'd probably need to experiment with .SubString to get the bits you wanted, and potentially pull out the required elements and then put them back together in the format wanted.

May be worth checking out this post http://www.lazywinadmin.com/2013/10/powershell-get-substring-out-of-string.html which gives some good examples of the kind of things you can do with strings, and the multiple ways you can approach it depending on personal taste.

July 3rd, 2015 2:22am

$line = 'ABC3007E 02072015 01:36:49 <11.10.14.10    /492C>   Zumb Client    --! STOPPING CURRENT PATH OF OBJECT RESOLUTION'

# Get everything from --! to the end of the line
($line | Select-String -Pattern '--!.*$' -List).Matches.Value

# Get everything after --! to the end of the line
($line -split '--! ')[1]

# Search using regex
$results = $line | Select-String -Pattern '(\w{8}).*(\d{2}\.\d{2}\.\d{2}\.\d{2}).*--!\s(.*)' -List

# ABC3007E
$results.Matches.Groups[1].Value

# 11.10.14.10
$results.Matches.Groups[2].Value

# STOPPING CURRENT PATH OF OBJECT RESOLUTION
$results.Matches.Groups[3].Value

Free Windows Admin Tool Kit Click here and download it now
July 3rd, 2015 2:24am

Regular Expression Language - Quick Reference
July 3rd, 2015 2:49am

Another option:

if ( $line -match '(\w{8}).*(\d{2}\.\d{2}\.\d{2}\.\d{2}).*--!\s(.*)' ) {

    # ABC3007E
    $Matches[1]

    # 11.10.14.10
    $Matches[2]

    # STOPPING CURRENT PATH OF OBJECT RESOLUTION
    $Matches[3]

}

Free Windows Admin Tool Kit Click here and download it now
July 3rd, 2015 2:55am

Thanks Keith and Leif-Arne.

This has really worked for me. It's really a very great learning experience for me

so for me the next step is doing the same thing in bunch of log files and getting the output. So thinking of using something like this

$a= gc d:\gdt\zumba\log\nvd*.log

$line = ($a | Select-String -Pattern '--!.*$' -List).Matches.Value

$o = ($line -split '--! ')[1]

$p = ($line -split ' ' -split '<')[4]

$s = $p+$o | out-file a.txt

Regards,

Vinod

July 3rd, 2015 2:55am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics