ReminiscientYeti ReminiscientYeti - 1 year ago 65
Bash Question

Identify arrays using regular expressions using grep in bash

I'm trying to write a bash script that takes input as C file and uses grep and regular expression to identify and extract array declarations and definitions to dump it on the terminal.

grep "[a-zA-Z][\0-9]" << $filename


But it doesn't seem to work.

Edit 1:
For example, if I have the following C code:

int main( void )
{
int a[N], b[N], c[N];

for (int i=0; i<N; i++)
{
a [i] = -i;
b[i] = i * i;
}
}


Is there a way I could extract the array declarations and definitions using regular expressions in grep?

Answer Source

I figured it out.

for((i=1;i<=$arr;i++))
do
    temp=$( grep -m$i "[a-z][a-z][a-z][a-z]*\s[a-zA-Z][a-zA-Z0-9_]*\[[a-zA-Z0-9]*\]" < $filename | tail -n1 )
    echo "$i $temp"
done
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download