Flip words in textfile OR make it work anyway?

Welcome Forums General PowerShell Q&A Flip words in textfile OR make it work anyway?

Viewing 9 reply threads
  • Author
    Posts
    • #277968
      Participant
      Topics: 4
      Replies: 21
      Points: 77
      Rank: Member

      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

    • #277974
      Participant
      Topics: 4
      Replies: 21
      Points: 77
      Rank: Member

      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
    • #277977
      Participant
      Topics: 4
      Replies: 427
      Points: 756
      Helping Hand
      Rank: Major Contributor

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

       

      • This reply was modified 1 month, 1 week ago by Matt Bloomfield. Reason: Formatting
    • #278043
      Participant
      Topics: 4
      Replies: 21
      Points: 77
      Rank: Member

      Thank you, that did the trick.

    • #278049
      Participant
      Topics: 5
      Replies: 349
      Points: 632
      Helping Hand
      Rank: Major Contributor
    • #278058
      Participant
      Topics: 17
      Replies: 1951
      Points: 3,988
      Helping Hand
      Rank: Community Hero

      Another method is using Reverse:

      Output:

      • This reply was modified 1 month, 1 week ago by Rob Simmers.
    • #278103
      Participant
      Topics: 4
      Replies: 21
      Points: 77
      Rank: Member

      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)

    • #278115
      Participant
      Topics: 17
      Replies: 1951
      Points: 3,988
      Helping Hand
      Rank: Community Hero

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

    • #278148
      Participant
      Topics: 1
      Replies: 96
      Points: 441
      Helping Hand
      Rank: Contributor

      Using -replace, you can do the following:

    • #278265
      Participant
      Topics: 4
      Replies: 21
      Points: 77
      Rank: Member

      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!

       

Viewing 9 reply threads
  • You must be logged in to reply to this topic.