Sublime Forum

How to cut and sort text in Sublime?

#1

Using ST3 on Windows 10, I have a text file which contains hundreds of my kids first and second events. An excerpt is shown below. What I’m trying to accomplish is a cut and sort operation, so the text below becomes this:

bernie3_second_time_point_pleasant
bernie3_first_time_virtual_reality_goggles

I do something similar with a linux shell script which uses cut and paste like this:

while read i; do basename “$i” | cut -d ‘.’ -f1 | cut -d ‘-’ -f1 ; done | sort -u

Can someone tell me if this is possible in ST3?

TEXT FILE:
bernie3_second_time_point_pleasant-5
bernie3_second_time_point_pleasant-4
bernie3_second_time_point_pleasant-3
bernie3_second_time_point_pleasant-2
bernie3_second_time_point_pleasant-1
bernie3_first_time_virtual_reality_goggles-01-12
bernie3_first_time_virtual_reality_goggles-01-11
bernie3_first_time_virtual_reality_goggles-01-10
bernie3_first_time_virtual_reality_goggles-01-9
bernie3_first_time_virtual_reality_goggles-01

0 Likes

#2

Oh boy… https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet

For your problem, I’m not an expert in bash, so I’m not sure I understand your right.

Did you try a regex to remove the -x and f9 (Edit -> Sort Lines) ?

0 Likes

#3

When you ask “Did you try a regex”, try it where? How do you use a regex?

-Thanks

0 Likes

#4

Generically speaking there is no cut functionality similar to what the *nix command provides, but for your purposes you can use a regex in the Find > Replace... panel to do something similar.

A way to get the effect of splitting the line into fields at each - character is to craft a regex that matches the entire line but also captures everything up to (but not including) the - character itself, then using the capture as the replacement.

That will throw away the parts of the line that you want. From there you can use the built in functionality of Sublime to sort the lines and unique them (although you have to do it in two steps).

Here’s an example of that based on the sample text you provided above. This also shows making sure that regex mode is turned on in the panel and that highlight matches is enabled, allowing you to check that the regex is matching what you expect.

I used the menus for everything instead of keyboard shortcuts to highlight the options used, but I did use the command palette to get to the permute and sort options, since the menu was taller than the area that I was recording.

Also, it’s not neccesary to accidentally insert an & character at the end of the regex; it will work if you skip that step. :wink:

0 Likes

#5

Awesome! I was not even sure that was possible with Sublime Text alone. I thought I was going to have to pipe the file to some Powershell commands, then pipe it back into ST3 using the plugin Shell Turtlestein.

-Thanks

0 Likes