gawk命令在CMD与&&操作员不工作 [英] gawk command in CMD with && operator not working
问题描述
我从Windows CMD发出gawk命令,但它只是卡在那里。命令在Cygwin终端完全正常工作。
我试图在第30行之后的文件第一列找到第一个结尾大括号}。
命令是
gawk'NR> 30&& / ^} $ / {print NR; exit}'Filename.c> Output.txt
我注意到另一件事,当我发出命令形式CMD,除了粘贴它创建一个文件(如果执行上面的命令,则创建30)
命令行
gawk'NR> 30&& / ^} $ / {print NR; exit}'Filename.c> Output.txt
由于以下两个原因不能由Windows命令解释程序解释:
-
与Unix和Linux shell相比,直接单引号在Windows命令行中没有特殊意义。因此,两个直的单引号中的字符串不会像Unix / Linux上的shell那样解释为单参数字符串。
-
gawk
在Windows命令过程中包含特殊含义的几个字符:
-
;
...重定向运算符
请参阅Microsoft TechNet文章使用命令重定向运算符了解此运算符的详细信息。 -
&&
。 ..条件命令连接运算符
请参见有关的单行命令使用Windows批处理文件的多行命令 -
^
...字符用于转义操作符,例如>
和&
在命令执行时被解释为文字。
-
在打开命令提示符窗口并运行 cmd /?
时,Windows命令解释器的简要帮助是输出在几页。最后一个帮助页面的最后一段列出了目录/文件名或另一个字符串中的哪些字符需要用直接双引号括起来的名称/字符串。
对于Windows命令行将使用
gawk.exeNR> 30&& / ^} $ / {print NR; exit}Filename.c> Output.txt
但这也不会产生预期的结果。我认为,但不知道它肯定, gawk
是从Unix / Linux移植到Windows,而不适应直单和双引号的解释。
gawk
参数字符串中的每个特殊字符来解释为字面值 gawk.exe'NR ^> 30 ^& ^& / ^^} $ / {print NR; exit}'Filename.c> Output.txt
此命令行使Unix / Linux控制台应用程序 gawk
与Windows不同的参数字符串也可以在Windows命令过程中工作。
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)
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:
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.
The parameter string for
gawk
contains several characters with special meaning in Windows command processes:>
... the redirection operator
See the Microsoft TechNet article Using command redirection operators for details on this operator.&&
... conditional command concatenation operator
See the answer on Single line with multiple commands using Windows batch file for details on this operator.^
... character to use for escaping operators like>
and&
for being interpreted as literal character on command execution.
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.
这篇关于gawk命令在CMD与&&操作员不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!