I have a header section at the beginning of a text file test.txt:
BLOGS-BODGER
SMALLTOWN COMPOSITE ROLL
PAGE 6
PAGE 7
SMALLTOWN COMPOSITE ROLL
BOOMER-BYGRAVE
I am extracting data from this header in the following way:
$filesPathText = "C:\test\test3\test.txt"
# find NAME-NAME strings in text file test.txt
$names = Select-String -Path $filesPathText -Pattern '[A-Z]-[A-Z]'
Write-Host "First name" $names[0] "Second name" $names[1]
# find PAGE X strings in text file test.txt
$pages = Select-String -Path $filesPathText -Pattern 'PAGE'
Write-Host "First page" $pages[0] "Second page" $pages[1]
The output is the following:
First name C:\test\test3\test.txt:3:BLOGS-BODGER Second name C:\test\test3\test.txt:8:BOOMER-BYGRAVE
First page C:\test\test3\test.txt:5:PAGE 6 Second page C:\test\test3\test.txt:6:PAGE 7
I can access line numbers in the following way:
[int]$lineNum = $names[1].LineNumber
So how do I do something similar for the NAME & PAGE values. That is to assign NAME & PAGE data to a variable by eliminating the path & line number data?
This doc is close Example 3: Find a pattern match but doesn't go into parsing out individual elements. This SO post Get a line number on Powershell? has a references to piping output to Select-Object & Expanding Properties??
Any suggestions/explanations would be appreciated.
.Lineproperty of the returned object.Lineproperty you have mentioned.LineNumber.name, location, pageand thenpage, location, name. that is horribly difficult to parse since it has no regular structure..Linewith.LineNumberIf you had provided this as an answer with explanation I could have upvoted. Thank youGet-Command convertfrom-string*<<<