Author Posts

August 7, 2017 at 5:41 pm

I tried to extract conditional strings using powershell as per below example.
Both $regex1 and $regex 2, i am able to get as a two text files. But i want output as pep OUTPUT file format.
But dont know what mistake i am making here. I new to powershell please help.

INPUT FILE(all.txt)

temp1.txt: file not found \xyz\ not found \123\
temp2.txt: text \ABC\ is here
temp2.txt: NUM \999\ yes \FIRST\

OUTPUT FILE(all-pts.txt)

temp1.txt \xyz\
temp1.txt \123\
temp2.txt \ABC\
temp2.txt \999\
temp2.txt \FIRST\

$input_path = "E:\DOC\all.txt"
$output_file = "E:\DOC\all-pts.txt"
(Get-Content "$input_path") | ForEach-Object
{if($_ -like '[ \b\t\n\r]+\\+[A-Z0-9_-]+\\$')

{
$regex1 = '[ \b\t\n\r]+\\+[A-Z0-9_-]+\\$'
$regex2 = '[ A-Z0-9_-]+\.txt$'
select-string -Path $input_path -Pattern $regex1 -AllMatches | %
{ $_.Matches } | % { $_.Value } > $output_file
select-string -Path $input_path -Pattern $regex2 -AllMatches | %
{ $_.Matches } | % { $_.Value } > $output_file
}
}

August 7, 2017 at 6:07 pm

Aside from continuing the line at the foreach instead of the { ?

Would this site help? https://regex101.com/

August 7, 2017 at 10:52 pm

You could do something like this

cls
$input = @'
temp1.txt: file not found \xyz\ not found \123\
temp2.txt: text \ABC\ is here
temp2.txt: NUM \999\ yes \FIRST\
'@ -split "`r`n"

$input | ForEach-Object {
    $file = ($_ | Select-String -Pattern "^(.*?):").Matches.Groups[1].Value
    ($_ | Select-String -Pattern "\\.*?\\" -AllMatches).Matches.value | ForEach-Object {
        [pscustomobject]@{
            File = $file
            Path = $_
        }
    }
}

Results:

File      Path   
----      ----   
temp1.txt \xyz\  
temp1.txt \123\  
temp2.txt \ABC\  
temp2.txt \999\  
temp2.txt \FIRST\

August 9, 2017 at 2:21 pm

hi,
i have tried to copy the results in a file, below are the code. i have used "get content" to copy the output as text file but i cant make generate a text file.

$input = "E:\to be del q\SEQ\MILL-A-SEQ\all.txt"
$output_file = "E:\to be del q\SEQ\MILL-A-SEQ\del.txt"
Get-Content $input | ForEach-Object {

$file = ($_ | Select-String -Pattern "^(.*?):").Matches.Groups[1].Value

($_ | Select-String -Pattern "\\.*?\\" -AllMatches).Matches.value | ForEach-Object {

[pscustomobject]@{
File = $file
Path = $_
}
}
(Get-Content "[pscustomobject]") | sort > "$output_file"
}

August 11, 2017 at 3:59 am

You should use the pre tags to format your code so it is readable.

Get-Content is for getting the content of an item, not a variable. Additionally [pscustomobject] is not a variable, it is a way to create a custom powershell object.

The result of this code

$input = "D:\Temp\all.txt"
$output_file = "D:\Temp\del.txt"
Get-Content $input |
 ForEach-Object {
    $file = ($_ | Select-String -Pattern "^(.*?):").Matches.Groups[1].Value
    ($_ | Select-String -Pattern "\\.*?\\" -AllMatches).Matches.value |
     ForEach-Object {
        [pscustomobject]@{
            File = $file
            Path = $_
        }
     }
}

Is custom object being generated that contain the data you desire. Those object are sent to the pipeline and then take the Out-Default when the pipeline end with no other out alternative provide. If you want to output those object to a file, you just specify that in the pipeline.

For Example to output to a text file

$input = "D:\Temp\all.txt"
$output_file = "D:\Temp\del.txt"
Get-Content $input |
 ForEach-Object {
    $file = ($_ | Select-String -Pattern "^(.*?):").Matches.Groups[1].Value
    ($_ | Select-String -Pattern "\\.*?\\" -AllMatches).Matches.value |
     ForEach-Object {
        [pscustomobject]@{
            File = $file
            Path = $_
        }
     }
} | Out-File -FilePath $output_file

Or to output to a CSV file

$input = "D:\Temp\all.txt"
$output_file = "D:\Temp\del.txt"
Get-Content $input |
 ForEach-Object {
    $file = ($_ | Select-String -Pattern "^(.*?):").Matches.Groups[1].Value
    ($_ | Select-String -Pattern "\\.*?\\" -AllMatches).Matches.value |
     ForEach-Object {
        [pscustomobject]@{
            File = $file
            Path = $_
        }
     }
} | Export-Csv -Path $output_file -NoTypeInformation