1

我正在寻找从我拥有的源代码创建文档。我一直在环顾四周,像 awk 这样的东西似乎会起作用,但到目前为止我还没有运气。信息分为两个文件,file1.cfile2.c.

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

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

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

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

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

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

THIS_IS_THE_STRING_I_WANT = 25
I_WANT_THIS_STRING_TOO = 42

有没有办法做到这一点?

谢谢

4

3 回答 3

2

这是一个开始:

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并使用您的示例运行它会产生:

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

编辑:

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

NR==FNR && $3=="0," && /start here/,/end here/ {                    
于 2013-03-28T14:50:52.077 回答
0

您可以执行文件file2.c,以便在 bash 中定义变量。然后,您只需打印$iAlsoNeedThis即可从中获得价值iAlsoNeedThis = 25;

可以用. file2.c.

然后,您可以做的是:

while read line;
do
    name=$(echo $line | cut -d"\"" -f2);
    value=$(echo $line | cut -d"&" -f2 | cut -d")" -f1);
    echo $name = ${!value};
done < file1.c

获取THIS_IS_THE_STRING_I_WANT,I_WANT_THIS_STRING_TOO文本。

于 2013-03-28T14:36:17.733 回答
0

您可以像这样在外壳中执行此操作。

#!/bin/sh

eval $(sed 's/[^a-zA-Z0-9=]//g' file2)

while read -r line; do
  case $line in
    (newFunction*)
      set -- $line
      string=${1#*\"}
      string=${string%%\"*}
      while test $# -gt 1; do shift; done
      x=${1#&}
      x=${x%);}
      eval x=\$$x
      printf '%s = %s\n' $string $x
   esac
done < file1.c

假设: newFunction 位于行首。后面没有任何内容);。空白与您的示例完全相同。输出

THIS_IS_THE_STRING_I_WANT = 25
I_WANT_THIS_STRING_TOO = 42
于 2013-03-28T14:52:40.897 回答