Friday, October 19, 2012

Filter stderr for common commands

For work I use a Mac. Macs are totally broken in a lot of ways and when running utilities directly on the mac it has been the case more than once that I have to either suffer constant error messages or resort to trickery. Here's the trickery.


#============================================================
# ALIASES
#============================================================
alias vagrant="vagrant_suppress_gssapi_warnings"
alias puppet="puppet_suppress_iconv_warnings"


#============================================================
# FUNCTIONS
#============================================================
function vagrant_suppress_gssapi_warnings {
  {
    \vagrant "$@" 2>&1 >&3 | egrep -v 'Check your GSSAPI' >&2;
  } 3>&1
}

function puppet_suppress_iconv_warnings {
  {
    \puppet "$@" 2>&1 >&3 | egrep -v 'iconv will be depr' >&2;
  } 3>&1
}

Ruby Heredocs and whitespace


-    raise(Puppet::Error, "this is a really really long line that causes total terminal line wrap. That's something I typically try to avoid whenever I can!")
+    raise(Puppet::Error, <<-EOL.gsub(/\s+/, " ").strip)
+      this is a really really long line that thanks to a 
+      little bit of heredoc trickery can safely be multi
+      line, and the extra whitespace will be reduced down
+      to a single space. Pretty cool, eh!?
+    EOL

Saturday, October 6, 2012

Delete all blank lines and comments in Vim

Got a config file that doubles as a manual? Not any more:

:g/^$\|^#.*/d

Friday, May 18, 2012

Disks not in a zpool

Ever wish you had a simple one-liner to tell you which of your /dev/rdsk's weren't in a zpool? Well now you do...
    diff <(zpool status | awk '/c.t.d./ {print $1}' | sort) <(ls /dev/rdsk/c?t?d? | cut -f4 -d/ | sort) | awk '/^>/ {print "free disk: " $2}'

Monday, April 9, 2012

Git Submodule Grepping

git grep doesn't work well when submodules are involved. Some specialized workflows and situations require superprojects though, and git grep is a heck of a lot faster than actually opening and reading all the files with grep. Problem: doesn't work across submodules. Solution: alias.


[alias]
  sgrep = "!f() { git grep \"$1\"; git submodule foreach \"git grep '$1'; true\" | grep -B 1 \"$1\"; }; f"

Monday, March 5, 2012

How much memory have you allocated?

for i in `virsh list | grep running | cut -d " " -f 3 | grep -v 9`; do virsh dumpxml $i | grep memory | sed s/\// | sed s/\<\\/memory\>// ; done | xargs | sed s/\ /+/g | bc

Monday, October 24, 2011

Of cats and redirection...

Let's say you want to cat a file and redirect the output to itself, something like:

cat file > file;

Or perhaps more usefully:

cat file | tail -n 100 > file;

You will end up with an empty file. This is likely because of how the shell handles inodes and redirection (just a guess). A quick and way to get around this is:

cat file | tail -n 100 | dd of=file;

And viola! file points at the correct inode.