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.