Combining non zero elements in array of header file

0

I have 3 C header file defining a couple of arrays with their initial values. I need to combine similar variables which has non-zeros into one.

It will be easier to describe with the sample:

first.h:

...
...
uint32_t _commonData_first[]={
    0x00000000,
    0x00000000,
    0x0002ae3c,
    0x00000000,
    0x00000000,
    0x0002ba7c,
};

second.h:
...
...
uint32_t _commonData_second[]={
    0x00000000,
    0x00020e00,
    0x00000000,
    0x00000000,
    0x00023bd4,
    0x00000000,
};

third.h:
...
...
uint32_t _commonData_third[]={
    0x00001ef8,
    0x00000000,
    0x00000000,
    0x00003a5c,
    0x00000000,
    0x00000000,
    };

I want the first.h, second.h, and third.h to have end up like this:

in first.h:
uint32_t _commonData_first[]={
    0x00001ef8,
    0x00020e00,
    0x0002ae3c,
    0x00003a5c,
    0x00023bd4,
    0x0002ba7c,
};

in second.h:
uint32_t _commonData_second[]={
    0x00001ef8,
    0x00020e00,
    0x0002ae3c,
    0x00003a5c,
    0x00023bd4,
    0x0002ba7c,
};

third.h:
uint32_t _commonData_third[]={
    0x00001ef8,
    0x00020e00,
    0x0002ae3c,
    0x00003a5c,
    0x00023bd4,
    0x0002ba7c,
};

What is the best way to do this using shell script?

python
bash
shell
awk
asked on Stack Overflow Aug 2, 2018 by electro

1 Answer

0

Could you please try following:

awk '/^[a-zA-Z]+\.h:$/{filename=$0;next} /^uint32_t.*/{flag=1} flag{print > filename} /}\;/{flag=filename=""}' Input_file

OR

awk '
/^[a-zA-Z]+\.h:$/{    ##Checking condition if a line starting from small or capital letters with .h: at last then do following.
  filename=$0         ##Creating a variable named filename whose value is current line value.
  next                ##next keyword will skip all further statements from here.
}
/^uint32_t.*/{        ##Checking condition if a line starts from uint32_t then do following.
  flag=1              ##Setting variable flag value to 1 here.
}
flag{                 ##Checking if variable flag is SET then do following.
  print > filename    ##Printing current line to variable filename.
}
/}\;/{                ##Checking condition here if a line has }; in them do following then.
  flag=filename=""    ##Nullifying the flag and filename variables here.
}' Input_file         ##Mentioning Input_file name here.

In case you don't want values 0x00000000, in answer then use following:

awk '/^[a-zA-Z]+\.h:$/{filename=$0;next} /^uint32_t.*/{flag=1} flag && $0 !~ /^ +0x00000000\,$/{print > filename} /}\;/{flag=filename=""}' Input_file

OR

awk '
/^[a-zA-Z]+\.h:$/{
  filename=$0
  next
}
/^uint32_t.*/{flag=1} flag && $0 !~ /^ +0x00000000\,$/{
  print > filename
}
/}\;/{
  flag=filename=""
}'  Input_file
answered on Stack Overflow Aug 2, 2018 by RavinderSingh13 • edited Aug 2, 2018 by RavinderSingh13

User contributions licensed under CC BY-SA 3.0