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

Commands tagged Text Processing from sorted by
Terminal - Commands tagged Text Processing - 37 results
ls -1 | xargs ruby -e'puts ARGV.shuffle'
perl -ne '$. == 4 && print && exit'
2011-09-05 21:52:16
User: bashrc
Functions: perl
-1

Perl version - just for completeness sake ;)

sed '4!d'
2011-09-05 17:54:57
User: totti
Functions: sed
-1

d --> delete

!d ---> delete others

echo "abcde" | sed 's/./& /g'
sort -R
2011-07-15 15:35:27
User: RyanM
Functions: sort
2

Randomizes a file. The opposite of sort is sort -R!

awk 'BEGIN{RS="\0"}{gsub(/\n/,"<SOMETEXT>");print}' file.txt
2010-12-12 21:43:22
User: __
Functions: awk
1

awk version of 7210. Slightly longer, but expanding it to catch blank lines is easier:

awk 'BEGIN{RS="\0"}{gsub(/\n+/,"<SOMETEXT>");print}' file.txt
tr "\n" " " < file
2010-12-08 16:13:54
User: randy909
Functions: tr
7

Even shorter. Stolen from comment posted by eightmillion.

cat file | tr -d "\n"
2010-12-02 09:22:02
User: uzsolt
Functions: cat file tr
-1

This command deletes the "newline" chars, so its output maybe unusable :)

cat file | tr "\n" " "
2010-12-02 09:21:02
User: uzsolt
Functions: cat file tr
9

It's works only when you replace '\n' to ONE character.

perl -pe 'eof()||s/\n/<SOMETEXT>/g' file.txt
2010-12-02 01:19:27
User: eightmillion
Functions: perl
3

This command turns a multi-line file into a single line joined with <SOMETEXT>. To skip blank lines, use:

perl -pe '(eof()||s/^\s*$//)||s/\n/<SOMETEXT>/g' file.txt
find . -name "*noticia*" -name "*jhtm*" -name "*.tpl" -exec grep -li "id=\"col-direita\"" '{}' \; | xargs -n1 mate
figlet gunslinger_
sort -t $'\t' -k 2 input.txt
2010-07-11 12:58:51
User: postrational
Functions: sort
4

Use this BASH trick to create a variable containing the TAB character and pass it as the argument to sort, join, cut and other commands which don't understand the \t notation.

sort -t $'\t' ... join -t $'\t' ... cut -d $'\t' ...
cut -f 1 three-column.txt > first-column.txt
2010-07-11 10:13:45
User: postrational
Functions: cut
4

There is a common command for outputting a field or list of fields from each line in a file. Why wouldn't you just use cut?

sed -n '4{p;q}'
tail -n +4 | head -n 1
awk '{print $1}' < three-column.txt > first-column.txt
while read col1 col23; do echo $col1; done < three-column.txt > first-column.txt
while read l; do echo ${l%% *}; done < three-column-list.txt > only-first-column.txt
2010-07-09 03:42:56
User: zed
Functions: echo read
1

The above is an example of grabbing only the first column. You can define the start and end points specifically by chacater position using the following command:

while read l; do echo ${l:10:40}; done < three-column-list.txt > column-c10-c40.txt

Of course, it doesn't have to be a column, or extraction, it can be replacement

while read l; do echo ${l/foo/bar}; done < list-with-foo.txt > list-with-bar.txt

Read more about parameter expansion here:

http://wiki.bash-hackers.org/syntax/pe

Think of this as an alternative to awk or sed for file operations

sed -n '4p'
head -n X | tail -n 1
2010-07-08 22:06:39
User: infinull
Functions: head tail
-1

using tail first won't do it because tail counts from the bottom of the file. You could do it this way but I don't suggest it

awk 'NR==4'
tail -n 4 | head -n 1
2010-07-08 19:50:06
User: puddy
Functions: head tail
-7

tail -n X | head -n 1

prints a specific line, where X is the line number

perl -wl -e '@f=<>; for $i (0 .. $#f) { $r=int rand ($i+1); @f[$i, $r]=@f[$r,$i] if ($i!=$r); } chomp @f; print join $/, @f;' try.txt
random -f <file>