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.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

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.

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

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

find which lines in a file are longer than N characters

Terminal - find which lines in a file are longer than N characters
perl -nle 'print length,"\t",$_ if length > 37' < /path/to/input/file
2014-03-20 09:44:41
User: fibo
Functions: perl
0
find which lines in a file are longer than N characters

Note that in the command N is, for instance, 37.

Alternatives

There are 4 alternatives - vote for the best!

Terminal - Alternatives
awk 'length>72' file
2009-09-10 05:54:41
User: haivu
Functions: awk
Tags: awk
16

This command displays a list of lines that are longer than 72 characters. I use this command to identify those lines in my scripts and cut them short the way I like it.

perl -lne '$l{$_}=length;END{for(sort{$l{$a}<=>$l{$b}}keys %l){print}}' < /usr/share/dict/words | tail
awk '{print length, $0;}' | sort -nr
perl -C -e 'print for sort { length $a <=> length $b or $a cmp $b } <>' < /usr/share/dict/words | tail
2011-10-20 01:43:25
User: dbr
Functions: cmp perl sort
3

making it "sound" more "natural" language like -- additionally sorting the longest words alphabetically:

this approach is using:

* to get at all lines of input

* post-"for" structure

* short-circuit-or in sort: if the lengths are the same, then sort alphabetically otherwise don't even evaluate the right hand side of the or

* -C sets all input and ouput channels to utf8

while read i; do [ ${#i} -gt 72 ] && echo "$i"; done < /path/to/file
2014-03-20 12:27:06
User: flatcap
Functions: echo read
1

Filter out lines of input that contain 72, or fewer, characters.

This uses bash only. ${#i} is the number of characters in variable i.

Know a better way?

If you can do better, submit your command here.

Your point of view

You must be signed in to comment.

Related sites and podcasts