I am using Awk inside a bash shell script to search header files for c++ functions so I can add these functions to another file. All of the functions I need will have form:
type getName()
or
type setName(type value)

Right now I get all the functions listed in between two points in the file (between "public:" and "private:"). This works fine:

variable=$(awk '/public:/ {flag=1;next}/private:/ {flag=0} flag {print}' $file) ;

But when I search the resulting string, I only get everything separated by spaces (i.e. bool getSilenceDetection() would return as bool, getSilenceDetection() and void setSilenceDetection(bool detection) would return as void, setSilenceDetection(bool, detection). Here is the code I am using for this:

for word in $variable; do
            function=$(awk '/[A-Za-z]* (set|get)[A-Za-z]*\((.*|.* .*)\)/' $word)
            echo
        done

Unfortunately, this then says "awk: cannot open \<whatever the contents of the string were> (No such file or directory). Why is it not picking up them when they're separated by spaces and why is it thinking they're files? Here is the example file fragment I use:

public:
        AudioSettings();
        double getVolume() { return volume_; }
        void setVolume(double volume) { volume_ = volume < 1.00 ? volume : 1.00; }
        bool getEchoCancellation() { return echoCancellation_; }
        void setEchoCancellation(bool cancelation) { echoCancellation_ = cancelation; }
        bool getSilenceDetection() {return silenceDetection_; }
        void setSilenceDetection(bool detection) { silenceDetection_ = detection; }
        string getAudioSettingsASCIIString();
        bool saveConfiguration();

    private:

And here is what I would want to get out of that:

double getVolume()
    void setVolume(double volume)
    bool getEchoCancellation()
    void setEchoCancellation(bool cancelation)
    bool getSilenceDetection()
    void setSilenceDetection(bool detection)
    string getAudioSettingsASCIIString()
    bool saveConfiguration()

Can someone help me with this? Thanks

hi,

isn't that what you want ?

awk '/public:/ {flag=1;next}/private:/ {flag=0} flag && /[A-Za-z]* (set|get)[A-Za-z]*\((.*|.* .*)\)/ { split($0,a,")"); print a[1]")" }' "$file"

double getVolume()
void setVolume(double volume)
bool getEchoCancellation()
void setEchoCancellation(bool cancelation)
bool getSilenceDetection()
void setSilenceDetection(bool detection)
string getAudioSettingsASCIIString()

That gives me:

void setVolume(double volume) void setEchoCancellation(bool cancelation) void setSilenceDetection(bool detection)

It does not give me the "get" functions (the ones with no arguments), and it doesn't put them on their own lines (I have to further process them after separating out the functions I want, so each function that matches the regex needs to be somehow demarcated.

Never mind. I mistyped one of the symbols. It does give me the right functions:

double getVolume() void setVolume(double volume) bool getEchoCanellation() void setEchoCancellation(bool cancelation) bool getSilenceDetection() void setSilenceDetection(bool detection) string getAudioSettingsASCIIString()

But it's still not separated. Any thoughts on how to do this?

Thanks, that worked to output the appropriate functions. However, once I separate the functions I need to operate on each of them using some other code I've written. I was trying to use a for loop (like for line in $variable), but this operates on each word, not each function.

Edited 4 Years Ago by iamthesgt

pipe the awk statement to a while loop.

as you began with awk, don't you want to go on with it? it'd be better.

I ended up piping the statement to a temporary file (of just the functions I need), then parsing each function individually into arrays for each value (returntype, propertytype, etc...) and this can be accessed by the index (same across all arrays for each function). This should suit my needs. Thanks for all your help.

This question has already been answered. Start a new discussion instead.