gawk命令在CMD与&&操作员不工作 [英] gawk command in CMD with && operator not working

查看:254
本文介绍了gawk命令在CMD与&&操作员不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从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命令解释程序解释:


  1. 与Unix和Linux shell相比,直接单引号在Windows命令行中没有特殊意义。因此,两个直的单引号中的字符串不会像Unix / Linux上的shell那样解释为单参数字符串。


  2. gawk 在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:

  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.

这篇关于gawk命令在CMD与&&操作员不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆