Skip to content

Instantly share code, notes, and snippets.

@rm
Created May 31, 2012 16:10
Show Gist options
  • Select an option

  • Save rm/2844454 to your computer and use it in GitHub Desktop.

Select an option

Save rm/2844454 to your computer and use it in GitHub Desktop.
Emacs - regexp sort

Region is:

Rajendra Prasad
Sarvepalli Radhakrishnan
Zakir Hussain
Varahagiri Venkata Giri
Fakhruddin Ali Ahmed
Neelam Sanjiva Reddy
Giani Zail Singh
Ramaswamy Venkataraman
Shankar Dayal Sharma
Kocheril Raman Narayanan
A. P. J. Abdul Kalam
Pratibha Patil

Then you do:

M-x sort-regexp-fields
Regexp specifying records to sort:^.*\s-\(\w+\)$
Regexp specifying key within record: \1

Region is now:

Fakhruddin Ali Ahmed
Varahagiri Venkata Giri
Zakir Hussain
A. P. J. Abdul Kalam
Kocheril Raman Narayanan
Pratibha Patil
Rajendra Prasad
Sarvepalli Radhakrishnan
Neelam Sanjiva Reddy
Shankar Dayal Sharma
Giani Zail Singh
Ramaswamy Venkataraman
@rm
Copy link
Author

rm commented May 31, 2012

Note that:

  1. ( and ) (for the named regexp block) need to be escaped.
  2. the regexp class for whitespace is \s- and not \s
  3. be careful about matching the entire line in the regexp, else sort-regexp-fields will just move the matched areas around leaving the rest of line as is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment