GNU awk on win-7 cmd, won't redirect output to file

荒凉一梦 提交于 2019-12-04 12:36:22

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.

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}"

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.

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

You were close. The issue here is that you are mixing awk redirection with cmd one.

For completness sake I'm using MSYS2 awk version (version should not matter in this issue):

awk --version
GNU Awk 4.2.1, API: 2.0 (GNU MPFR 4.0.1, GNU MP 6.1.2)

Windows version is in this case irrelevant - will work both on Win7 and Win10

Your command:

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

uses > which you expect to be a cmd.exe redirection, but awk expects a file, thus you get the error: awk: cmd. line:1: fatal: cannot open file ``>'

1) Fixing the redirection

You can fix that by doing the redirection directly at awk:

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

2) Using awk to read the file

The type command is here superfluous as you can use directly awk to read the file:

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

Don't forget note: What is important to note is that GNU utils are case sensitive but the default filesystem settings at windows is case-insensitive.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!