I'm trying to add regex to the beginning of each line in a text file if it isn't already present. The input is a list of urls and the regex is a list of protocols.
I've tried different if/else loops and cmdlets like add-content/set-content to add the regex but the logic is off each time. Currently what I have is:
$content = Get-Content "C:\path\to\file\test.txt"
$pattern = "[regex]::^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/|tcp:\/\/|ssl:\/\/)"
ForEach-Object {
if ($content -match $pattern)
{$content}
else {
{foreach($_ in $content) {"^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/|tcp:\/\/|ssl:\/\/)" + $_ }}
}
} | Out-File "C:\path\to\file\test.txt"
I'd like the output to be
^(http://www.|https://www.|http://|https://|tcp://|ssl://)netflix.com ^(http://www.|https://www.|http://|https://|tcp://|ssl://)google.com ^(http://www.|https://www.|http://|https://|tcp://|ssl://)yahoo.com
but the actual output is
"^(http://www.|https://www.|http://|https://|tcp://|ssl://)" + $_
Aucun commentaire:
Enregistrer un commentaire