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 - 331 results
git remote -v | grep fetch | sed 's/\(.*github.com\)[:|/]\(.*\).git (fetch)/\2/' | awk {'print "https://github.com/" $1'} | xargs open
2012-04-15 20:48:46
User: brockangelo
Functions: awk grep sed xargs
1

Written for Mac OSX. When you are working in a project and want to open it on Github.com, just type "gh" and your default browser will open with the repo you are in. Works for submodules, and repo's that you don't own.

You'll need to copy / paste this command into a gh.sh file, then create an alias in your bash or zsh profile to the gh.sh script. Detailed instructions here if you still need help:

http://gist.github.com/1917716

for k in $(git branch | sed /\*/d); do echo "$(git log -1 --pretty=format:"%ct" $k) $k"; done | sort -r | awk '{print $2}'
2012-04-07 11:19:00
User: dahuie
Functions: awk echo sed sort
Tags: bash git sed awk
0

Simpler and without all of the coloring gimmicks. This just returns a list of branches with the most recent first. This should be useful for cleaning your remotes.

sed G input.txt | cat -s
sed -i 's/$/\r/' file
2012-02-23 08:34:30
User: evolix
Functions: sed
-1

This permit to convert an UNIX file to DOS file.

You can use it in a loop to convert multiple files, like :

for i in *.bat; do sed -i 's/$/\r/' $i; done

cmdfu(){ local t=~/cmdfu;echo -e "\n# $1 {{{1">>$t;curl -s "commandlinefu.com/commands/matching/$1/`echo -n $1|base64`/plaintext"|sed '1,2d;s/^#.*/& {{{2/g'>$t;vim -u /dev/null -c "set ft=sh fdm=marker fdl=1 noswf" -M $t;rm $t; }
2012-02-21 05:43:16
User: AskApache
Functions: echo rm sed vim
6

Here is the full function (got trunctated), which is much better and works for multiple queries.

function cmdfu () {

local t=~/cmdfu;

until [[ -z $1 ]]; do

echo -e "\n# $1 {{{1" >> $t;

curl -s "commandlinefu.com/commands/matching/$1/`echo -n $1|base64`/plaintext" | sed '1,2d;s/^#.*/& {{{2/g' | tee -a $t > $t.c;

sed -i "s/^# $1 {/# $1 - `grep -c '^#' $t.c` {/" $t;

shift;

done;

vim -u /dev/null -c "set ft=sh fdm=marker fdl=1 noswf" -M $t;

rm $t $t.c

}

Searches commandlinefu for single/multiple queries and displays syntax-highlighted, folded, and numbered results in vim.

sed -e 's/[;|][[:space:]]*/\n/g' .bash_history | cut --delimiter=' ' --fields=1 | sort | uniq --count | sort --numeric-sort --reverse | head --lines=20
sed -i 6,66d <filename>
2012-02-04 02:29:40
User: icsamples
Functions: sed
Tags: bash sed
0

Delete range of lines. Ex: Line 6 through 66 in .

print "$(lsvg -Lo |xargs lsvg -L|grep "TOTAL PPs"|awk -F"(" '{print$2}'|sed -e "s/)//g" -e "s/megabytes/+/g"|xargs|sed -e "s/^/(/g" -e "s/+$/)\/1000/g"|bc ) GB"
2012-02-03 13:58:41
0

Not figured by me, but a colleague of mine.

See the total amount of data on an AIX machine.

sed -r 's/(\[|])//g' | awk ' { $1=strftime("%D %T",$1); print }'
2012-02-03 13:07:37
User: Zulu
Functions: awk sed
Tags: sed awk timestamp
0

It remove the square bracket and convert UNIX time to human readable time for all line of a stream (or file).

sed -re '/^#/d ; s/#.*$// ; /^\s*$/d'
2012-02-02 18:12:52
User: Zulu
Functions: sed
Tags: sed
-1

Use it with cat and '|' for know what is used in a conf file.

For example cat /etc/squid/squid.conf | sed -re '/^#/d ; s/#.*$// ; /^\s$/d' :

Show you what you use in your file conf.

It removes all comments and empty lines.

Empty lines are lines with nothing, a tab, or a space.

sed -i <file> -re '<start>,<end>d'
sed -re '/^#/d ; s/#.*$//'
2012-02-01 20:39:23
User: Zulu
Functions: sed
Tags: sed
-1

Delete all comments (#) on text :

It deletes the entire comment line and remove comments form end of others.

sed 's!<[Aa] *href*=*"\([^"]*\)"*>\([^<>]*\)</[Aa]>!\1,\2!g' links.html
for fn in xkcd*.png xkcd*.jpg; do echo $fn; read xw xh <<<$(identify -format '%w %h' $fn); nn="$(echo $fn | sed 's/xkcd-\([^-]\+\)-.*/\1/')"; wget -q -O xkcd-${nn}.json http://xkcd.com/$nn/info.0.json; tt="$(sed 's/.*"title": "\([^"]\+\)",.*/\1/' ...
2012-01-06 20:26:11
User: fpunktk
Functions: echo read wget
-2

full command:

for fn in xkcd*.png xkcd*.jpg; do; echo $fn; read xw xh <<<$(identify -format '%w %h' $fn); nn="$(echo $fn | sed 's/xkcd-\([0-9]\+\)-.*/\1/')"; wget -q -O xkcd-${nn}.json http://xkcd.com/$nn/info.0.json; tt="$(sed 's/.*"title": "\([^"]*\)", .*/\1/' xkcd-${nn}.json)"; at="$(sed 's/.*alt": "\(.*\)", .*/\1/' xkcd-${nn}.json)"; convert -background white -fill black -font /usr/share/fonts/truetype/freefont/FreeSansBold.ttf -pointsize 26 -size ${xw}x -gravity Center caption:"$tt" tt.png; convert -background '#FFF9BD' -border 1x1 -bordercolor black -fill black -font /usr/share/fonts/truetype/freefont/FreeSans.ttf -pointsize 16 -size $(($xw - 2))x -gravity Center caption:"$at" at.png; th=$(identify -format '%h' tt.png); ah=$(identify -format '%h' at.png); convert -size ${xw}x$(($xh+$th+$ah+5)) "xc:white" tt.png -geometry +0+0 -composite $fn -geometry +0+$th -composite at.png -geometry +0+$(($th+$xh+5)) -composite ${fn%\.*}_cmp.png; echo -e "$fn $nn $xw $xh $th $ah \n$tt \n$at\n"; done

this assumes that all comics are saved as xkcd-[number]-[title].{png|jpg}.

it will then download the title and alt-text, create pictures from them, and put everything together in a new png-file.

it's not perfect, but it worked for nearly all my comics.

it uses the xkcd-json-interface.

though it's poorly written, it doesn't completely break on http://xkcd.com/859/

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
6

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)}'