Search code examples
awksed

sed/awk expression to prepend text and append increasing numbers to list


I want to convert this list:

DISPATCH_C
HELLO_C
INTERFACES_C
LIST_C

to this list:

#define LOG_FILE_DISPATCH_C         1
#define LOG_FILE_HELLO_C            2
#define LOG_FILE_INTERFACES_C       3
#define LOG_FILE_LIST_C             4

How can I do this using sed or awk? I thought it would be something like

sed -En '/(.*)/#define LOG_FILE_\1   {=;p;}/'

but that just gives me an error. Apparently sed is interpreting the "#define" as a command and I don't know how to fix that.


Solution

  • You can may use this awk solution:

    awk '{printf "#define LOG_FILE_%-30s%d\n", $1, NR}' file
    
    #define LOG_FILE_DISPATCH_C                    1
    #define LOG_FILE_HELLO_C                       2
    #define LOG_FILE_INTERFACES_C                  3
    #define LOG_FILE_LIST_C                        4