Shell snippet that prepends a nice to a command's output.
For example, here's the (snipped) output of VAGRANT_LOG=debug vagrant provision:
INFO environment: Home path: /Users/dergachev/.vagrant.d
WARN environment: No local data path is set. Local data cannot be stored.
WARN environment: In a bundler environment, not loading environment plugins!
INFO environment: Running hook: environment_load
INFO environment: Initializing config...
Would you prefer to see the following?
17:47:19:938 INFO environment: Home path: /Users/dergachev/.vagrant.d 17:47:19:939 WARN environment: No local data path is set. Local data cannot be stored. 17:47:19:939 WARN environment: In a bundler environment, not loading environment plugins! 17:47:19:939 INFO environment: Running hook: environment_load 17:47:19:948 INFO environment: Initializing config...
Then simply pipe the command to | ruby -ne 'puts "\e[33m" + Time.now().strftime("%T.%L") + "\e[0m " + $_', as follows:
VAGRANT_LOG=debug vagrant provision 2>&1 | ruby -ne 'puts "\e[33m" + Time.now().strftime("%T.%L") + "\e[0m " + $_'
Keep in mind the following:
2>&1redirects stderr to stdout, which allows the script to work on it too.\e[33mand\e[0mare terminal escape codes that make the text between them yellow%Tis HH:MM:SS and%Ladds milliseconds in the time formatting command.
Prefer to invoke the script in a friendlier way?
vagrant up 2>&1 | prepend-timestampThen do the following:
##
## Creates a ~/bin folder and ensure it's in $PATH;
## via http://askubuntu.com/a/247422/194314
##
mkdir ~/bin
cat - >> ~/.bashrc <<'EOT'
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi
EOT
source ~/.bashrc
##
## Installs ~/bin/prepend-timestamp and makes it executable
##
cat - > ~/bin/prepend-timestamp <<'EOT'
#!/bin/sh
ruby -ne 'puts "\e[33m" + Time.now().strftime("%T.%L") + "\e[0m " + $_'
EOT
chmod u+x ~/bin/prepend-timestamp
Done!