# powershell regex text concat

**URL:** <https://forums.powershell.org/t/powershell-regex-text-concat/9150>\
**Category:** PowerShell Help\
**Created:** [August 7, 2017, 12:11pm UTC](https://forums.powershell.org/t/powershell-regex-text-concat/9150 "2017-08-07T12:11:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![arjun232](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@arjun232](https://forums.powershell.org/u/arjun232)\
**Post date:** [August 7, 2017, 12:11pm UTC](https://forums.powershell.org/t/powershell-regex-text-concat/9150/1 "2017-08-07T12:11:50Z")

</div>

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 } &gt; $output_file
            select-string -Path $input_path -Pattern $regex2 -AllMatches | % 
            { $_.Matches } | % { $_.Value } &gt; $output_file
            }
       }

```

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [August 7, 2017, 12:37pm UTC](https://forums.powershell.org/t/powershell-regex-text-concat/9150/2 "2017-08-07T12:37:15Z")

</div>

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

Would this site help? [https://regex101.com/](https://regex101.com/)

---

<div class="post-metadata">

**Author:** ![curtis-smith](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/curtis-smith/32/121_2.png) [@curtis-smith](https://forums.powershell.org/u/curtis-smith)\
**Post date:** [August 7, 2017, 5:22pm UTC](https://forums.powershell.org/t/powershell-regex-text-concat/9150/3 "2017-08-07T17:22:34Z")

</div>

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\
```

---

<div class="post-metadata">

**Author:** ![arjun232](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@arjun232](https://forums.powershell.org/u/arjun232)\
**Post date:** [August 9, 2017, 8:51am UTC](https://forums.powershell.org/t/powershell-regex-text-concat/9150/4 "2017-08-09T08:51:29Z")

</div>

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 &gt; "$output_file"

```

}

---

<div class="post-metadata">

**Author:** ![curtis-smith](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/curtis-smith/32/121_2.png) [@curtis-smith](https://forums.powershell.org/u/curtis-smith)\
**Post date:** [August 10, 2017, 10:29pm UTC](https://forums.powershell.org/t/powershell-regex-text-concat/9150/5 "2017-08-10T22:29:32Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:38pm UTC](https://forums.powershell.org/t/powershell-regex-text-concat/9150/6 "2024-05-16T20:38:23Z")

</div>


