Commands using sed (1,319)

  • This will generate 3 paragraphs with random text. Change the 3 to any number. Show Sample Output


    5
    lynx -source http://www.lipsum.com/feed/xml?amount=3|perl -p -i -e 's/\n/\n\n/g'|sed -n '/<lipsum>/,/<\/lipsum>/p'|sed -e 's/<[^>]*>//g'
    houghi · 2010-04-26 17:26:44 5
  • This command will format your alias or function to a single line, trimming duplicate white space and newlines and inserting delimiter semi-colons, so it continues to work on a single line. Show Sample Output


    5
    goclf() { type "$1" | sed '1d' | tr -d "\n" | tr -s '[:space:]'; echo }
    meathive · 2010-06-26 21:44:17 17
  • Tired copy paste to get opcode from objdump huh ? Get more @ http://gunslingerc0de.wordpress.com Show Sample Output


    5
    objdump -d ./PROGRAM|grep '[0-9a-f]:'|grep -v 'file'|cut -f2 -d:|cut -f1-6 -d' '|tr -s ' '|tr '\t' ' '|sed 's/ $//g'|sed 's/ /\\x/g'|paste -d '' -s |sed 's/^/"/'|sed 's/$/"/g'
    gunslinger_ · 2010-07-11 15:44:48 110
  • If you're going to use od, here's how to suppress the labels at the beginning. Also, it doesn't output the \x, hence the sed command at the end. Remove it for space separated hex values instead Show Sample Output


    5
    echo -n "text" | od -A n -t x1 |sed 's/ /\\x/g'
    camocrazed · 2010-07-14 15:31:36 8

  • 5
    sed -r "s:\x1B\[[0-9;]*[mK]::g"'
    roylez · 2010-07-22 05:37:25 6
  • Default output-file is "liveh.txt". This uses only BRE, in case you're using an older version of sed(1) that doesn't have support for ERE added. With a modern sed(1), to reduce false positive matches, you might do something like: liveh(){ tcpdump -lnnAs512 -i ${1-} tcp |sed 's/.*GET /GET /;s/.*Host: /Host: /;s/.*POST /POST /;/GET |Host: |POST /!d;/[\"'"'"]/d;/\.\./d;w '"${2-liveh.txt}"'' >/dev/null ;} Anyway, it's easy to clean up the output file with sed(1) later.


    5
    liveh(){ tcpdump -lnAs512 ${1-} tcp |sed ' s/.*GET /GET /;s/.*Host: /Host: /;s/.*POST /POST /;/[GPH][EOo][TSs]/!d;w '"${2-liveh.txt}"' ' >/dev/null ;} # usage: liveh [-i interface] [output-file] && firefox &
    argv · 2010-10-11 01:01:11 3
  • substitute "example" with desired string; tl = target language (en, fr, de, hu, ...); you can leave sl parameter as-is (autodetection works fine) Show Sample Output


    5
    wget -U "Mozilla/5.0" -qO - "http://translate.google.com/translate_a/t?client=t&text=translation+example&sl=auto&tl=fr" | sed 's/\[\[\[\"//' | cut -d \" -f 1
    sairon · 2011-03-06 13:46:16 109
  • Modify file in place to remove empty lines and create a backup of the original with the extension .bak Show Sample Output


    5
    sed -e '/^$/d' -i .bak filewithempty.lines
    donnoman · 2011-03-24 00:03:25 6
  • Gets a BOFH excuse from the BOFH excuse server (towel.blinkenlights.nl port 666), and passes it through sed and tr to get rid of telnet connection stuff. Show Sample Output


    5
    telnet towel.blinkenlights.nl 666 | sed "s/=== The BOFH Excuse Server ===//" | tr -d '\n' && echo
    hintss · 2011-03-31 05:50:57 3
  • This command outputs a table of sighting opportunities for the International Space Station. Find the URL for your city here: http://spaceflight.nasa.gov/realdata/sightings/ Show Sample Output


    5
    links -dump "http://spaceflight.nasa.gov/realdata/sightings/cities/view.cgi?country=United_States&region=Wisconsin&city=Portage" | sed -n '/--/,/--/p'
    eightmillion · 2011-05-03 12:15:56 7
  • usage: mem memcache-command [arguments] where memcache-command might be: set add get[s] append prepend replace delete incr decr cas stats verbosity version notes: exptime argument is set to 0 (no expire) flags argument is set to 1 (arbitrary)


    5
    mem(){ { case $1 in st*|[vgid]*) printf "%s " "$@";; *) dd if=$3 2>&1|sed '$!d;/^0/d;s/ .*//;s/^/'"$1"' '"$2"' 1 0 /; r '"$3"'' 2>/dev/null;;esac;printf "\r\nquit\r\n";}|nc -n 127.0.0.1 11211; }
    argv · 2011-06-17 06:39:07 6
  • Using the sed -i (inline), you can replace the beginning of the first line of a file without redirecting the output to a temporary location.


    5
    sed -i '1s/^/text to prepend\n/' file1
    xeor · 2011-06-25 12:02:11 13
  • - grep for the word in a files, use recursion (to find files in sub directories), and list only file matches -| xargs passes the results from the grep command to sed -sed -i uses a regular expression (regex) to evaluate the change: s (search) / search word / target word / g (global replace)


    5
    grep -lr -e '<oldword>' * | xargs sed -i 's/<oldword>/<newword>/g'
    o0110o · 2011-08-27 02:42:51 7
  • This should work with anything://url.whatever etc etc ;)


    5
    sed "s/\([a-zA-Z]*\:\/\/[^ ]*\)\(.*\)/\<a href=\"\1\"\>\1\<\/a\>\2/"
    c3w · 2012-01-04 02:08:29 7
  • pgrep foo may return several pids for process foobar footy01 etc. like this: 11427 12576 12577 sed puts "-p " in front and we pass a list to top: top -p 11427 -p 12576 -p 12577


    5
    top $(pgrep foo | sed 's|^|-p |g')
    michelsberg · 2012-06-14 15:13:00 3
  • This makes your commandlinefu.com's favorites appear as most recent commands in your history.


    5
    (cat ~/.bash_history;U='curl -s www.commandlinefu.com';$U/users/signin -c/tmp/.c -d'username=<USER>&password=<PASS>&submit=1'|$U/commands/favourites/json -b/tmp/.c|grep -Po 'nd":.*?[^\\]",'|sed -re 's/.*":"(.*)",/\1/g')>~/.h;HISTFILE=~/.h bash --login
    xenomuta · 2012-08-17 12:31:51 34
  • (Please see sample output for usage) script.bash is your script, which will be crypted to script.secure script.bash --> script.secure You can execute script.secure only if you know the password. If you die, your script dies with you. If you modify the startup line, be careful with the offset calculation of the crypted block (the XX string). Not difficult to make script editable (an offset-dd piped to a gpg -d piped to a vim - piped to a gpg -c directed to script.new ), but not enough space to do it on a one liner. Show Sample Output


    5
    echo "eval \"\$(dd if=\$0 bs=1 skip=XX 2>/dev/null|gpg -d 2>/dev/null)\"; exit" > script.secure; sed -i s:XX:$(stat -c%s script.secure): script.secure; gpg -c < script.bash >> script.secure; chmod +x script.secure
    rodolfoap · 2013-03-09 11:16:48 17
  • Automatically drops mount points that have non-numeric sizes (e.g. /proc). Tested in bash on Linux and AIX. Show Sample Output


    5
    for m in `df -P | awk -F ' ' '{print $NF}' | sed -e "1d"`;do n=`df -P | grep "$m$" | awk -F ' ' '{print $5}' | cut -d% -f1`;i=0;if [[ $n =~ ^-?[0-9]+$ ]];then printf '%-25s' $m;while [ $i -lt $n ];do echo -n '=';let "i=$i+1";done;echo " $n";fi;done
    drockney · 2013-07-29 20:12:39 6
  • Opposite: Convert an one-liner to script: foo() { <one-liner> ; } ... typeset -f foo ... unset -f foo


    5
    (sed 's/#.*//g'|sed '/^ *$/d'|tr '\n' ';'|xargs echo) < script.sh
    knoppix5 · 2013-10-26 23:23:51 16
  • Show the current load of the CPU as a percentage. Read the load from /proc/loadavg and convert it using sed: Strip everything after the first whitespace: sed -e 's/ .*//' Delete the decimal point: sed -e 's/\.//' Remove leading zeroes: sed -e 's/^0*//' Show Sample Output


    5
    sed -e 's/ .*//' -e 's/\.//' -e 's/^0*//' /proc/loadavg
    flatcap · 2014-04-18 19:12:05 10
  • weather 97405 Show Sample Output


    5
    weather() { curl -s "http://www.wunderground.com/q/zmw:$1.1.99999" | grep "og:title" | cut -d\" -f4 | sed 's/&deg;/ degrees F/'; }
    cadejscroggins · 2015-09-19 07:57:47 12
  • Very quick way to change a word in a file. I use it all the time to change variable names in my PHP scripts (sed -i 's/$oldvar/$newvar/g' index.php)


    4
    sed -i 's/OLD/NEW/g' FILE
    nanexcool · 2009-02-05 18:07:41 26
  • Very useful when the ssh key of a host has changed and ssh refuses to connect to the machine, while giving you the line number that has changed in ~/.ssh/known_hosts.


    4
    sed -i '10d' <somefile>
    raphink · 2009-02-16 15:05:29 126

  • 4
    sed '/./,$!d'
    grep · 2009-02-16 20:39:40 9
  • The ctrl+v,ctrl+m portion represents key presses that you should do. If you do it successfully you should see a ^M character appear.


    4
    sed 's/$/<ctrl+v><ctrl+m>/'
    SiegeX · 2009-02-16 20:53:05 12
  • ‹ First  < 4 5 6 7 8 >  Last ›

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. 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.

Share Your Commands


Check These Out

show all established tcp connections on os x

List all files in current dir and subdirs sorted by size
or $ tree -ifsF --noreport .|sort -n -k2|grep -v '/$' (rows presenting directory names become hidden)

Recover tmp flash videos (deleted immediately by the browser plugin)
Newer versions of the flashplayer browser plugin delete the tmp flash video immediately after opening a filehandle to prevent the user from "exporting" the video by simply copying the /tmp/FlashXYZ file. This command searches such deleted flash videos and creates symbolic links to the opened filehandle with the same name as the deleted file. This allows you to play your flash-videos (from e.g. youtube) with e.g. mplayer or copy the buffered video if you want to keep it.

Find where a kind of file is stored
In this case searches for where .desktop files are stored. The resulted is a sorted list of the top directories containing such files.

Split a large file, without wasting disk space
It's common to want to split up large files and the usual method is to use split(1). If you have a 10GiB file, you'll need 10GiB of free space. Then the OS has to read 10GiB and write 10GiB (usually on the same filesystem). This takes AGES. . The command uses a set of loop block devices to create fake chunks, but without making any changes to the file. This means the file splitting is nearly instantaneous. The example creates a 1GiB file, then splits it into 16 x 64MiB chunks (/dev/loop0 .. loop15). . Note: This isn't a drop-in replacement for using split. The results are block devices. tar and zip won't do what you expect when given block devices. . These commands will work: $ hexdump /dev/loop4 . $ gzip -9 < /dev/loop6 > part6.gz . $ cat /dev/loop10 > /media/usb/part10.bin

Random number generation within a range N, here N=10

Find the most recent snapshot for an AWS EBS volume
Uses the python-based AWS CLI (https://aws.amazon.com/cli/) and the JSON query tool, JQ (https://stedolan.github.io/jq/)

FAST Search and Replace for Strings in all Files in Directory
I needed a way to search all files in a web directory that contained a certain string, and replace that string with another string. In the example, I am searching for "askapache" and replacing that string with "htaccess". I wanted this to happen as a cron job, and it was important that this happened as fast as possible while at the same time not hogging the CPU since the machine is a server. So this script uses the nice command to run the sh shell with the command, which makes the whole thing run with priority 19, meaning it won't hog CPU processing. And the -P5 option to the xargs command means it will run 5 separate grep and sed processes simultaneously, so this is much much faster than running a single grep or sed. You may want to do -P0 which is unlimited if you aren't worried about too many processes or if you don't have to deal with process killers in the bg. Also, the -m1 command to grep means stop grepping this file for matches after the first match, which also saves time.

Route outbound SMTP connections through a addtional IP address rather than your primary

GRUB2: set Super Mario as startup tune


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: