Home > Mobile >  How can I use ForEach-Object output for Select-String directly?
How can I use ForEach-Object output for Select-String directly?

Time:12-15

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?

CodePudding user response:

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 >

  • Related