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 sed from sorted by
Terminal - Commands tagged sed - 317 results
sed "s/\([a-zA-Z]*\:\/\/[^,]*\),\(.*\)/\<a href=\"\1\"\>\2\<\/a\>/"
2012-01-06 13:55:05
User: chrismccoy
Functions: sed
Tags: sed html link
-2

an extension of command 9986 by c3w, allows for link text.

http://google.com,search engine

will link the hyperlink with the text after the url instead of linking with the url as linktext

sed 's/$/uniqueString/' file.old | sed 's/,/\n/g' | sed ':loop;/^\"[^\"]*$/N;s/\n/,/;/[^\"]$/t loop' | sed ':loop;N;s/\n/@/g;/uniqueString$/!b loop;s/uniqueString$//' > file.new
2012-01-06 10:06:40
User: moogmusic
Functions: sed
Tags: sed CSV delimiter
0

Useful for CSV files. In the command, the file in question is comma delimited but contains double quoted fields containing commas and contains no @ symbols (as confirmed with http://www.commandlinefu.com/commands/view/9998/delimiter-hunting). This command converts the delimiting commas to @s while preserving the commas in the fields using the "uniqueString" to mark the ends of lines.

sed -r 's/'$(echo -e "\033")'\[[0-9]{1,2}(;([0-9]{1,2})?)?[mK]//g'
ls -a | sed "s#^#${PWD}/#"
2011-12-16 22:19:06
User: bbbco
Functions: ls sed
Tags: sed ls pwd
-9

Use the -a flag to display all files, including hidden files. If you just want to display regular files, use a -1 (yes, that is the number one). Got this by RTFM and adding some sed magic.

[bbbco@bbbco-dt ~]$ ls -a | sed "s#^#${PWD}/#"

/home/bbbco/.

/home/bbbco/..

/home/bbbco/2011-09-01-00-33-02.073-VirtualBox-2934.log

/home/bbbco/2011-09-10-09-49-57.004-VirtualBox-2716.log

/home/bbbco/.adobe

/home/bbbco/.bash_history

/home/bbbco/.bash_logout

/home/bbbco/.bash_profile

/home/bbbco/.bashrc

...

[bbbco@bbbco-dt ~]$ ls -1 | sed "s#^#${PWD}/#"

/home/bbbco/2011-09-01-00-33-02.073-VirtualBox-2934.log

/home/bbbco/2011-09-10-09-49-57.004-VirtualBox-2716.log

/home/bbbco/cookies.txt

/home/bbbco/Desktop

/home/bbbco/Documents

/home/bbbco/Downloads

...

sed -n '3,6p' /path/to/file
2011-12-14 15:09:38
User: flatcap
Functions: sed
Tags: sed
5

Print all lines between two line numbers

This command uses sed(1) to print all lines between two known line numbers in a file. Useful for seeing output in a log file, where the line numbers are known. The above command will print all lines between, and including, lines 3 and 6.

sed -i "s/\s*/ /g;s/\s*$//" input_file
sed -i "s/\(\x09\{1,\}\)\|\( \{1,\}\)/ /g;s/\(\x09\{1,\}$\)\|\( \{1,\}$\)//g" brisati.txt
2011-12-12 10:24:03
User: knoppix5
Functions: sed
-4

This command does the following:

- converts any sequence of multiple spaces/tabs to one space only

- completely removes any space(s)/tab(s) at the end of each line

(If spaces and tabs are mixed in a sequence i.e. [tab][tab][space][tab], you have to execute this command twice!)

echo foobar | sed -r 's/(^.|.$)//g'
sed -i '$a\FOOBAR' *
seq 1 3 20 | awk '{ T[NR]=$1} END {for (i=1;i<=(NR-1);i++) print T[i+1],"-",T[i],"=" , T[i+1]-T[i]}'
2011-11-13 10:36:18
User: benoit_c_lbn
Functions: awk seq
Tags: sed
1

It's allways strange for me to see sed and awk in the same command line if you can avoid it

seq 1 3 20 | sed -n '1{h;d};H;x;s/\n/\t/p' | awk '{printf("%d - %d = %d\n", $2, $1, $2-$1)}'
sshostnew () {sed -i "$1d" $HOME/.ssh/known_hosts ; }
2011-11-07 10:33:04
User: _john
Tags: ssh sed
-1

If you work in an environment, where some ssh hosts change regularly this might be handy...

nocomments () { cat $1 | egrep -v '^[[:space:]]*#|^[[:space:]]*$|^[[:space:]]*;' | sed '/<!--.*-->/d' | sed '/<!--/,/-->/d'; }
2011-11-04 12:47:39
User: RuizTapiador
Functions: cat egrep sed
Tags: sed grep comment
1

Hide comments and empty lines, included XML comments,

sed -n '1~5{N;N;p}' file.txt
sed G file.txt
seq 9 | sed 'H;g' | awk -v RS='' '{for(i=1;i<=NF;i++)printf("%dx%d=%d%s", i, NR, i*NR, i==NR?"\n":"\t")}'
2011-10-22 18:41:09
User: kev
Functions: awk sed seq
Tags: sed awk
10

seq -s ' ' 1 9 | sed -n ':a;p;s/ *\w$//;h;/^$/t;b a;q' | tac | awk '{for(i=1;i

1x1=1

1x2=2 2x2=4

1x3=3 2x3=6 3x3=9

1x4=4 2x4=8 3x4=12 4x4=16

1x5=5 2x5=10 3x5=15 4x5=20 5x5=25

1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36

1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49

1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64

1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81

grep -n log4j MainPm.java | sed -e 's/^\([^:]*\):\(.*\)/\2 \1/'
2011-10-21 12:50:30
User: bash_vi
Functions: grep sed
Tags: sed grep regex
0

Uses sed with a regex to move the linenumbers to the line end. The plain regex (w/o escapes) looks like that:

^([^:]*):(.*)

grep -rlZ oldstring . | xargs -0 sed -i -e 's/oldstring/newstring/'
2011-10-18 19:14:02
Functions: grep sed xargs
Tags: sed
1

Using -Z with grep and -0 with xargs handles file names with spaces and special characters.

curl -u username --silent "https://mail.google.com/mail/feed/atom" | awk 'BEGIN{FS="\n";RS="(</entry>\n)?<entry>"}NR!=1{print "\033[1;31m"$9"\033[0;32m ("$10")\033[0m:\t\033[1;33m"$2"\033[0m"}' | sed -e 's,<[^>]*>,,g' | column -t -s $'\t'
2011-10-15 23:15:52
User: frntn
Functions: awk column sed
2

Just an alternative with more advanced formating for readability purpose. It now uses colors (too much for me but it's a kind of proof-of-concept), and adjust columns.

title() { sed 's/\<\w*/\u&/g' <<<$@; }
svn status|grep -iR '^!'|sed 's/!/ /g'|xargs -i svn rm '{}'
2011-10-06 08:11:25
User: erdeszt
Functions: grep rm sed xargs
Tags: svn sed xargs grep
0

Helps if you accidentally deleted files from an svn repo with plain rm and you would like to mark them for svn to delete too.

tail -n +<N> <file> | head -n 1
2011-09-30 08:30:30
User: qweqq
Functions: head tail
-5

Tail is much faster than sed, awk because it doesn't check for regular expressions.

echo "command lines" | rev | cut -c 2- | rev
2011-09-21 11:27:52
User: ztank1013
Functions: cut echo ping rev
Tags: sed awk cut rev
0

In case sed and awk are not available you may use this to remove the last character from a string with "rev" and "cut".

read -ra words <<< "<sentence>" && echo "${words[@]^}"
echo 'fOo BaR' | tr '[A-Z]' '[a-z]' | sed 's/\(^\| \)\([a-z]\)/\1\u\2/g'