Blog 1

Random Talk on Random Thoughts

Awk Column Alignment

| Comments |

Background

I use Google’s PageSpeed Insights to check if the images on this blog are optimised. Recently, it reported that two images in An Invalid Claim by the Government could be resized and compressed. I did so, and then the file size of the files were displayed using ls -lh.

[owner@localhost ~/octopress/source/images/posts/W3CInvalid]$ ls -lh
total 312K
-rw-rw-r-- 1 owner owner  32K Apr  9 12:20 cedb1-300.png
-rw-rw-r-- 1 owner owner 132K Mar 27 14:32 cedb1.png
-rw-rw-r-- 1 owner owner  32K Apr  9 12:20 cedb2-300.png
-rw-rw-r-- 1 owner owner  83K Apr  9 12:20 cedb2.png

Problem

To remove the first four column, one uses awk to do this. (I assume that the current working directory is the same as above.)

$ ls -lh | awk '{print $5 $6 $7 $8 $9}'

32KApr912:20cedb1-300.png
132KMar2714:32cedb1.png
32KApr912:20cedb2-300.png
83KApr912:20cedb2.png

How can one properly align the columns?

Solution

Searching “awk align columns” on Google, I quickly got an answer posted by Mike Sherrill on Stack Overflow. However, there’s room for improvement in the visual effect of the output.

$ ls -l | awk '{printf("%6s %s %2s %s %13s\n", $5, $6, $7, $8, $9);}'
                         
 32718 Apr  9 12:20 cedb1-300.png
135159 Mar 27 14:32     cedb1.png
 32665 Apr  9 12:20 cedb2-300.png
 84580 Apr  9 12:20     cedb2.png

The solution can be found in the second search result for the Google search for “awk align columns left right”. Added a hyphen after % will suffice. Note that the newline \n can’t be omitted. Otherwise, the output will be messed up.

$ ls -l | awk '{printf("%6s %s %2s %s %-s\n", $5, $6, $7, $8, $9);}'
                         
 32718 Apr  9 12:20 cedb1-300.png
135159 Mar 27 14:32 cedb1.png    
 32665 Apr  9 12:20 cedb2-300.png
 84580 Apr  9 12:20 cedb2.png    

Comments