(Couldn't come with any better title)
I'm trying to convert a number of lines, like these:
#define GENERIC_TYPE_METER_PULSE 0x30 /*Pulse Meter*/
#define SPECIFIC_TYPE_NOT_USED 0x00 /*Specific Device Class not used*/
......
#define MFG_ID_WAYNE_DALTON 0x0008 //Wayne Dalton
#define MFG_ID_WILSHINE_HOLDING_CO_LTD 0x012D //Wilshine Holding Co., Ltd
#define MFG_ID_WIDOM 0x0149 //wiDom
......
#define COMMAND_CLASS_ALARM 0x71
#define COMMAND_CLASS_ALARM_V2 0x71
#define COMMAND_CLASS_NOTIFICATION_V3 0x71
#define COMMAND_CLASS_NOTIFICATION_V4 0x71
in a file (c++) to something like these (for Java):
SPECIFIC_TYPE_NOT_USED((byte)0x00) /*Specific Device Class not used*/
MFG_ID_WILSHINE_HOLDING_CO_LTD((byte)0x012D) //Wilshine Holding Co., Ltd
COMMAND_CLASS_ALARM((byte)0x71)
....
I came up with this:
gawk '/^#define/ && / [[:xdigit:]]/ { printf "%s((byte)%s)\n",$2,$3 }'
but there are two issues - it doesn't work with awk
; requires GNU-awk (gawk
) and I also need the end-of-the-line comments in the output whenever available. How can I do that? I'm especially interested using awk
but can live with sed
as well. Cheers!!
Change the
2
inmaxLgth+2
to whatever spacing you want between the longest value and it's associated comment.You can use this awk one-liner:
EDIT: Here is an awk that you can try for better alignment of comments:
OUTPUT:
one
sed
solution, i just tested once, so first test some casesfor a proper format, ( in my sense, it is aligning the comment ) , i was not able to come up with any
sed
solution, ( i am interested in one ) here is anawk
solution ( looks like a little clumsy, but self explanatory..