Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question

migrated from stackoverflow.com Dec 12 '12 at 21:53

This question came from our site for professional and enthusiast programmers.

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

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

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
share|improve this answer

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
} 
share|improve this answer

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
share|improve this answer

You must log in to answer this question.

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