0

I have several single line xml files out of which I want to extract the contents of a particular tag <LIFNR>. I managed doing that using the following two lines of power shell:

get-content P:\Webservice\21*_*CR_d*.xml | foreach-object { $_ -replace '>', ">`r`n" } > xmls_newline.txt
get-content .\xmls_newline.txt | Select-String -Pattern '.*<\/LIFNR>'

However if I want to skip the step of creating an intermediary text file I cannot achieve the same result.

$xml = get-content P:\Webservice\21*_*CR_d*.xml | foreach-object { $_ -replace '>', ">`r`n" }
$xml | Select-String -Pattern '.*<\/LIFNR>'

or

get-content P:\Webservice\21*_*CR_d*.xml | foreach-object { $_ -replace '>', ">`r`n" } | Select-String -Pattern '.*<\/LIFNR>'

each just print the result of the string splitting in the foreach-object statement, the Select-String command is ignored.

Can somebody explain to me what is different about the latter two attempts compared to the working solution?

2
  • Why not parse the file as XML? Commented Dec 14, 2021 at 9:54
  • A valid concern, but for one thing I don't know how and - more importantly - I want to know the answer to this exact question. This being an XML is of no particular importance, this could be any text file. Commented Dec 14, 2021 at 10:03

1 Answer 1

1

Get-Content outputs each line as a separate string. To emulate the same behavior, split the string after > instead of appending a linebreak:

Get-Content P:\Webservice\21*_*CR_d*.xml |ForEach-Object { $_ -split '(?<=>)' } | Select-String -Pattern '.*<\/LIFNR>'

The construct (?<=...) is a lookbehind assertion - this way, PowerShell will split on a zero-length string immediately after >

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your help, it works perfectly! Just for clarification so I really get this: The -replace leaves the XML input still in a single one line string which is then piped to Select-String which matches the first occurence and returns the entire line of that occurence, hence the entire string. Saving and retrieving from a file "splits" the string because of the newlines and thus Select-String is fed single lines. U doing the split is basically doing the same?

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.