How to quickly reverse the order of numbers in a text editor?

Off-topic posts of interest to the "Everything" community.
Post Reply
Debugger
Posts: 630
Joined: Thu Jan 26, 2017 11:56 am

How to quickly reverse the order of numbers in a text editor?

Post by Debugger »

How to quickly reverse the order of numbers in a text editor?

Example:
22,34,50,15,27,74,29,25,77,6,54,40,38,73,9,67,57,70,58,60
therube
Posts: 4977
Joined: Thu Sep 03, 2009 6:48 pm

Re: How to quickly reverse the order of numbers in a text editor?

Post by therube »

(Without trying) I'd think something along the lines of, Reverse order of dot-delimited elements in a string, would do it.
Debugger
Posts: 630
Joined: Thu Jan 26, 2017 11:56 am

Re: How to quickly reverse the order of numbers in a text editor?

Post by Debugger »

This is Unix, I need for Windows, and certainly not commands and scripts, because it also takes time, identical to how I would manually change the order of numbers.
Debugger
Posts: 630
Joined: Thu Jan 26, 2017 11:56 am

Re: How to quickly reverse the order of numbers in a text editor?

Post by Debugger »

But all these scripts.... or online tools, of which there are hundreds, innumerable. But every one such app reverses numbers.
Probably it is not about "reverse" but about "inverse".

Reverse String Script - wrong

Inverse Order / Inverse Sequence - ???

Image
vsub
Posts: 474
Joined: Sat Nov 12, 2011 11:51 am

Re: How to quickly reverse the order of numbers in a text editor?

Post by vsub »

There is probably a better way with RegEx but I am not good with it
Autohotkey
I place those numbers in the clipboard when I run the script and then sort them

Code: Select all

Clipboard = 22,34,50,15,27,74,29,25,77,6,54,40,38,73,9,67,57,70,58,60
Loop,Parse,Clipboard,`,
List .= A_Index "|" A_LoopField "`n"
Sort,List,N R
Loop,Parse,List,`n
{
If A_LoopField =
{
Sorted := SubStr(Sorted,1,-1)
Break
}
Sorted .= SubStr(A_LoopField,InStr(A_LoopField,"|",,1,1)+1) ","
}
MsgBox,% Sorted
Clipboard := Sorted
exitapp
Debugger
Posts: 630
Joined: Thu Jan 26, 2017 11:56 am

Re: How to quickly reverse the order of numbers in a text editor?

Post by Debugger »

In this case, I would have to modify the script each time, adding numbers to the lines. And it would not be possible to just paste the numbers in the window, at any time and at any time, any numbers and click: run?
therube
Posts: 4977
Joined: Thu Sep 03, 2009 6:48 pm

Re: How to quickly reverse the order of numbers in a text editor?

Post by therube »

and then sort them
Should it be sorted or reversed?
22,34,50,15,27,74,29,25,77,6,54,40,38,73,9,67,57,70,58,60
60,58,70,57,67,9,73,38,40,54,6,77,25,29,74,27,15,50,34,22


(The missing post was a spammer.)
vsub
Posts: 474
Joined: Sat Nov 12, 2011 11:51 am

Re: How to quickly reverse the order of numbers in a text editor?

Post by vsub »

By "and then sort them" I mean,it reverses the order...last is first

Debugger you don't need to edit the code every time,this was just an example
1.Remove the first line
2.Copy the numbers in your text editor and then run the scrips

There are man ways to make it work,I just don't know what you want to do with the values after you rearrange them
For example with this if you copy the values and then Press Shift+F1(the hotkey can be different),the script will rearrange the values and replace them

Code: Select all

+F1::
Keywait,Shift
Send,^c
ClipWait,1
Loop,Parse,Clipboard,`,
List .= A_Index "|" A_LoopField "`n"
Sort,List,N R
Loop,Parse,List,`n
{
If A_LoopField =
{
Sorted := SubStr(Sorted,1,-1)
Break
}
Sorted .= SubStr(A_LoopField,InStr(A_LoopField,"|",,1,1)+1) ","
}
Clipboard := Sorted
Send,^v
List =
Sorted =
Return
Debugger
Posts: 630
Joined: Thu Jan 26, 2017 11:56 am

Re: How to quickly reverse the order of numbers in a text editor?

Post by Debugger »

You are not serious, you give some code, and you think that the other person will understand how to use it. Master, give me some corrections. Do something.
vsub
Posts: 474
Joined: Sat Nov 12, 2011 11:51 am

Re: How to quickly reverse the order of numbers in a text editor?

Post by vsub »

I am serious but you have to follow the thread
After I posted my first example,you said
In this case, I would have to modify the script each time, adding numbers to the lines. And it would not be possible to just paste the numbers in the window, at any time and at any time, any numbers and click: run?
And then I reply to that with what you have to do to be able to make the code work the way you want.
Debugger you don't need to edit the code every time,this was just an example
1.Remove the first line
2.Copy the numbers in your text editor and then run the scrips
Then I posted another code that allows you to execute that reordering and automatically replace the selected

If you want directions
First code:
1.Remove the file line and save the file
2.Select your numbers
3.Run the script
4.When the message box appear and when you hit ok,the rearranged digits will be placed into the clipboard which you can paste wherever you want

Second code:
1.Run the script(this example don't need the script to be started every time,when you start this once,it will always run and wait for you to press Shift+F1
2.Select your digits
3.Without deselecting them(clicking somewhere else),press Shift+F1 and release the buttons to run the code which will rearrange them and replace what you have selected

There are a lot of other ways to make it easier to run the script
therube
Posts: 4977
Joined: Thu Sep 03, 2009 6:48 pm

Re: How to quickly reverse the order of numbers in a text editor?

Post by therube »

reverse.bat:

Code: Select all

@echo off
sed  -e "s/,/\r\n/g"  |  tac  |  sed -e "s/\r//"  |  paste -sd,
file.txt:

Code: Select all

1,2,3,4
gvim file.txt
!!reverse.bat

results:

Code: Select all

4,3,2,1
therube
Posts: 4977
Joined: Thu Sep 03, 2009 6:48 pm

Re: How to quickly reverse the order of numbers in a text editor?

Post by therube »

You'll need the Unix-like utilities.
(If you're using Win10, they may be there already?)


sed changes 1,2,3,4 to:

1
2
3
4

tac reverses those lines:

4
3
2
1

sed removes the <cr>

(may only be needed because of DOS using \r\n kind of thing?

paste

(joins things together using , as a separator)


For whatever reason, from a command prompt the output ends up like:

Code: Select all

4 ,3,2,1
called from within gvim you get:
4,3,2,1

Similarly you could do something like:

Code: Select all

reverse.bat  <  file.txt
or
reverse.bat  <  file.txt  >  file_reversed.txt
vsub
Posts: 474
Joined: Sat Nov 12, 2011 11:51 am

Re: How to quickly reverse the order of numbers in a text editor?

Post by vsub »

@therube I am not sure how to test this but is this actually working when the values are random,not sequential like 1,2,3,4
When they are ordered from smaller\bigger to bigger\smaller number,then there are plenty of ways to do it

Can your example reverse the order like my script and when the values are separated by coma,not new line?

22,34,50,15,27,74,29,25,77,6,54,40,38,73,9,67,57,70,58,60
to
60,58,70,57,67,9,73,38,40,54,6,77,25,29,74,27,15,50,34,22
therube
Posts: 4977
Joined: Thu Sep 03, 2009 6:48 pm

Re: How to quickly reverse the order of numbers in a text editor?

Post by therube »

Yes, working.

Code: Select all

C:\TMP\>cat file
22,34,50,15,27,74,29,25,77,6,54,40,38,73,9,67,57,70,58,60
C:\TMP\>reverse.bat  < file  > file.out

C:\TMP\>cat file.out
60,58,70,57,67,9,73,38,40,54,6,77,25,29,74,27,15,50,34,22

C:\TMP\>

(I forgot an @echo off in the above batch file, since edited.)


Can your example reverse the order like my script and when the values are separated by coma,not new line?
The source file (aka "file", in my example) is just as you show, a single line separated by commas.
I split the file at the commas, into separate lines, only because it makes it easier to parse (to reverse the order - that's what the tac command does -- tac is the reverse of cat, BTW).
After things are reversed, I then recombine them from individual "lines" back into a single line, separated by commas.
(All this is done in a stream.)


So long as your editor can make a system call... In Vim !!reverse.bat does that (for the current line, the line you want reversed).
therube
Posts: 4977
Joined: Thu Sep 03, 2009 6:48 pm

Re: How to quickly reverse the order of numbers in a text editor?

Post by therube »

Code: Select all

C:\TMP\>cat > cat
red,cat,blue,cat,one,cat,two,cat
^Z

C:\TMP\>cat cat
red,cat,blue,cat,one,cat,two,cat

C:\TMP\>reverse < cat  > cat.out

C:\TMP\>cat cat.out
cat,two,cat,one,cat,blue,cat,edr

C:\TMP\>
Look at "edr"!
I have no idea how or why that happened?
(I'm on XP currently. Was on Win7 earlier. And see differences between the two.)


cat is like type.
epyt, if it existed, would be like tac ;-).
Debugger
Posts: 630
Joined: Thu Jan 26, 2017 11:56 am

Re: How to quickly reverse the order of numbers in a text editor?

Post by Debugger »

I know how to turn on and run a script in PSPad 32-bit 5, but I am looking for information on how to do it in EmEditor 64-bit 18.9.10?
Post Reply