Commands using sed (1,319)

  • recursive find and replace. important stuff are grep -Z and zargs -0 which add zero byte after file name so sed can work even with file names with spaces.


    0
    grep -ZlRr -e BAD_SCRIPT_LINE * |xargs -0 sed -i 's/BAD_SCRIPT_LINE//g'
    homoludens · 2010-08-30 22:12:57 5
  • Useful for creating MAC addresses for virtual machines on a subnet. 00:16:3e is a standard Xen OID, change as needed. Show Sample Output


    0
    echo 00:16:3e$(gethostip 10.1.2.11 | awk '{ print tolower(substr($3,3)) }' |sed 's/.\{2\}/:&/g' )
    chwilk · 2010-09-23 16:46:21 3
  • Sometimes when I find a new cool command I want to know: 1.- which package owns it, and 2.- are there any other cool commands provided by this package? Since I don't necessarily need to know always both, I don't use this version, but I bundle it into two separate functions: # get command package owner # it can work without the full path, but sometimes fails, so better to provide it with whereis command owner () { pacman -Qo `whereis $1 | awk '{print $2}'` } whatelse () { package=`owner ${1} | sed -e 's/.*is owned by \([[:alpha:]]\+\).*/\1/'` pacman -Ql $package | grep 'bin' } Show Sample Output


    0
    w=`whereis <command> | awk '{print $2}'`; p=`pacman -Qo $w | sed -e 's/.*is owned by \([[:alpha:]]\+\).*/\1/'`; pacman -Ql $p | grep 'bin'
    conan · 2010-10-01 04:28:04 3

  • 0
    curl --silent http://www.dudalibre.com/gnulinuxcounter?lang=en | grep users | head -2 | tail -1 | sed 's/.*<strong>//g' | sed 's/<\/strong>.*//g'
    hunterm · 2010-10-07 04:12:45 3
  • Useful to duplicate and change iptables rules


    0
    sed -i -e '/foo/p' -e 's/foo/barfoo/' file
    lokutus25 · 2010-10-08 13:23:55 4
  • Grabs the ip2location site and removes everything but the span tag containing the country value. Place it inside your .bashrc or .bash_aliases file. Show Sample Output


    0
    ip2loc() { wget -qO - www.ip2location.com/$1 | grep "<span id=\"dgLookup__ctl2_lblICountry\">" | sed 's/<[^>]*>//g; s/^[\t]*//; s/&quot;/"/g; s/</</g; s/>/>/g; s/&amp;/\&/g'; }
    bkuri · 2010-10-13 00:19:35 4
  • Just a few minor changes. First the usage of lynx instead of curl so no sed is needed to revert the spaces. Then the usages of egrep instead of grep -e to save a few characters and last the removal of the extra 0. Show Sample Output


    0
    findlocation() {place=`echo $@`; lynx -dump "http://maps.google.com/maps/geo?output=json&oe=utf-8&q=$place" | egrep "address|coordinates" | sed -e 's/^ *//' -e 's/"//g' -e 's/address/Full Address/';}
    houghi · 2010-10-18 21:59:26 3

  • 0
    sed 's/^\(.*\)\(.\)\(.\)$/\1\3/' fileName
    harish · 2010-10-19 09:19:41 3
  • just a leaner, smaller version. Love the original idea!


    0
    kill -9 `ps xawo state=,pid=|sed -n 's/Z //p'`
    AskApache · 2010-10-27 07:38:07 3
  • Uses Google's "OneBox" to look up the sunrise in any city by name. If no city is specified, it defaults to Seattle. For the sunset time, you change the search query to "sunset", like so, . sunset() { city=${1-Seattle}; w3m "google.com/search?q=sunset:$city" | sed -r '1,/^\s*1\./d; /^\s*2\./,$d; /^$/d' ;} . "OneBox" is Google's term for that box that appears before the organic search results that has useful information that Google thinks you might be looking for (mathematical calculations, weather, currency conversions, and such). I'm not actually using OneBox correctly, but that's because I'm not sure that there is a "correctly". I looked for a command line API, but couldn't find one, so I settled on parsing stdout from the fantastic w3m web browser. I use the sed script to show only the first hit by deleting everything from the beginning of the file until it sees " 1." and then deleting everything from " 2." to the end of the file. Ugly and fragile, yes, but it works fine. . BUG1: w3m represents the picture of the sun rising, "weather_sunset-40.gif" as "[weat]" which is slightly confusing and probably should be removed. . BUG2: The output is more easily readable by a human, which means it's less useful for scripting. Show Sample Output


    0
    sunrise() { city=${1-Seattle}; w3m "google.com/search?q=sunrise:$city" | sed -r '1,/^\s*1\./d; /^\s*2\./,$d; /^$/d' ;}
    hackerb9 · 2010-11-02 21:24:23 3
  • Your version works fine except for someone who's interested in commands 'sudo' was prefixed to i.e. in your command, use of sudo appears as number of times sudo was used. Slight variation in my command peeks into what commands sudo was used for and counts the command (ignores 'sudo')


    0
    history | awk '{print $2,$3}' | sed s/sudo// | awk '{print $1}' | awk 'BEGIN {FS="|"}{print $1}' | sort | uniq -c | sort -n | tail | sort -nr
    b_t · 2010-11-17 12:15:04 3
  • Normally the bash builtin command 'set' displays all vars and functions. This just shows the vars. Useful if you want to see different output then env or declare or export. Alias 'sete' shows sets variables alias sete='set|sed -n "/^`declare -F|sed -n "s/^declare -f \(.*\)/\1 ()/p;q"`/q;p"' Alias setf shows the functions. alias setf='set|sed -n "/^`declare -F|sed -n "s/^declare -f \(.*\)/\1 ()/p;q"`/,\$p"' Also see: http://www.commandlinefu.com/commands/view/6899/print-all-environment-variables-including-hidden-ones At the very least, some cool sed commands! From my .bash_profile http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html Show Sample Output


    0
    alias sete='set|sed -n "/^`declare -F|sed -n "s/^declare -f \(.*\)/\1 ()/p;q"`/q;p"'
    AskApache · 2010-11-17 23:58:01 10

  • 0
    mv -i something.conf{,~} && sed "/regexp/s/^/#/" < something.conf~ > something.conf
    jasonjgw · 2010-11-18 00:10:45 4
  • useful if you want to get the source code of a in memory function Show Sample Output


    0
    source_print(){ set | sed -n "/^$1/,/^}$/p"; };
    glaudiston · 2010-11-24 15:55:34 4
  • This command finds all of the functions defined in any shell script you specify including .bashrc


    0
    functions(){ read -p "File name> "; sort -d $REPLY | grep "(){" | sed -e 's/(){//g' | less; }
    LinuxMan · 2010-12-01 18:49:48 7
  • This also works on non-Linux machines. If you have GNU sed you can do it more elegantly: ifconfig | sed -n 's/^\s*inet \(addr:\)\?\([^\s]*\) .*/\2/;T;/^127\./d;p'


    0
    ifconfig | sed -ne 's/^.*inet \(addr:\)*\([^ ]*\).*/\2/;te' -e 'd;:e' -e '/^127\./d;p'
    dramaturg · 2010-12-05 17:14:57 3
  • This will first remove any leading white space. If the line then starts with a comment character, it is cleared. If the result is an empty line, it's deleted. This allows for comment lines with leading white space.


    0
    sed 's/^[[:blank:]]*//; s/^#.*//; /^$/d' filename
    putnamhill · 2010-12-10 13:24:16 28
  • Im' not interested in images, but that's how I would do it.


    0
    curl -s http://boards.4chan.org/wg/|sed -r 's/.*href="([^"]*).*/\1\n/g'|grep images|xargs wget
    rodolfoap · 2010-12-12 06:32:19 3
  • GoAccess is an open source real-time Apache web log analyzer and interactive viewer that runs in a terminal in *nix systems. It provides fast and valuable HTTP statistics for system administrators that require a visual server report on the fly. http://goaccess.prosoftcorp.com/ Show Sample Output


    0
    sed -n '/05\/Dec\/2010/,$ p' access.log | goaccess -s -b
    allinurl · 2010-12-13 17:37:33 3
  • function for .bash_aliases that prints a line of the character of your choice in the color of your choice across the terminal. Default character is "=", default color is white.


    0
    println() {echo -n -e "\e[038;05;${2:-255}m";printf "%$(tput cols)s"|sed "s/ /${1:-=}/g"}
    joedhon · 2011-01-09 18:08:18 3
  • scrot, curl, egrep, sed, xsel, libnotify-bin must be installed. P.S. Sorry for so long command Show Sample Output


    0
    scrot $1 /tmp/screenshot.png && curl -s -F file1=@/tmp/screenshot.png -F submit="OMPLOAD\!" http://ompldr.org/upload | egrep '(View file: <a href="v([A-Za-z0-9+\/]+)">)' | sed 's/^.*\(http:\/\/.*\)<.*$/\1/' | xsel -b -i ? (full in a sample output)
    artleg · 2011-01-15 11:33:43 4
  • Default interface is wlan0, change as appropriate. This simply strips all the noise from the iwlist output in order to make it easier to skim. It makes sense to make this a very simple shell function, replacing the iwlist call with something like: iwlist "${1:-wlan0}" # append pipe etc. This makes wlan0 the default if the function receives no arguments. Show Sample Output


    0
    iwlist wlan0 scan | sed -ne 's#^[[:space:]]*\(Quality=\|Encryption key:\|ESSID:\)#\1#p' -e 's#^[[:space:]]*\(Mode:.*\)$#\1\n#p'
    unixmonkey17146 · 2011-01-25 14:50:34 5
  • "sort_csn" is a function to sort a comma separated list of numbers. Define the the function with this: sort_csn () { echo "${1}" | sed -e "s/,/\n/g"| sort -nu | awk '{printf("%s,",$0)} END {printf("\n")}' | sed -e "s/,$//"; } Use the function like this: sort_csn 443,22,80,8200,1533,21,1723,1352,25 21,22,25,80,443,1352,1533,1723,8200 One example where this is useful is when port scanning with nmap and getting a list of open ports in random order. If you use Nessus, you may need to create a scan policy for that set of specific ports and it is clearer to read with the port numbers in ascending order (left to right). Caveat: no spaces in the comma separated list (just number1,number2,number3,etc). A variation of this to sort a comma separated list of strings: sort_css () { echo "${1}" | sed -e "s/,/\n/g"| sort -u | awk '{printf("%s,",$0)} END {printf("\n")}' | sed -e "s/,$//"; } usage: sort_css apples,pears,grapes,melons,oranges apples,grapes,melons,oranges,pears Show Sample Output


    0
    sort_csn () { echo "${1}" | sed -e "s/,/\n/g"| sort -nu | awk '{printf("%s,",$0)} END {printf("\n")}' | sed -e "s/,$//"; }
    mpb · 2011-01-26 15:18:08 5
  • Changed wget to curl and it doesn't create a file anymore. Show Sample Output


    0
    curl http://www.discogs.com/search?q=724349691704 2> /dev/null | grep \/release\/ | head -2 | tail -1 | sed -e 's/^<div>.*>\(.*\)<\/a><\/div>/\1/'
    mrman · 2011-01-30 23:45:50 3
  • ur1.ca needs http:// on your URL


    0
    ur1() { curl -s --url http://ur1.ca/ -d longurl="$1" | sed -n -e '/Your ur1/!d;s/.*<a href="\(.*\)">.*$/\1/;p' ; }
    vando · 2011-02-06 16:07:00 7
  • ‹ First  < 27 28 29 30 31 >  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

Redirect incoming traffic to SSH, from a port of your choosing
Stuck behind a restrictive firewall at work, but really jonesing to putty home to your linux box for some colossal cave? Goodness knows I was...but the firewall at work blocked all outbound connections except for ports 80 and 443. (Those were wide open for outbound connections.) So now I putty over port 443 and have my linux box redirect it to port 22 (the SSH port) before it routes it internally. So, my specific command would be: $iptables -t nat -A PREROUTING -p tcp --dport 443 -j REDIRECT --to-ports 22 Note that I use -A to append this command to the end of the chain. You could replace that with -I to insert it at the beginning (or at a specific rulenum). My linux box is running slackware, with a kernel from circa 2001. Hopefully the mechanics of iptables haven't changed since then. The command is untested under any other distros or less outdated kernels. Of course, the command should be easy enough to adapt to whatever service on your linux box you're trying to reach by changing the numbers (and possibly changing tcp to udp, or whatever). Between putty and psftp, however, I'm good to go for hours of time-killing.

Simple colourized JSON formatting for BASH
Leave out pygmentize or `pip install pygments` first.

Protect your eye
Redshift will adjust the color temperature and protects eye at night -b : will adjust the brightness

Find 10 largest files in git history

Interactively build regular expressions
txt2regex can be interactive or noninteractive and generates regular expressions for a variety of dialects based on user input. In interactive mode, the regex string builds as you select menu options. The sample output here is from noninteractive mode, try running it standalone and see for yourself. It's written in bash and is available as the 'txt2regex' package at least under debian/ubuntu.

check open ports
Tested in Linux and OSX

Block an IP address from connecting to a server
This appends (-A) a new rule to the INPUT chain, which specifies to drop all packets from a source (-s) IP address.

adjust laptop display hardware brightness [non root]

use jq to validate and pretty-print json output
the `jq` tool can also be used do validate json files and pretty print output `cat file.json | jq` available on several platforms, including newer debian-based systems via `#sudo apt install jq`, mac via `brew install jq`, and from source https://stedolan.github.io/jq/download/

Filter the output of a file continously using tail and grep
The OPs solution will work, however on some systems (bsd), grep will not filter the data, unless the --line-buffered option is enabled.


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: