Combine multiple Get-Content cmdlet in powershell

Viewed 134

I am a beginner to PowerShell and would like to use it to perform an automated file editing. Below is my current work to make a "OR" delimited string:

$inputFile = "C:\Users\David Kao\Desktop\Powershell\Input\input.txt"
$outputFile = "C:\Users\David Kao\Desktop\Powershell\Output\output.txt"
$final = "C:\Users\David Kao\Desktop\Powershell\Output\final.txt"
(Get-Content $inputFile ) -replace ' \[.*','' -replace ' \(.*','' -replace ';','' -replace ',','' -replace '- ',''|
    Where-Object { $_ -notmatch '[^\p{IsBasicLatin}]' }|
    Sort-Object -Unique |
    Set-Content $outputFile
    
(Get-Content $outputFile) -join '/' -replace '/','" OR "' -replace '\t" OR ', '' -replace '$', '"'|
    Set-Content $final

Here is the sample data:

multiple sclerosis [A0484253/AOD/DE/0000006106]
ms [A1145632/BI/AB/BI00548]
multiple sclerosis [A0484254/BI/PT/BI00548]
    
MS [A0432904/CCPSS/PT/0056346]
    
MULTIPLE SCLEROSIS [A0433042/CCPSS/PT/0037395]
    
Multiple sclerosis [A0436411/CCS/MD/6.2.2]
    
Multiple sclerosis [A0436412/CCS/SD/80]
    
Multiple sclerosis [A31482484/CCSR_10/SD/NVS005]
    
disseminated sclerosis [A18685620/CHV/SY/0000008328]
    
insular sclerosis [A18685621/CHV/SY/0000008328]
    
MS [A18592794/CHV/SY/0000008328]
    
MS multiple sclerosis [A18685622/CHV/SY/0000008328]
    
multiple sclerosis [A18611430/CHV/SY/0000008328]
    
multiple sclerosis (MS) [A18555705/CHV/PT/0000008328]
    
multiple sclerosis MS [A18574147/CHV/SY/0000008328]

And the output would be something like:

"multiple scelorosis" OR "MS" OR "insular sclerosis" OR ....

So far as now, this code works well, but what I would like to achieve is to get rid of the second section and put it into the first section to make it more concise, efficient and professional. Something like this:

$inputFile = "C:\Users\helloworld\Desktop\Powershell\Input\input.txt"
$outputFile = "C:\Users\helloworld\Desktop\Powershell\Output\output.txt"

(Get-Content $inputFile ) -replace ' \[.*','' -replace ' \(.*','' -replace ';','' -replace ',','' -replace '- ',''|
    Where-Object { $_ -notmatch '[^\p{IsBasicLatin}]' }|
    Sort-Object -Unique |
    (Get-Content $_) -join '/' -replace '/','" OR "' -replace '\t" OR ', '' -replace '$', '"'|
    Set-Content $outputFile

I have googled a lot about this issue but got stuck for a while. Can anyone help me out? Thanks!


I am now adding ForEach-Object:

$inputFile = "C:\Users\David Kao\Desktop\Powershell\Input\input.txt"
$outputFile = "C:\Users\David Kao\Desktop\Powershell\Output\output.txt"

(Get-Content $inputFile ) -replace ' \[.*','' -replace ' \(.*','' -replace ';','' -replace ',','' -replace '- ',''|
    Where-Object { $_ -notmatch '[^\p{IsBasicLatin}]' }|
    Sort-Object -Unique |
    ForEach-Object { $_ -join '/' -replace '/','" OR "' -replace '\t" OR ', '' -replace '$', '"'}|
    Set-Content $outputFile

But it seems that join does not work it ForEach-Object, if I can fix this, then I think everything would be fine.

2 Answers

I would do the following, which assumes all spaces are a single space and names will only contain alpha characters and spaces.

$inputfile = Get-Content inputfile.txt
($inputfile -replace '(^[a-z][a-z ]*) [^a-z].*$','"$1"' -ne '' |
    sort -unique) -join ' OR ' | Set-Content output.txt

See Regex for matching explanation. Capture group 1 is everything matched within the first () grouping. It is substituted in the replace string as $1.

-ne '' is to remove solitary blank lines. If there are other spaces on those lines you may need an additional -notmatch '^\s*$'.

If you have a custom definition of uniqueness, then the code will need to be altered slightly rather than doing just sort -unique.

here's another way to do this ... [grin]

what it does ...

  • creates a set of sample data to work with
    when ready to work with real data, replace the entire #region/#endregion block with a Get-Content call.
  • splits the lines on the open [
  • filters out the lines that have a closing ']'
    that gets rid of the unwanted remainder from the split.
  • filters out any lines that contain 2-or-more consecutive spaces
    this gets rid of those odd lines that have just 4 spaces on them.
  • trims away any leading or trailing spaces
  • sorts the items and removes any exact dupes
  • joins the items with double-quoted OR strings
  • assigns that to $Result
  • adds the required leading and trailing double quotes to the previous string
  • assigns that to $FinalResult
  • displays that last item on screen

the code ...

#region >>> fake reading in a plain text file
#    in real life, use Get-Content
$InStuff = @'
multiple sclerosis [A0484253/AOD/DE/0000006106]
ms [A1145632/BI/AB/BI00548]
multiple sclerosis [A0484254/BI/PT/BI00548]

MS [A0432904/CCPSS/PT/0056346]

MULTIPLE SCLEROSIS [A0433042/CCPSS/PT/0037395]

Multiple sclerosis [A0436411/CCS/MD/6.2.2]

Multiple sclerosis [A0436412/CCS/SD/80]

Multiple sclerosis [A31482484/CCSR_10/SD/NVS005]

disseminated sclerosis [A18685620/CHV/SY/0000008328]

insular sclerosis [A18685621/CHV/SY/0000008328]

MS [A18592794/CHV/SY/0000008328]

MS multiple sclerosis [A18685622/CHV/SY/0000008328]

multiple sclerosis [A18611430/CHV/SY/0000008328]

multiple sclerosis (MS) [A18555705/CHV/PT/0000008328]

multiple sclerosis MS [A18574147/CHV/SY/0000008328]
'@ -split [System.Environment]::NewLine
#endregion >>> fake reading in a plain text file

# split on the open "["
$Result = (($InStuff -split '\[').
    Where({
        # filter out the lines that have a closing "]"
        $_ -notmatch '\]' -and
        # filter out the lines that have two-or-more consecutive spaces
        $_ -notmatch '\s{2,}'
        }).
    # trim away any leading/trailing spaces
    Trim() |
    # sort the items and toss out any dupes.
    # does a preliminary join with double-quoted OR strings
    Sort-Object -Unique) -join '" OR "'

# adds leading and trailing double quotes
$FinalResult = '"{0}"' -f $Result

$FinalResult

output ...

"disseminated sclerosis" OR "insular sclerosis" OR "ms" OR "MS multiple sclerosis" OR "multiple sclerosis" OR "multiple sclerosis (MS)" OR "multiple sclerosis MS"
Related