Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sed

awk

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.

like image 740
lord_nimon Avatar asked Oct 31 '25 03:10

lord_nimon


1 Answers

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
like image 66
anubhava Avatar answered Nov 02 '25 08:11

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!