Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. 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.


Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Universal configuration monitoring and system of record for IT.
Hide

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:

Hide

News

May 19, 2015 - A Look At The New Commandlinefu
I've put together a short writeup on what kind of newness you can expect from the next iteration of clfu. Check it out here.
March 2, 2015 - New Management
I'm Jon, I'll be maintaining and improving clfu. Thanks to David for building such a great resource!
Hide

Top Tags

Hide

Functions

Psst. Open beta.

Wow, didn't really expect you to read this far down. The latest iteration of the site is in open beta. It's a gentle open beta-- not in prime-time just yet. It's being hosted over at UpGuard (link) and you are more than welcome to give it a shot. Couple things:

  • » The open beta is running a copy of the database that will not carry over to the final version. Don't post anything you don't mind losing.
  • » If you wish to use your user account, you will probably need to reset your password.
Your feedback is appreciated via the form on the beta page. Thanks! -Jon & CLFU Team

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

Terminal - 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; )
2010-03-06 23:02:28
User: bartonski
Functions: export nl rm sed sort
0
sort selected lines in a text file to the beginning or end of the file.

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.

Alternatives

There are 2 alternatives - vote for the best!

Terminal - Alternatives

Know a better way?

If you can do better, submit your command here.

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.

Comment by infinull 338 weeks and 3 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.

Comment by bartonski 338 weeks and 2 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.

Comment by bartonski 338 weeks and 2 days ago

Your point of view

You must be signed in to comment.