Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

4 Answers 4

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

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

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

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.