mareks
mareks

Reputation: 491

Convert initialised variables to declared variables

I am restructuring my code and I have a long list of initialised variables:

int i = 3;

etc.

What is an easy way of converting this into a list of declarations? These would be:

int i;

etc.

Upvotes: 0

Views: 66

Answers (1)

mavam
mavam

Reputation: 12562

You can use any tool for automated text-processing for this task. A particularly apt and well-known one is sed. Just pipe your source code through it:

sed -e 's/\(\s*\)\([a-zA-Z_][a-zA-Z0-9_]*\) \([a-zA-Z_][a-zA-Z0-9_]*\) =.*/\1\2 \3;/' code/src/*.{cc,cpp,c}

The regular expression above uses [a-zA-Z_][a-zA-Z0-9_] to represent a C identifier. (Based on the consistency of your coding style, you may have to be more clever with some of the whitespace.)

Add the switch -i for an in-place replacement. This changes your file directory, so be careful.

Upvotes: 1

Related Questions