简体   繁体   English

在Mawk工作; 但不在Awk中

[英]Working in Mawk; but not in Awk

This could be a difficult fix, could be a dead simple fix that's glaring me in the eyes and I just can't see it. 这可能是一个困难的解决方案,可能是一个死了的简单解决方案,使我眼前一亮,而我却看不到它。

In trying to run this awk command for file piece1.txt: 在尝试为文件piece1.txt运行此awk命令时:

    awk 'BEGIN { RS = "href=\""; ORS = ""; FS = OFS = "\"" } NR > 1 {  gsub("~", "", $1); gsub("!", "", $1); gsub("%20", "_", $1); gsub("#", "", $1); gsub("$", "", $1); gsub("%", "", $1); gsub("^", "", $1); gsub("&", "_", $1); gsub("@", "", $1); gsub("*", "", $1); gsub("\(", "", $1); gsub("\)", "", $1); gsub(/ /, "_", $1); gsub("____", "_", $1); gsub("___", "_", $1); gsub("__", "_", $1); print RS } 1' piece1.txt

Output error: 输出错误:

    awk: cmd. line:1: (FILENAME=piece1.txt FNR=2) fatal: Unmatched ( or \(: /(/

It seems to run the cmd up until the first instance of "href=", as specified, and then it wipes the rest of the txt file. 它似乎一直运行cmd,直到指定的第一个“ href =“实例为止,然后擦除了txt文件的其余部分。

I'm led to believe there's just a problem in my code that I'm overlooking. 我被认为是我的代码中有一个我所忽略的问题。 But the strange thing is that this code works perfectly in a Debian/Ubuntu Distro (MAWK is default). 但是奇怪的是,该代码在Debian / Ubuntu发行版(MAWK是默认设置)中完美地工作了。 It's only in GNU Awk in a Mint KDE distro that I'm getting this error. 只有在Mint KDE发行版的GNU Awk中出现此错误。

If it's relevant: 如果相关:

    > awk --version
    > GNU Awk 4.0.1

Any help? 有什么帮助吗?

You needed to quote your regex string further as you used "" to encapsulate them instead of // : 您需要进一步引用正则表达式字符串,因为使用""来封装它们而不是//

awk 'BEGIN { RS = "href=\""; ORS = ""; FS = OFS = "\"" } NR > 1 {  gsub("~", "", $1); gsub("!", "", $1); gsub("%20", "_", $1; gsub("#", "", $1); gsub("$", "", $1); gsub("%", "", $1); gsub("^", "", $1); gsub("&", "_", $1); gsub("@", "", $1); gsub("*", "", $1); gsub("\\(", "", $1); gsub("\\)", "", $1); gsub(/ /, "_", $1); gsub("____", "_", $1); gsub("___", "_", $1); gsub("__", "_", $1); print RS } 1' piece1.txt

This was the part that was changed: gsub("\\\\(", "", $1); gsub("\\\\)", "", $1); 这是已更改的部分: gsub("\\\\(", "", $1); gsub("\\\\)", "", $1);

I suggest changing your patterns and use // instead. 我建议更改您的模式并改为使用// It's also more efficient. 效率也更高。

You might find this simpler as well: 您可能还会发现这个更简单:

awk 'BEGIN { RS = "href=\""; ORS = ""; FS = OFS = "\"" } NR > 1 { gsub(/(%20|_)+/, "_", $1); gsub(/[~!#$%^&*()@]/, "", $1); print RS } 1' piece1.txt

Or 要么

awk 'BEGIN { RS = "href=\""; ORS = ""; FS = OFS = "\"" } NR > 1 { gsub(/%20/, "_", $1); gsub(/[~!#$%^&*()@]/, "", $1); gsub(/_+/, "_", $1); print RS } 1' piece1.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM