Flip words in textfile OR make it work anyway?

1.) Is there a way to flip words from a textfile.txt ?

From: To:
Ärna:Tänka Ärnade:Tänkte Ärnar:Tänker Tänka:Ärna Tänkte:Ärnade Tänker:Ärnar
I use this in the script, but I want to make it able to revert what was made, I did flip "$find(?=\W)", $replace" to "$replace(?=\W)", $find", but it slows down in the way (its about 3000 words its checking) and does sometimes crash and gives wierd output.

2.) Or is it possible to solve it some other way to get this to work without flipping the words in the textfile.txt?

$ord = Get-Content $Path\textfile.xml -encoding utf8
Get-Content $Path\textfile.xml -encoding utf8 | foreach {
$find, $replace = $_ -split ‘:’
((Get-Content $FileBrowser.FileName -encoding utf8) -creplace “$find(?=\W)”, $replace) | Set-Content $FileBrowser.FileName -encoding utf8

I know “[array]::Reverse()” maybe used? But I can’t figure out how.

But how do I make the split between the ‘:’ ? Also some have two words after the ‘:’ otherwise it’s just this i got. And it doe not what I want.

$words = Get-Content -Path c:\textfile.txt
$results = foreach ($word in $words){$name -replace '(\w+) (\w+)','$2 $1'}
$results | Out-File c:\done.txt

Can be done on one line but I’ve split it at the | for readability on the forum.

Get-Content E:\Temp\dictionary.txt | 
    foreach {$_ -replace $_,(($_ -split ':')[1,0] -join ':') |
        Out-File E:\Temp\reversedDictionary.txt -Append}

 

Thank you, that did the trick.

$result = Get-Content .\name.txt | ForEach-Object {
        $name = $_ -split ':'
        "{0}:{1}" -f $name[1],$name[0]
}
Add-Content -Path .\newname.txt -Value $result

Another method is using Reverse:

$names = @"
Ärna:Tänka
Ärnade:Tänkte
Ärnar:Tänker
"@ -split [environment]::NewLine

foreach ($name in $names) {
    $arr = $name -split ":"
    [array]::Reverse($arr)

    [pscustomobject]@{
       'OriginalName'  = $name
       'ReverseName'   = $arr -join ':'
    }
}

Output:

OriginalName  ReverseName
------------  -----------
Ärna:Tänka    Tänka:Ärna
Ärnade:Tänkte Tänkte:Ärnade
Ärnar:Tänker  Tänker:Ärnar

Thank you all for the answers! I have tried all they do the work!

For the other question:

I do replace the first word with the second word from the textfile in another textfile that contains a mixture of many words. Is it possible to do replace the second word with the first word in the textfile? I did put up a code I use but it does not work so good, crashes and does weird things with the textfile it’s replacing word in.

(It’s related watching this two alternatives, i can’t figure t out completely why its buggy and does not work that way, cause it works with the first word and replacing with the other)

Glad the samples are working as expected. As you did before, provide examples of the text files and expected outcome.

Using -replace, you can do the following:

(Get-Content file.txt -encoding utf8) -replace '^([^:]+):(.*)$','$2:$1' |
    Set-Content file.txt -encoding UTF8

[quote quote=278115]Glad the samples are working as expected. As you did before, provide examples of the text files and expected outcome.

[/quote]

(I solved it, read under the picture…)

I did try a txt-file used only 1 word. and it ended up just after a few seconds to a gigantic text-file of 284 mb and it didn’t make all. And the output is really hard to open. But something is wrong. The word shown here is not even in the text-file.

However, After a good night sleep and woken fresh like a cucumber I did find out that I didn’t do a # to inactivate the line used before so I guesse it had some problem with that. So now everything is working, pheuw!

Thank you all for your help!