Win-7 cmd上的GNU awk,不会将输出重定向到文件 [英] GNU awk on win-7 cmd, won't redirect output to file

查看:140
本文介绍了Win-7 cmd上的GNU awk,不会将输出重定向到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果相关,我可以直接从sourceforge中的GNU指向源下载GNU awk V 3.1.6.

If relevant I have GNU awk V 3.1.6 downloaded directly from GNU pointed source in sourceforge.

我正在使用Windows的wget获取URL页面.处理传入的文件后,我将其缩减为单行,必须从中提取键值,这是一个很长的字符串.最后一行如下所示:

I am getting a page of URLs using wget for windows. After prcoessing the incoming file, I reduce it to single line, from which I have to extract a key value, which is quite a long string. The final line looks something like this:

<ENUM_TAG>content"href:e@5nUtw3Fc^b=tZjqpszvja$sb=Lp4YGH=+J_XuupctY9zE9=&KNWbphdFnM3=x4*A@a=W4YXZKV3TMSseQx66AHz9MBwdxY@B#&57t3%s6ZyQz3!aktRNzcWeUm*8^$B6L&rs5X%H3C3UT&BhnhXgAXnKZ7f2Luy*jYjRLLwn$P29WzuVzKVnd3nVc2AKRFRPb79gQ$w$Nea6cA!A5dGRQ6q+L7QxzCM%XcVaap-ezduw?W@YSz!^7SwwkKc"</ENUM_TAG>

我需要两个"之间的长字符串.

I need the long string between the two " signs.

所以我将此结构与awk一起使用

So I use this construct with awk

type processedFile | awk -F "\"" "{print $2}"

我得到预期的输出

href:e@5nUtw3Fc^b=tZjqpszvja$sb=Lp4YGH=+J_XuupctY9zE9=&KNWbphdFnM3=x4*A@a=W4YXZKV3TMSseQx66AHz9MBwdxY@B#&57t3%s6ZyQz3!aktRNzcWeUm*8^$B6L&rs5X%H3C3UT&BhnhXgAXnKZ7f2Luy*jYjRLLwn$P29WzuVzKVnd3nVc2AKRFRPb79gQ$w$Nea6cA!A5dGRQ6q+L7QxzCM%XcVaap-ezduw?W@YSz!^7SwwkKc

但是当我运行同一命令并将输出重定向到文件时,例如

but when I run the same command with output redirected to a file, such as

type processedFile | awk -F "\"" "{print $2}" > tempDummy

我收到此错误消息:

awk: cmd. line:1: fatal: cannot open file `>' for reading (Invalid argument)

我认为\字段分隔符使我有些痛苦,并且使最后一个"字符成为未封闭的字符串值,但是我不确定如何正确地做到这一点.顺便说一下,相同的构造在我的centos盒子上运行得很好.

I am thinking the \" field separator is causing me some grief and making the last " character as a non-closed string value, but I am not sure how to make this right. The same construct runs on my centos box perfectly well by the way.

任何指针都将不胜感激.我尝试读取我可以找到的所有自述文件,但是它们都没有触及输出重定向.

Any pointers are greatly appreciated. I tried reading all the readme files I could find but none of them touches the output redirection.

推荐答案

是的,您对cmd解析器如何处理引用区域的开始/结束位置有疑问. cmd看到的是

Yes, you have problems with how cmd parser deals with where quoted areas start/end. What cmd sees is

awk -F "\"" "{print $2}" > tempDummy
       ^-^^-^          ^-------------
       1  2            3

,即三个引用区域.由于>属于带引号的区域,因此不会被视为 重定向运算符,它是管道右端的命令参数.

that is, three quoted areas. As the > falls inside a quoted area it is not handled as a redirection operator, it is an argument to the command in the rigth side of the pipe.

这可以通过仅转义(^cmd的常规转义字符)的引号来解决,以确保cmd在解析行后正确生成最终命令,并且重定向不属于awk命令

This can be solved by just escaping (^ is cmd's general escape character) a quote to ensure cmd properly generates the final command after parsing the line and that the redirection is not part of the awk command

type processedFile | awk -F ^"\"" "{print $2}" > tempDummy
                               ^^ ^..........^

或者您可以对命令重新排序以将重定向操作放置在不会干扰的位置

Or you can reorder the command to place the redirection operation where it could not interfere

type processedFile | > tempDummy awk -F "\"" "{print $2}"

,但是虽然使用此方法行之有效,但以后在其他情况下可能会失败,因为awk代码({print $2})被放置在未引用的区域中.

but while this works using this approach may later fail in other cases because the awk code ({print $2}) is placed in an unquoted area.

有一种更简单,标准,可移植的方法,而不必处理引号转义:与其将引号作为参数传递,不如使用awk字符串处理并只包含引号的转义序列更好.字符

There is a simpler, standard, portable way of doing it without having to deal with quote escaping: instead of passing the quote as argument it is better to use the awk string handling and just include the escape sequence of the quote character

type processedFile | awk -F "\x22" "{print $2}" > tempDummy

这篇关于Win-7 cmd上的GNU awk,不会将输出重定向到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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