简体   繁体   中英

awk syntax in Windows

I'm trying to port an unix script on windows, and i'm struggling on a awk command. 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. 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 )

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. EDIT: finally understand what's the given file and what should this awk command do! 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:

FunctionName;executed:%X of Y

then i got sed command to suppress the executed: part. (\\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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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