Welcome › Forums › General PowerShell Q&A › Flip words in textfile OR make it work anyway?
- This topic has 9 replies, 5 voices, and was last updated 1 month, 1 week ago by
Participant.
-
AuthorPosts
-
-
December 9, 2020 at 8:16 am #277968
1.) Is there a way to flip words from a textfile.txt ?
From: To: Ärna:Tänka
Ärnade:Tänkte
Ärnar:TänkerTänka:Ärna
Tänkte:Ärnade
Tänker:ÄrnarI 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 -
December 9, 2020 at 8:43 am #277974
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 -
December 9, 2020 at 8:53 am #277977
Can be done on one line but I’ve split it at the | for readability on the forum.
PowerShell123Get-Content E:\Temp\dictionary.txt |foreach {$_ -replace $_,(($_ -split ':')[1,0] -join ':') |Out-File E:\Temp\reversedDictionary.txt -Append}-
This reply was modified 1 month, 1 week ago by
Matt Bloomfield. Reason: Formatting
-
This reply was modified 1 month, 1 week ago by
-
December 9, 2020 at 10:20 am #278043
Thank you, that did the trick.
-
December 9, 2020 at 10:21 am #278049PowerShell12345$result = Get-Content .\name.txt | ForEach-Object {$name = $_ -split ':'"{0}:{1}" -f $name[1],$name[0]}Add-Content -Path .\newname.txt -Value $result
-
December 9, 2020 at 10:29 am #278058
Another method is using Reverse:
PowerShell123456789101112131415$names = @"Ärna:TänkaÄrnade:TänkteÄrnar:Tänker"@ -split [environment]::NewLineforeach ($name in $names) {$arr = $name -split ":"[array]::Reverse($arr)[pscustomobject]@{'OriginalName' = $name'ReverseName' = $arr -join ':'}}Output:
PowerShell12345OriginalName ReverseName------------ -----------Ärna:Tänka Tänka:ÄrnaÄrnade:Tänkte Tänkte:ÄrnadeÄrnar:Tänker Tänker:Ärnar-
This reply was modified 1 month, 1 week ago by
Rob Simmers.
-
This reply was modified 1 month, 1 week ago by
-
December 9, 2020 at 11:15 am #278103
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)
-
December 9, 2020 at 11:22 am #278115
Glad the samples are working as expected. As you did before, provide examples of the text files and expected outcome.
-
December 9, 2020 at 12:06 pm #278148
Using -replace, you can do the following:
PowerShell12(Get-Content file.txt -encoding utf8) -replace '^([^:]+):(.*)$','$2:$1' |Set-Content file.txt -encoding UTF8 -
December 10, 2020 at 12:51 am #278265
Glad the samples are working as expected. As you did before, provide examples of the text files and expected outcome.
(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!
-
-
AuthorPosts
- You must be logged in to reply to this topic.