1

I have a text file. The text file contain of this

Name(0x0)/Class(0x17,0x0)/Format(0x0,0xFFFF,0x0)

I want to get this string only Format(0x0,0xFFFF,0x0)

I try to split it and get the array number. BUT THE PROBLEM is this format text file sometime change. This string Format(0x0,0xFFFF,0x0) NOT always in the last array, and the total of array sometime different as well. like this.

Name(0x0)/Class(0x17,0x0)/Class2(0000,0000)/Format(0x0,0xFFFF,0x0)
or
Name(0x0)/Class(0x17,0x0)/Format(0x0,0xFFFF,0x0)/Class2(0000,0000)

My expectation, I can get this string Format(0x0,0xFFFF,0x0) , wherever it's place. Anyone can help me please. Thanks a lot.

$File = Get-Content .\PC.txt
$split = $File -split "/"
$found= $split[2]
$found
1

2 Answers 2

1

As iRon suggests, you can use Select-String with a regular expression as follows:

(Select-String 'Format\(.+?\)' .\PC.txt).Matches.Value

The .Matches.Value part extracts what the regular expression Format\(.+?\) matched from each selected line in file .\PC.txt, via the [Microsoft.PowerShell.Commands.MatchInfo] instances that Select-Object outputs.

A simpler solution will be possible once the -OnlyMatching switch gets implemented, which directly outputs only the matching parts of selected lines.

# Upcoming feature, not yet implemented as of PowerShell Core 7.0.0-preview.5
Select-String 'Format\(.+?\)' .\PC.txt -OnlyMatching
Sign up to request clarification or add additional context in comments.

Comments

0

I found this simple way as well

$File = Get-Content .\PC.txt
$split = $File -split "/"
$found = $split -like "*Format*"

it works for me.

Comments

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.