使用awk解析源代码 [英] Use awk to parse source code

查看:220
本文介绍了使用awk解析源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从现有的源代码中创建文档.我一直在环顾四周,类似awk的东西似乎可以工作,但是到目前为止我还没有运气.信息分为两个文件file1.cfile2.c.

I'm looking to create documentation from source code that I have. I've been looking around and something like awk seems like it will work, but I've had no luck so far. The information is split in two files, file1.c and file2.c.

注意:我已经为程序设置了自动构建环境.这将检测源中的更改并进行构建.我想生成一个文本文件,其中包含自上次成功构建以来已修改的所有变量的列表.我要查找的脚本将是构建后的步骤,并且将在编译后运行

Note: I've set up an automatic build environment for the program. This detects changes in the source and builds it. I would like to generate a text file containing a list of any variables which have been modified since the last successful build. The script I'm looking for would be a post-build step, and would run after compilation

file1.c中,我有一个函数调用列表(所有相同的函数),它们具有一个字符串名称来标识它们,例如:

In file1.c I have a list of function calls (all the same function) that have a string name to identify them such as:

newFunction("THIS_IS_THE_STRING_I_WANT", otherVariables, 0, &iAlsoNeedThis);
newFunction("I_WANT_THIS_STRING_TOO", otherVariable, 0, &iAnotherOneINeed);
etc...

函数调用中的第四个参数包含file2中字符串名称的值.例如:

The fourth parameter in the function call contains the value of the string name in file2. For example:

iAlsoNeedThis = 25;
iAnotherOneINeed = 42;
etc...

我希望将列表以以下格式输出到txt文件:

I'm looking to output the list to a txt file in the following format:

THIS_IS_THE_STRING_I_WANT = 25
I_WANT_THIS_STRING_TOO = 42

有什么办法吗?

谢谢

推荐答案

这是一个开始:

NR==FNR {                     # Only true when we are reading the first file
    split($1,s,"\"")          # Get the string in quotes from the first field
    gsub(/[^a-zA-Z]/,"",$4)   # Remove the none alpha chars from the forth field
    m[$4]=s[2]                # Create array 
    next
}
$1 in m {                     # Match feild four from file1 with field one file2
    sub(/;/,"")               # Get rid of the ;
    print m[$1],$2,$3         # Print output
}

保存此script.awk并将其与您的示例一起运行将产生:

Saving this script.awk and running it with your example produces:

$ awk -f script.awk file1 file2
THIS_IS_THE_STRING_I_WANT = 25
I_WANT_THIS_STRING_TOO = 42

您需要的修改会影响脚本的第一行:

The modifications you require affects the first line of the script:

NR==FNR && $3=="0," && /start here/,/end here/ {                    

这篇关于使用awk解析源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆