June 15, 2009

How to pretty-print my.cnf with a one-liner

Posted by Baron Schwartz |

When I'm looking at a server, I often want to see the /etc/my.cnf file nicely formatted, and with comments stripped. This Perl one-liner will pretty-print the file:

CODE:
  1. perl -ne 'm/^([^#][^\s=]+)\s*(=.*|)/ && printf("%-35s%s\n", $1, $2)' /etc/my.cnf
  2. [client]                           
  3. port                               = 3306
  4. socket                             = /var/run/mysqld/mysqld.sock
  5. [mysqld_safe]                     
  6. socket                             = /var/run/mysqld/mysqld.sock
  7. nice                               = 0
  8. [mysqld]                           
  9. user                               = mysql
  10. pid-file                           = /var/run/mysqld/mysqld.pid
  11. socket                             = /var/run/mysqld/mysqld.sock
  12. port                               = 3306
  13. ....

Related posts: :The ultimate tool for generating optimal my.cnf files for MySQL::Poor man’s query logging::Linux failing to boot screen on the plane:
 

17 Comments »

  1. Very nice, thanks!

    Comment :: June 15, 2009 @ 7:51 am

  2. Thanks for sharing.

    Comment :: June 15, 2009 @ 8:05 am

  3. Besides forcing the spacing with the printf, this looks the same as: egrep -v ‘^$|^#’ /etc/my.cnf

    Comment :: June 15, 2009 @ 9:38 am

  4. Nice, i like awk so i give you mine with sections separated by a line:
    awk ‘! /^#/ && ! /^$/ {if($1 ~ /^\[/ ){gsub(”\[”,”\n[”,$1) };printf(”%-35s%s %s\n”,$1, $2, $3)}’ /etc/my.cnf

    cheers.

    Comment :: June 15, 2009 @ 12:50 pm

  5. 5. Jacob Sohn

    shorter version – :)
    awk ‘! /(^#|^$)/ {printf “%-34s %s %s\n”, $1, $2, $3}’ /etc/my.cnf

    if you don’t care about pretty formats – :)
    grep -Ev ‘(^#|^$)’ /etc/my.cnf

    -jacob

    Comment :: June 15, 2009 @ 5:30 pm

  6. The awk versions work well as long as there’s whitespace, but they won’t pretty-print

    port=3306

    I like awk too, but I’m so clumsy with it that I’m not sure what to do to fix that!

    (PS: the Perl version doesn’t pretty-print things without whitespace perfectly either, but it’s pretty enough for me)

    Comment :: June 15, 2009 @ 6:37 pm

  7. 7. Jacob Sohn

    using field separator option and if condition, with or without white space shows pretty format – :)

    awk -F= ‘! /(^#|^$)/ { if ($2 != “”) printf “%-34s = %s\n”, $1,$2; else printf “%s\n”, $1 }’ /etc/my.cnf

    I admit, this is longer than i want it to be…

    -jacob

    Comment :: June 15, 2009 @ 10:55 pm

  8. for pretty output with colors, you can use this python script: http://www.linibou.com/colorex/ also useful for logs.

    Comment :: June 16, 2009 @ 12:13 am

  9. 9. Seth

    Any hints on how to get this to work as a bash alias?

    I would like to add something like this to my .bashrc:

    alias printmycnf=

    but no amount of quote hacking is getting it to work.

    Or has someone refactored it has a perl script? I think I could do that, but my first attempt was taking longer than 5 minutes so I thought I would ask.

    Thanks

    Comment :: June 17, 2009 @ 10:11 am

  10. 10. Seth

    Should have said:

    alias printmycnf=[insert perl one-liner]

    Comment :: June 17, 2009 @ 10:12 am

  11. 11. Jacob Sohn

    @Seth
    knowning where to escape, you should be able to do as this;

    alias printmycnf=’awk -F= “! /(^#|^$)/ { if (\$2 != \”\”) printf \”%-34s = %s\n\”, \$1,\$2; else printf \”%s\n\”, \$1 }”‘

    you can replace with perl one-liner where awk is.

    -jacob

    Comment :: June 17, 2009 @ 8:13 pm

  12. 12. Seth

    @Jacob – Not sure what’s going on here… but the quoting is messed up. Appears to be the MS-SmartQuotes / unicode issue. Some of the double-quotes above are leaning one direction and some are leaning the other way. When I paste it as is into my terminal I get:

    alias printmycnf=.awk -F= .! /(^#|^$)/ { if (\$2 != \.\.) printf \.%-34s = %s\n\., \$1,\$2; else printf \.%s\n\., \$1 }..

    Did you copy and paste from somewhere weird, like a word document or something? I tried to modify it back to what it should be:

    alias printmycnf=’awk -F= “! /(^#|^$)/ { if (\$2 != \”\”) printf \”%-34s = %s\n\”, \$1,\$2; else printf \”%s\n\”, \$1 }”‘

    But when I ran it (RHEL 4.8) it just hung. I’ve tried inserting the perl string as an alias and doing various things to escape the quotes w/l much luck.

    I guess I could just put the command into a shell script for now.

    Comment :: June 17, 2009 @ 8:27 pm

  13. 13. Seth

    Appears that WordPress is messing up the quotes for us, as my post is exhibiting the same quoting issue.

    Comment :: June 17, 2009 @ 8:28 pm

  14. 14. Jacob Sohn

    @Seth

    Yeah, the “,’,` characters gets altered from ISO-8859-1 to UTF-8 probably. Copy and paste the code and just replace those characters with proper ones. Then all should work fine.

    -jacob

    Comment :: June 17, 2009 @ 9:43 pm

  15. 15. Seth

    I tried that, but it just hung.

    Comment :: June 18, 2009 @ 12:30 am

  16. 16. Jacob Sohn

    @Seth

    have you tried “printmycnf /etc/my.cnf” after alias command?

    On my system;
    $ alias printmycnf=’awk -F= “! /(^#|^$)/ { if (\$2 != \”\”) printf \”%-34s = %s\n\”, \$1,\$2; else printf \”%s\n\”, \$1 }”‘
    $ printmycnf /etc/my.cnf

    works like a charm.

    ps: don’t forget to replace single quote and double quote. better yet, just type it in manually.

    Comment :: June 23, 2009 @ 4:39 pm

  17. 17. Seth

    Thanks, I got it working now. I don’t think I was including the filename before.

    Comment :: June 23, 2009 @ 5:53 pm

 

Subscribe without commenting

Trackbacks/Pingbacks