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
  • 1
    can you show the initial output? Commented Apr 17, 2017 at 16:24

1 Answer 1

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
4
  • 1
    Pedantry dictates I have to highlight the "useless cat" here. column -t /tmp/output serves beautifully here. Commented Apr 17, 2017 at 16:38
  • 'column -t' works great for me. Thanks! Commented Apr 17, 2017 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? Commented Apr 30, 2017 at 20:09
  • 1
    @mamun, Yes, you are understanding it correctly. Commented May 1, 2017 at 15:40

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.