Commands using sed (1,209)

  • Using "", which allow bash commands to be interpreted by sed scripts, I've embedded a sed script in another sed substitution, in case, the 4th line of the file at the end of the second one. Show Sample Output


    0
    $ sed "2s/$/ $(sed -n '4p' sound.desktop)/" sound.desktop
    pibarnas · 2016-08-23 02:50:31 0
  • When bundle install sucks ...This runs isuckat_ruby.rb and when stderror matches find gem ' it will gem install what ever is missing ... Show Sample Output


    0
    gem install `ruby ./isuckat_ruby.rb 2>&1 | sed -e 's/.*find gem .//g' -e 's/ .*//g' | head -n 1`
    operat0r · 2016-08-03 19:41:27 0
  • N: On the current line, sed will display it on pattern space, plus a \n (new line); but s/\n//: Will get rid of new line displayed on pattern space, joining the current line's end with the start of the next line Useful in scripts. Show Sample Output


    2
    sed 'X{N;s/\n//;}' file.txt (where X is the current line)
    pibarnas · 2016-07-30 14:27:20 0

  • 0
    function csv() { C=`echo -e '\e[0;31m'`; N=`echo -e '\033[0m'`; for i in $@; do column -s, -t < $i | sed "1 s|^\(.*\)$|$C\1$N|gi"; done; }; csv test*.csv
    oso · 2016-07-24 21:14:52 0
  • Replace thread_link with the link of the thread you want to download images of.


    0
    wget thread_link -qO - | sed 's/\ /\n/g' | grep -e png -e jpg | grep href | sed 's/href\=\"/http:/g' | sed 's/"//g' | uniq | xargs wget
    hztrance · 2016-07-16 20:43:33 0

  • 0
    tr "\|\;" "\n" < ~/.bash_history | sed -e "s/^ //g" | cut -d " " -f 1 | sort | uniq -c | sort -rn | head -20
    turrtle13 · 2016-07-01 19:27:12 0
  • Starting with a large MySQL dump file (*.sql) remove any lines that have inserts for the specified table. Sometimes one or two tables are very large and uneeded, eg. log tables. To exclude multiple tables you can get fancy with sed, or just run the command again on subsequently generated files.


    1
    sed '/INSERT INTO `unwanted_table`/d' mydb.sql > reduced.sql
    sudopeople · 2016-06-24 20:13:47 0
  • Retrieves AS route prefixes for IPv4 and IPv6, aggregates the routes to the minimal set, and adds netfilter rules to reject them. Relies on two helpers: IPv4 - "aggregate" by Joe Abley (package name 'aggregate'), IPv6 - "aggregate6" by Job Snijders ( https://github.com/job/aggregate6 ) Show Sample Output


    0
    ASN=32934; for IP in 4 6; do whois -h riswhois.ripe.net \!${IP/4/g}as${ASN} | sed -n '2 p' | tr \ \\n | aggregate${IP/4/} | while read NET; do ip${IP/4/}tables -I INPUT -S ${NET} -j REJECT; done; done
    iam_TJ · 2016-05-29 09:45:34 0
  • Check that all websites on the current server are working as expected. This is a quick easy way to do that. Show Sample Output


    0
    grep -h 'Server\(Name\|Alias\)' /etc/httpd/conf.d/*.conf | sed 's#\s*Server\(Name\|Alias\)\s*##g' | tr ' ' "\n" | xargs -I{} sh -c 'echo {},`curl -s -o /dev/null -w "%{http_code}" {}`'
    AmadoMartinez · 2016-05-13 17:16:52 0
  • retrieve file names back from touch commands for them Show Sample Output


    1
    touch files.txt; cat reorder_files.sh | while read line; do x=`echo $line | sed 's/touch \([a-z0-9\.]\+.*.pdf\);.*/\1/'`; echo $x >> files.txt ; done;
    programmer · 2016-04-19 11:01:17 1
  • Above example is upgrading Solaris from 11.1SRU20.0.5 to 11.2SRU15.0.5. Patches are not applied options "-nv" are for dryrun and verbose output. Show Sample Output


    0
    pkg update -nv | sed -n '/entire/{N;p;}'
    apaton · 2016-04-15 20:48:49 0

  • 0
    docker stats --no-stream $( docker ps -q ) | sed -e "$( docker ps --format "{{.ID}} {{.Names}}" | sed -e "s/\(.*\) \(.*\)/s\/\1\/\2\t\/g;/" )"
    gtron · 2016-04-14 15:20:13 0
  • It's quite fun to invert text using "flip.pl" (ref: http://ubuntuforums.org/showthread.php?t=2078323 ). Slightly more challenging is to flip a whole "cowsay". :-) Show Sample Output


    1
    echo Which way up? | flip.pl | cowsay | tac | sed -e "s,/,+,g" -e "s,\\\,/,g" -e "s,+,\\\,g" -e "s,_,-,g" -e "s,\^,v,g"
    mpb · 2016-04-08 11:41:44 2

  • 3
    ss -t -o state established '( dport = :443 || dport = :80 )'|grep tcp|awk '{ print $5 }'|sed s/:http[s]*//g|sort -u|netcat whois.cymru.com 43|grep -v "AS Name"|sort -t'|' -k3
    koppi · 2016-04-07 01:48:44 2
  • Requires: curl xsel access to the internet(http://transfer.sh) This is an alias utilizing the transfer.sh service to make sharing files easier from the command line. I have modified the alias provided by transfer.sh to use xsel to copy the resulting URL to the clipboard. The full modified alias is as follows since commandlinefu only allows 255 characters: transfer() { if [ $# -eq 0 ]; then echo "No arguments specified. Usage:\necho transfer /tmp/test.md\ncat /tmp/test.md | transfer test.md"; return 1; fi if tty -s; then basefile=$(basename "$1" | sed -e 's/[^a-zA-Z0-9._-]/-/g'); curl --progress-bar --upload-file "$1" "https://transfer.sh/$basefile" |xsel --clipboard; else curl --progress-bar --upload-file "-" "https://transfer.sh/$1" |xsel --clipboard ; fi; xsel --clipboard; } Show Sample Output


    2
    transfer() { basefile=$(basename "$1" | sed -e 's/[^a-zA-Z0-9._-]/-/g');curl --progress-bar --upload-file "$1" "https://transfer.sh/$basefile"|xsel --clipboard;xsel --clipboard ; }
    leftyfb · 2016-03-20 19:38:48 0
  • Dependencies on phone: adb access, screencap command, base64 command. Dependencies on computer: adb, sed, base64, display (from imagemagick, but can substitute other image viewer which reads from stdin). This should work around adb stupidies (i.e. that it replaces \n with \r\n) with base64.


    4
    adb shell "screencap -p | base64" | sed 's/\r$//' | base64 -d | display
    T4b · 2016-02-07 18:46:35 1
  • Nginx (and other webservers like Apache) can be awkward to trace. They run as root, then switch to another user once they're ready to serve web pages. They also have a "master" process and multiple worker processes. The given command finds the process IDs of all Nginx processes, joins them together with a comma, then traces all of them at once with "sudo strace." System trace output can be overwhelming, so we only capture "networking" output. TIP: to kill this complex strace, do "sudo killall strace". Compare with a similar command: http://www.commandlinefu.com/commands/view/11918/easily-strace-all-your-apache-processes Show Sample Output


    1
    sudo strace -e trace=network -p `pidof nginx | sed -e 's/ /,/g'`
    shavenwarthog · 2016-01-28 18:48:16 0
  • use get_iplay to download all listed content from http://www.bbc.co.uk/radio4extra run every night to make sure no episodes are missed


    0
    get_iplayer --type=radio --channel "Radio 4 Extra" | grep : | awk '{ if ( NR > 1 ) { print } }'|sed 's/:.*//' |sed '$ d' > pidlist && while read p; do get_iplayer --get --fields=pid $p; done <pidlist && rm pidlist
    dunryc · 2016-01-16 17:20:54 0
  • Print a git log (in reverse order) giving a reference relative to HEAD. HEAD (the current revision) can also be referred to as HEAD~0 The previous revision is HEAD~1 then HEAD~2 etc. . Add line numbers to the git output, starting at zero: ... | nl -v0 | ... . Insert the string 'HEAD~' before the number using sed: ... | sed 's/^ \+/&HEAD~/' . Thanks to bartonski for the idea :-) Show Sample Output


    1
    git log --oneline | nl -v0 | sed 's/^ \+/&HEAD~/'
    flatcap · 2015-11-23 21:35:57 0

  • 3
    find <mydir> -type f -exec sed -i 's/<string1>/<string2>/g' {} \;
    Plancton · 2015-11-12 14:47:18 1

  • -5
    ls -1 | while read a; do mv "$a" `echo $a | sed -e 's/\ //g'`; done
    antonangeli · 2015-10-27 12:52:47 2
  • This command validates if exist any character different in 0-255 and dot. If any characters different is typed the error menssage is showing. Show Sample Output


    0
    echo ${IP} | sed "s/[0-9\.]//g"
    andregyn62 · 2015-10-19 18:20:03 1

  • 0
    adb shell screencap -p | sed 's/\r$//' > FILENAME.PNG
    alikhalil · 2015-10-07 09:42:36 4
  • Use -q as first argument (as described in `man curl`) to ignore curlrc to ensure the output is always the same regardless of user's configuration. Show Sample Output


    0
    curl -q -s http://www.hasthelhcdestroyedtheearth.com/ | sed -En '/span/s/.*>(.*)<.*/\1/p'
    Tatsh · 2015-09-23 23:26:30 1
  • This says if the LHC has destroyed the world. Run it in a loop to monitor the state of Earth. Might not work reliable, if the world has actually been destroyed. Show Sample Output


    1
    curl -s http://www.hasthelhcdestroyedtheearth.com/ | sed -En '/span/s/.*>(.*)<.*/\1/p'
    BeniBela · 2015-09-23 12:30:31 2
  •  < 1 2 3 4 5 >  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



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: