I have a bunch of lines in a row that look like text and I want to reorder them into alphabetical order, using the alt attribute to sort. Is there a slick one or two lines of vimscript that I could use to get this? For concreteness, there are no line breaks in any of the entries and the relevant lines are 17-35.
You can use a regular expression as argument to the built-in :sort command:
:17,35sort /.*alt="\zs/
Related
In the DOS-era, text sorting when it comes to numbers use to work normally. ASCII order was correctly taken into account by any editor. Example: the list 100,1,20,3,10,2 would of been arranged in the correct order: 1,2,3,10,20,100. Now-days any text editor seems to disregard numbers (and special characters), resulting in something like: 1,10,100,2,20,3, which is practically a mess. This is also valid for other characters.
How can I make a correct sorting now-days ?
Note: I'm trying to use this to put many IP addresses in order.
What sort or any editor does:
103.207.39.0
124.248.228.0
125.75.132.0
13.107.6.0
136.243.202.0
139.217.27.0
14.139.200.0
14.53.187.0
144.76.109.0
148.251.204.0
This is the desired output:
13.107.6.0
14.53.187.0
14.139.200.0
103.207.39.0
124.248.228.0
125.75.132.0
136.243.202.0
139.217.27.0
144.76.109.0
148.251.204.0
Open your file of IP addresses in Notepad++. Do a regex Find-and-Replace:
Find what: (?:^|(?<=\.))\d(\d)?(?=\.|$)
Replace with: \x20(?1:\x20)$0
Make sure the search mode is "Regular expression" and click Replace All.
Now sort the lines using Edit > Line Operations > Sort lines Lexicographically Ascending
Now do another regex Find-and-Replace in order to get rid of the spaces:
Find what: \x20
Replace with nothing: make sure the search mode is "Regular expression" and click Replace All.
source: https://notepad-plus-plus.org/community/topic/14354/can-i-sort-ip-addresses-in-numeric-value
is it possible to sort on multiple columns and ignore certain lines starting with # ?
I have my a text like this:
#Comments
#More comments
foo;1;1
foo;3;2
bar;2;1
I'd like to sort on the first number and if those are equal on the last number.
I tried this:
:%!sort -t';' -k2n -k3n
but this will affect the comments section.
I know i can make vim ignore the comments like this:
:sort /^#/
but how do i select the fields now??
Does the shell sort have a field ignorer? Or can the VIM sort use fields?
BTW the comments section's length can increase so head/tail won't work.
I do not think that
:sort /^#/
does what you want. It will sort the comments, putting them at the end of the buffer, and leave the other lines in the original order. A lot closer to what you want is
:sort /;/
This will leave all comments at the top of the buffer, in the original order, and sort on the part of the line after the first ;. Probably lexicographic sort is not what you want. Instead, you could use
:sort /;/ n
This will do numeric sort, but ignore the part of the line after the first number.
In order to avoid sorting comment lines that happen to contain ; characters, you could use a more complicated pattern:
:sort /^\(\s*#\)\#!.\{-};/ n
or (using a feature that I may never have tried before)
:sort /^\s*[^#]\&.\{-\};/ n
I am old-school, and use vim's default settings, but a lot of people prefer the \v (very magic) setting. That makes these a little simpler:
:sort /\v^(\s*#)#!.{-};/ n
:sort /\v^\s*[^#]&.{-};/ n
OTOH, the version you suggested using the external sort seems to work perfectly.
$ sort --version
sort (GNU coreutils) 5.93
Can I sort lines in vim depending on a part of line and not the complete line?
e.g
My Name is Deus Deceit
I would like to sort depending on the column that the name starts + 6 columns
for example
sort by column 19-25 and vim will only check those characters for sorting.
If it can be done without a plugin that would be great. ty
Check out :help :sort. The command takes an options {pattern} whose matched text is skipped (i.e. sorting happens after the match.
For example, to sort by column 19+ (see :help /\%c and the related regexp atoms):
:sort /.*\%19c/
I'm having problems about a regexp.
I'm trying to implement a regex to select just the tab indent blocks, but i cant find a way of make it work:
Example:
INDENT(1)
INDENT(2)
CONTENT(a)
CONTENT(b)
INDENT(3)
CONTENT(c)
So I need blocks like:
INDENT(2)
CONTENT(a)
CONTENT(b)
AND
INDENT(3)
CONTENT(c)
How I can do this?
really tks, its almost that, here is my original need:
table
tr
td
"joao"
"joao"
td
"marcos"
I need separated "td" blocks, could i adapt your example to that?
It depends on exactly what you are trying to do, but maybe something like this:
^(\t+)(\S.*)\n(?:\1\t.*\n)*
Working example: http://www.rubular.com/r/qj3WSWK9JR
The pattern searches for:
^(\t+)(\S.*)\n - a line that begins with a tab (I've also captured the first line in a group, just to see the effect), followed by
(?:\1\t.*\n)* - lines with more tabs.
Similarly, you can use ^( +)(\S.*)\n(?:\1 .*\n)* for spaces (example). Mixing spaces and tabs may be a little problematic though.
For the updated question, consider using ^(\t{2,})(\S.*)\n(?:\1\t.*\n)*, for at least 2 tabs at the beginning of the line.
You could use the following regex to get the groups...
[^\s]*.*\r\n(?:\s+.*\r*\n*)*
this requires that your lines not begin with white space for the beginning of the blocks.
I have been looking at regular expressions to try and do this, but the most I can do is find the start of a line with ^, but not replace it.
I can then find the first characters on a line to replace, but can not do it in such a way with keeping it intact.
Unfortunately I donĀ“t have access to a tool like cut since I am on a windows machine...so is there any way to do what I want with just regexp?
Use notepad++. It offers a way to record an sequence of actions which then can be repeated for all lines in the file.
Did you try replacing the regular expression ^ with the text you want to put at the start of each line? Also you should use the multiline option (also called m in some regex dialects) if you want ^ to match the start of every line in your input rather than just the first.
string s = "test test\ntest2 test2";
s = Regex.Replace(s, "^", "foo", RegexOptions.Multiline);
Console.WriteLine(s);
Result:
footest test
footest2 test2
I used to program on the mainframe and got used to SPF panels. I was thrilled to find a Windows version of the same editor at Command Technology. Makes problems like this drop-dead simple. You can use expressions to exclude or include lines, then apply transforms on just the excluded or included lines and do so inside of column boundaries. You can even take the contents of one set of lines and overlay the contents of another set of lines entirely or within column boundaries which makes it very easy to generate mass assignments of values to variables and similar tasks. I use Notepad++ for most stuff but keep a copy of SPFSE around for special-purpose editing like this. It's not cheap but once you figure out how to use it, it pays for itself in time saved.