简体   繁体   English

Windows中的awk语法

[英]awk syntax in Windows

I'm trying to port an unix script on windows, and i'm struggling on a awk command. 我试图在Windows上移植Unix脚本,而我在awk命令上苦苦挣扎。 Here it is: 这里是:

awk 'BEGIN { func=""; } /FUnction/ { func=$2 } /Lines/ { if (func != "") { printf func";"$2 }; func = ""; }'

I've read some topic here and ther and found out that Windows have problems with quotes. 我在这里读过一些主题,然后发现Windows在引号方面存在问题。 So i corrected it like that: 所以我这样纠正了它:

awk "BEGIN { func=\\""\\""; } /Function/ { func=$2 } /Lines/ { if (func != \\""\\"") { printf func\\"";\\""$2 }; func = \\""\\""; }"

But i still got this error: 但是我仍然遇到这个错误:

awk: BEGIN { func=""; } /Function/ { func=$2 } /Lines/ { if (func != ""){ print func";"$2 }; func = "";}  
             ^syntax error           ^syntax error           ^syntax error                   ^syntax error

(note that there is no error on the print func ) (请注意, print func上没有错误)

Some people said that it should be better to put the awk code in a separate script and call it, but this awk command is already in a script calling some others scripts...i would like to reduce the numbers of scripts. 有人说将awk代码放在一个单独的脚本中并调用它会更好,但是此awk命令已经在一个调用其他脚本的脚本中了……我想减少脚本的数量。 EDIT: finally understand what's the given file and what should this awk command do! 编辑:最终了解给定的文件是什么,这个awk命令应该做什么! Here it is: 这里是:

The given file is like that: 给定的文件是这样的:

Function 'FunctionName'
Lines executed: X% of Y

\\Later on, i have this: 
File 'FileName'
Lines executed: X% of Y
Creating 'FileName.gcov'

The awk command is supposed to find the word Function , take what is after it (so the function name), then found the word Lines , take what is after it, and gives it back this way: 应该使用awk命令找到单词Function ,获取其后的内容(即函数名),然后找到Lines ,获取其后的内容,并以此方式返回:

FunctionName;executed:%X of Y

then i got sed command to suppress the executed: part. 然后我得到了sed命令来禁止executed:部分。 (\\o/) (\\ O /)

so, made some thought about it: why is it suppose to work better when in a separate script, and why it seems to dislike every character in my brackets? 因此,考虑了一下:为什么要在单独的脚本中工作会更好,为什么它似乎不喜欢方括号中的每个字符? So i've tried this thing: 所以我尝试了这个东西:

awk " \\" BEGIN { func=\\""\\""; } /Function/ { func=$2 } /Lines/ { if (func != \\""\\"") { printf func\\"";\\""$2 }; func = \\""\\""; }\\" "

It is working... more or less. 它正在或多或少地起作用。 Well, i don't have syntax error anymore and it does gives me an output file, but it's not what i was looking for. 好吧,我不再有语法错误,它确实给了我一个输出文件,但这不是我想要的。 What i got looks like that: 我得到的是这样的:

Function 'FunctionName'
Function 'FunctionName'
Lines executed: X% of Y
Lines executed: X% of Y
\\same for each function; dunno why the lines are doubled
\\Later on, i have this: 
File 'FileName'
File 'FileName'
Lines executed: X% of Y
Lines executed: X% of Y
Creating 'FileName.gcov'
Creating 'FileName.gcov'

So it's just repeating each line of the input file. 因此,它只是重复输入文件的每一行。 No idea why. 不知道为什么。

So, now that i understand what it takes and what it's supposed to do, i'll be more able to find a solution, but still open to any tip! 因此,既然我了解了它需要做什么以及应该做什么,那么我将能够找到解决方案,但是仍然可以接受任何建议!

So, i found out the problem, and it was a weird one: 因此,我发现了问题所在,这很奇怪:

Apparently, you can't use the words func or function as a variable name. 显然,您不能将funcfunction用作变量名。

That's it. 而已。

So, i've just changed the variable name, and it works perfectly -_- 所以,我刚刚更改了变量名称,它可以完美地工作-_-

awk " BEGIN {f=\"\" } /Function/ { f=$2 } /Lines/ { if (f!= \"\"){ print f\";\"$3 } f= \"\"} "

(note: for my particular problem, i also had to change $2 to $3, but this is because the input file does not really have the same format. nothing to do with awk) (注意:对于我的特殊问题,我还不得不将$ 2更改为$ 3,但这是因为输入文件的格式实际上并不相同。与awk无关)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM