sort selected lines in a text file to the beginning or end of the file.

2end () ( export LC_ALL=C; nl -n rz $1 > $1.tmp; ${EDITOR:-vi} $1.tmp; sort $1.tmp | sed -r 's/^.*[0-9]+\t+//' > $1; rm $1.tmp; )
This function is used to sort selected lines of a text file to the end of that file. Especially useful in cases where human intervention is necessary to sort out parts of a file. Let's say that you have a text file which contains the words rough slimy red fluff dough For whatever reason, you want to sort all words rhyming with 'tough' to the bottom of the file, and all words denoting colors to the top, while keeping the order of the rest of the file intact. '$EDITOR' will open, showing all of the lines in the given file, numbered with '0' padding. Adding a '~' to the beginning of the line will cause the line to sort to the end of the file, adding '!' will cause it to sort to the beginning.
Sample Output
$ 2end /tmp/2end.txt
~000001 rough
000002  slimy
!000003 red
~000004 fluff
000005  dough
$ cat /tmp/2end.txt 
red
slimy
dough
rough
fluff

0
By: bartonski
2010-03-06 23:02:28

What Others Think

while I approve the creative use of shell script, I'm curious when this came up? why would you want to "sort" to top or bottom and not to different files? Is it a priority thing (like for todo lists)? if so why not "rate" importance from 1-10 and then sort on that field? Just curious.
infinull · 601 weeks and 4 days ago
No... I do a lot of this at work. I do ETL (Extract, Transform, Load) on text data gathered from tens of thousands of heterogeneous data feeds. I'm pulling selections of a particular raw data feed to the top or bottom of a file, for trouble shooting purposes, typically based on ID numbers that I find in log files. Reading the log files and figuring out whether I need to look at the associated raw data is an art form. Due to the nature of the raw data, I can't usually select the records in the raw text files using grep: ID number might change position in a record, based on record type, but a record number might not have a format that's guaranteed to be distinguishable from one of the other pieces of data in the file. Due to all of this variability, automating the process of pulling data records is time consuming, I'm almost always better off searching for ID numbers using VIM's search function, and deciding whether or not the record should be selected, and where it should go in the file based on visual inspection.
bartonski · 601 weeks and 4 days ago
"I can't usually select the records in the raw text files using grep: ID number might change position in a record, based on record type, but a record number might not have a format that's guaranteed to be distinguishable from one of the other pieces of data in the file." To be clear: Because the record number isn't guaranteed to be of a unique format, I have to constrain the search for that ID number by position within a record... but the position of the ID number usually changes across record types, making for a regular expression that's probably more trouble to compose than it's worth.
bartonski · 601 weeks and 4 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: