1

I'm issuing gawk command from Windows CMD but it just stuck there.Same command is working perfectly fine in Cygwin Terminal. I am trying to find first occurrence of ending brace "}" on first column in a file after line number 30

Command is

gawk 'NR > 30 &&  /^}$/ { print NR; exit }' Filename.c > Output.txt

I noticed another thing that when i issue command form CMD,Besides sticking it creates a file with the name of Line number(If above command is executed then 30 is created)

Naeem Khan
  • 31
  • 7

2 Answers2

1

Standard advice to avoid Windows quoting hell is to store the awk script (in this case NR > 30 && /^}$/ { print NR; exit }) in a file (e.g. named script.awk) and execute it as awk -f script.awk Filename.c > Output.txt.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
0

The command line

gawk 'NR > 30 && /^}$/ { print NR; exit }' Filename.c > Output.txt

is not interpreted by Windows command interpreter as expected because of two reasons:

  1. A straight single quote has no special meaning on Windows command line in comparison to Unix and Linux shells. Therefore the string within the two straight single quotes is not interpreted as single parameter string like in shells on Unix/Linux.

  2. The parameter string for gawk contains several characters with special meaning in Windows command processes:

On opening a command prompt window and running cmd /?, the brief help of Windows command interpreter is output on several pages. The last paragraph on last help page lists on which characters in a directory/file name or another string requires enclosing the name/string in straight double quotes.

So the standard solution for Windows command line would be using

gawk.exe "NR > 30 && /^}$/ { print NR; exit }" Filename.c > Output.txt

But this also does not produce the expected result. I think, but does not know it for sure, that gawk is ported from Unix/Linux to Windows without adapting the interpretation of straight single and double quotes.

For that reason another solutions is needed which is using straight single quotes and escaping each special character in gawk parameter string to be interpreted as literal character by Windows command interpreter.

gawk.exe 'NR ^> 30 ^&^& /^^}$/ { print NR; exit }' Filename.c > Output.txt

This command line makes the Unix/Linux console application gawk with a parameter string being unusual for Windows also work within a Windows command process.

Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143
  • When using GAWK for Windows, double quotes are needed around any script placed on the command line. I don't think GAWK has much to do with the quotes; I think it is more a function of how the shell (command) provides arguments to executable programs, and it recognizes double-quoted strings as a single argument, ignoring most special characters in them except for % which is used for variable substitution. – ReluctantBIOSGuy Jul 07 '16 at 14:43
  • @ReluctantBIOSGuy Windows command interpreter first parses each command line for variable expansions, redirections, conditional command concatenations, etc. Then it runs the application with the preprocessed string which command interpreter thinks is for this application. The startup code of the application executed before function `main` decomposes the command line to its arguments. So how arguments are interpreted depends mainly on application itself, see [this anser](http://stackoverflow.com/a/24008269/3074564) and [this answer](http://stackoverflow.com/a/34402887/3074564). – Mofi Jul 08 '16 at 08:34
  • Thanks. But, for the record, GAWK for Windows does recognize double-quoted command line scripts. C:\Users\JimD>gawk "BEGIN { print \"I expect double quotes!\" }" I expect double quotes! C:\Users\JimD> ` – ReluctantBIOSGuy Jul 08 '16 at 15:44
  • Apologies for the (lack of) formatting. Backticks do not seem to work. :-( – ReluctantBIOSGuy Jul 08 '16 at 15:52