0

in a txt file I have this line:

<em:version>0.0.0.2</em:version>

Which cmd (dos or powershell) can I use fetch this?

0.0.0.2

4 Answers 4

1
PS> '<em:version>0.0.0.2</em:version>' -replace '<[^>]+>'
0.0.0.2

# replace file content
(Get-Content file.txt) -replace '<[^>]+>'
4
  • works. But how can I spcify the file itself now i'm runnning the cmd at a folder's level
    – Elad Benda
    Dec 12, 2012 at 10:53
  • I edited the thread, give it a try
    – Shay Levy
    Dec 12, 2012 at 12:34
  • 1
    That will only work if there is a single line of text in the file!
    – dangph
    Dec 12, 2012 at 22:10
  • Works for me just fine with a multi line text file.
    – Shay Levy
    Dec 13, 2012 at 6:50
4

PowerShell can parse XML directly. No need for regular expressions. Here's an example with a FireFox install.rdf file. (I'm guessing your file might be something like that.)

PS> [xml]$rdf = Get-Content .\install.rdf
PS> $rdf.RDF.Description.version
1.0
1

try this

$fileContent=gc "path to text file"

$pattern =  '(?i)<em:version[^>]*>(.*)</em:version>'


$result = [Regex]::Matches($fileContent, $pattern)

$result | ForEach-Object {
  $_.Groups[1].Value
} 
0

How about this? A one line DOS command:

for /f "usebackq tokens=2 delims=<>" %%A in (`type "File.txt" ^| find "<em:version>"`) do echo.%%A

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .