Easy way to display right-aligned with the awk command


I this short post I will show how to work with the awk command

Example log file

Case 1

Sometimes number of characters in the column is different, so it is difficult to see. Use awk to display the columns right justified.

root@vagrant:/home/vagrant# cat test.log | awk '{printf("%10s %10s %10s %10s %10s\n",$1,$4,$5,$9,$10)}'                                                       2018/8/5 1927402673     273564 lsjdnh:666 snhdgs:000
2018/9/15 72946629773 285275 lsjdnh:342 snhdgs:111
2018/10/5 8203749274 196283 lsjdnh:124 snhdgs:222
2018/11/21 9253772253 197026 lsjdnh:634 snhdgs:333
2019/1/2 8241196002 102836 lsjdnh:745 snhdgs:444

Case 2

If the date notation in the log file is as follows, it may be difficult to see. In such a case, it will be easier to see if you convert the date format to yyyy/mm/dd with the awk command and display it.

root@vagrant:/home/vagrant# cat test.log | awk -F '[ /]' '{printf("%4d/%02d/%02d %08s %10s %10s %6s %7s %3s %3s %10s %10s\n",$1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12)}'
2018/08/05 00:03:10 [LEVEL4] 1927402673 273564 KEY001 asd zxc lsjdnh:666 snhdgs:000
2018/09/15 10:04:11 [LEVEL4] 72946629773 285275 KEY002 asd zxc lsjdnh:342 snhdgs:111
2018/10/05 21:31:05 [LEVEL4] 8203749274 196283 KEY0003 asd zxc lsjdnh:124 snhdgs:222
2018/11/21 08:13:40 [LEVEL4] 9253772253 197026 KEY0004 asd zxc lsjdnh:634 snhdgs:333
2019/01/02 22:03:22 [LEVEL4] 8241196002 102836 KEY0005 asd zxc lsjdnh:745 snhdgs:444

 by the author.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


DevOps Consultant. I’m strongly focused on automation, security, and reliability.