1

Is there any way to format the output of a grep command to align the columns nicely? For example:

4TO/struct-2222332/coord-2222332.out:          FINAL HEAT OF FORMATION =       -299.61496 KCAL/MOL =   -1253.58899 KJ/MOL
2E/struct-1212123/coord-1212123.out:          FINAL HEAT OF FORMATION =       -299.61496 KCAL/MOL =   -1253.58901 KJ/MOL
OT4/struct-2222121/coord-2222121.out:          FINAL HEAT OF FORMATION =       -299.61497 KCAL/MOL =   -1253.58904 KJ/MOL
E3/struct-1312322/coord-1312322.out:          FINAL HEAT OF FORMATION =       -299.61497 KCAL/MOL =   -1253.58903 KJ/MOL  

I want to allocate 40 spaces for column one and align them from the left. Similarly, do the same for other columns based on the expected maximum size of each column.

  • 1
    can you show the initial output? – RomanPerekhrest Apr 17 '17 at 16:24
4

You can use the column command (if you're using Linux), to format the data into tab delimited columns. I don't think you can specify a different column width for the first column vs other columns, though.

bash-[24]$ column -t /tmp/output
4TO/struct-2222332/coord-2222332.out:  FINAL  HEAT  OF  FORMATION  =  -299.61496  KCAL/MOL  =  -1253.58899  KJ/MOL
2E/struct-1212123/coord-1212123.out:   FINAL  HEAT  OF  FORMATION  =  -299.61496  KCAL/MOL  =  -1253.58901  KJ/MOL
OT4/struct-2222121/coord-2222121.out:  FINAL  HEAT  OF  FORMATION  =  -299.61497  KCAL/MOL  =  -1253.58904  KJ/MOL
E3/struct-1312322/coord-1312322.out:   FINAL  HEAT  OF  FORMATION  =  -299.61497  KCAL/MOL  =  -1253.58903  KJ/MOL

Your other option would be to parse the files, split the fields, and process the columns yourself with awk and printf().

bash-[54]$ awk '{printf "%-40s",$1 ; $1=""; printf "%-s\n",$0}' /tmp/output
4TO/struct-2222332/coord-2222332.out:    FINAL HEAT OF FORMATION = -299.61496 KCAL/MOL = -1253.58899 KJ/MOL
2E/struct-1212123/coord-1212123.out:     FINAL HEAT OF FORMATION = -299.61496 KCAL/MOL = -1253.58901 KJ/MOL
OT4/struct-2222121/coord-2222121.out:    FINAL HEAT OF FORMATION = -299.61497 KCAL/MOL = -1253.58904 KJ/MOL
E3/struct-1312322/coord-1312322.out:     FINAL HEAT OF FORMATION = -299.61497 KCAL/MOL = -1253.58903 KJ/MOL
  • 1
    Pedantry dictates I have to highlight the "useless cat" here. column -t /tmp/output serves beautifully here. – steve Apr 17 '17 at 16:38
  • 'column -t' works great for me. Thanks! – mamun Apr 17 '17 at 17:40
  • Though, I am using column -t, as it is convenient, but I want to understand the second option too. My understanding is, 1) use 40 space to print column one (- for left alignment) 2) then replace column 1 with empty character or basically removing column one from the whole record 3) then print the whole record with left alignment. Am I right? – mamun Apr 30 '17 at 20:09
  • 1
    @mamun, Yes, you are understanding it correctly. – Tim Kennedy May 1 '17 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.