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/
Related
Due to poor past naming practices, I'm left with a list of names that is proving to be a challenge to work with. The bottom line is that I want the most current name (by date) to be placed in a variable. All the names are listed (unsorted) in a file called bar.txt.
In this case I can't rename, and there's no way to get the actual dates of the images; these names are all I have to go on. The names can follow one of several patterns;
foo
YYYYMMDD-foo
YYYYMMDD##-foo
foo can be anything from a single character to a long string of letters/numbers/symbols. I am interested only in the names matching the second use case, YYMMDD-foo, as those are from after we started tagging consistently.
I would like to end up with a variable containing the most recent date that follows the pattern YYMMDD-foo.
I know sort -k1 -n < bar.txt, but then I'm not sure how to isolate the second pattern's results to extract what I need.
How do I sort the file to ignore anything but the second pattern, and return the most current date?
Sample
Given that bar.txt looks like this;
test
2017120901-develop-BUILD-31
20170326-TEST-1.2.0
20170406-BUILD-40-1.2.0-test
2010818_001
I would want to extract 20170406-BUILD-40-1.2.0-test
Since your requirement involves 1) to get only files of a certain format 2) apply sorting and get only the latest file. Am using a Awk & GNU sort together to achieve it
awk -F'-' 'length($1) == 8' file | sort -nrk1 | head -1
20170406-BUILD-40-1.2.0-test
The solution works by only getting those lines in the file whose first column has 8 characters exactly corresponding to YYYYMMDD alignment. Once those filtered, sort applied on first field and the first line is obtained using head.
I have a text file similar to
"3"|"0001"
"1"|"0003"
"1"|"0001"
"2"|"0001"
"1"|"0002"
i.e. a pipe-delimited text file containing quoted strings.
What I need to do is:
First, extract the first line which contains each value in the first column, producing
"3"|"0001"
"1"|"0003"
"2"|"0001"
Then, sort by the values in the first column, producing
"1"|"0003"
"2"|"0001"
"3"|"0001"
Performing the sort is easy - sort -k 1,1 -t \| - but I'm stuck on extracting the first line in the file which contains each value in the first column. I thought of using uniq but it doesn't do what I want, and it's "column-handling" abilities are limited to ignoring the first 'x' columns of space-or-tab delimited text.
Using the Posix shell (/usr/bin/sh) under HP-UX.
I'm kind of drawing a blank here. Any suggestions welcomed.
you can do:
awk -F'|' '!a[$1]++' file|sort...
The awk part will remove the duplicated lines, only leave the first occurrence.
I don't have a HP-unix box, I therefore cannot do real test. But I think it should go...
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/
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
I have a large text file with 4-digit codes and some information about them in every row. It looks something like this:
3456 information
1234 info
2222 Some ohter info
I need to sort this file, so the codes are in ascending order in the file. Also, some codes appear more than once, so I need to remove duplicates. Can I do this with perl, awk or some other scripting language?
Thanks in advance,
-skazhy
sort happybirthday.txt | uniq
From IBM.
1st result for Google: unix remove duplicate lines.
You can create a hash then read the file in line by line and for each line
split at the first space
check if the val(0), the number that you just split, is in the hash
if not the insert the val(1), rest of the line, into the hash with a key val(0)
continue
Then print the (sorted) hash to the file.