Where the open source database community meets: Use code PERCONA75 and secure your spot for Percona Live.  Register

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

June 15, 2009
Author
Baron Schwartz
Share this Post:

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:

0 0 votes
Article Rating
Subscribe
Notify of
guest

17 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Marc Handelman
16 years ago

Very nice, thanks!

deminy
16 years ago

Thanks for sharing.

Jeff Schroeder
16 years ago

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

julien antony
16 years ago

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

cheers.

Jacob Sohn
Jacob Sohn
16 years ago

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

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

-jacob

Jacob Sohn
Jacob Sohn
16 years ago

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

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

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

-jacob

julien antony
16 years ago

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

Seth
Seth
16 years ago

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

Seth
Seth
16 years ago

Should have said:

alias printmycnf=[insert perl one-liner]

Jacob Sohn
Jacob Sohn
16 years ago

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

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

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

-jacob

Seth
Seth
16 years ago

‘@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 = %sn., $1,$2; else printf .%sn., $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 = %sn”, $1,$2; else printf “%sn”, $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.

Seth
Seth
16 years ago

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

Jacob Sohn
Jacob Sohn
16 years ago

‘@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

Seth
Seth
16 years ago

I tried that, but it just hung.

Jacob Sohn
Jacob Sohn
16 years ago

‘@Seth

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

On my system;
$ alias printmycnf=’awk -F= “! /(^#|^$)/ { if ($2 != ””) printf ”%-34s = %sn”, $1,$2; else printf ”%sn”, $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.

Seth
Seth
16 years ago

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

Far
Enough.

Said no pioneer ever.
MySQL, PostgreSQL, InnoDB, MariaDB, MongoDB and Kubernetes are trademarks for their respective owners.
© 2026 Percona All Rights Reserved