我有3 C头文件,用其初始值定义了几个数组。我需要将具有非零的相似变量组合为一个。
使用示例进行描述会更容易:
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,
};
我希望first.h,second.h和Third.h像这样结束:
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,
};
使用Shell脚本执行此操作的最佳方法是什么?
答案 0 :(得分:0)
可以请您尝试以下操作:
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.
如果您不需要答案中的值0x00000000,
,请使用以下命令:
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