为什么我的Awk命令在没有for循环的情况下过滤我的文本,而在添加for循环时却没有呢? [英] Why does my Awk command filter my text without a for loop but not when a for loop is added?

查看:44
本文介绍了为什么我的Awk命令在没有for循环的情况下过滤我的文本,而在添加for循环时却没有呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试过滤"5"并使用"for"进行打印3次

I'm trying to filter "5" and use "for" for print 3 times

这是没有for循环的命令:

awk -v OFS='"' -v FS='Name": "'  'match($2, /5"/){print $0}' sumacomando

这是带有for循环的命令:

awk -v OFS='"' -v FS='Name": "'  '{for(i=1;i<=3;i++) {match($2, /5"/); {print $0}}}' sumacomando

此过滤器仅打印3次

我正在尝试过滤"5"并使用for打印3次.

I'm trying to filter "5" and use for to print 3 times.

"fechaName": "1","firstName": "gdrgo",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "222",dfg
"fechaName": "2","xxxxx": "John",    "firstName": "beto",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "111","xxxxx": "John",
"fechaName": "3","xxxxx": "John",    "firstName": "beto",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "111","xxxxx": "John",
"fechaName": "4","xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "555", "xxxxx": "John","xxxxx": "John",
"fechaName": "5","xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "444", "xxxxx": "John","xxxxx": "John",
"fechaName": "6","firstName": "gdrgo",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "222",dfg
"fechaName": "7","xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "444", "xxxxx": "John","xxxxx": "John",

awk -v OFS='"' -v   FS='Name": "'      '{for(i=1;i<=3;i++) {match($2, /5"/)  ;{  print $0}}}' sumacomando

这是我的输出

"fechaName": "1","firstName": "gdrgo",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "222",dfg
"fechaName": "1","firstName": "gdrgo",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "222",dfg
"fechaName": "1","firstName": "gdrgo",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "222",dfg
"fechaName": "2","xxxxx": "John",    "firstName": "beto",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "111","xxxxx": "John",
"fechaName": "2","xxxxx": "John",    "firstName": "beto",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "111","xxxxx": "John",
"fechaName": "2","xxxxx": "John",    "firstName": "beto",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "111","xxxxx": "John",
"fechaName": "3","xxxxx": "John",    "firstName": "beto",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "111","xxxxx": "John",
"fechaName": "3","xxxxx": "John",    "firstName": "beto",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "111","xxxxx": "John",
"fechaName": "3","xxxxx": "John",    "firstName": "beto",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "111","xxxxx": "John",
"fechaName": "4","xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "555", "xxxxx": "John","xxxxx": "John",
"fechaName": "4","xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "555", "xxxxx": "John","xxxxx": "John",
"fechaName": "4","xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "555", "xxxxx": "John","xxxxx": "John",
"fechaName": "5","xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "444", "xxxxx": "John","xxxxx": "John",
"fechaName": "5","xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "444", "xxxxx": "John","xxxxx": "John",
"fechaName": "5","xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "444", "xxxxx": "John","xxxxx": "John",
"fechaName": "6","firstName": "gdrgo",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "222",dfg
"fechaName": "6","firstName": "gdrgo",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "222",dfg
"fechaName": "6","firstName": "gdrgo",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "222",dfg
"fechaName": "7","xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "444", "xxxxx": "John","xxxxx": "John",
"fechaName": "7","xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "444", "xxxxx": "John","xxxxx": "John",
"fechaName": "7","xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "444", "xxxxx": "John","xxxxx": "John",

这是我的预期输出

"fechaName": "5","xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "444", "xxxxx": "John","xxxxx": "John",
"fechaName": "5","xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "444", "xxxxx": "John","xxxxx": "John",
"fechaName": "5","xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "444", "xxxxx": "John","xxxxx": "John",

这是我的没有for循环的代码:

This is my code without for loop:

awk -v OFS='"' -v FS='Name": "'  'match($2, /5"/){print $0}' sumacomando

我该如何解决?

推荐答案

我已经告诉过您如何解决此类问题.请参见 https://stackoverflow.com/a/42753485/1745001 ,然后在该答案中添加一行,内容如下:

I already told you how to approach problems like this. See https://stackoverflow.com/a/42753485/1745001 and just add a line to that answer that says:

f["fechaName"]==5{for (i=1;i<=3;i++) print}

看:

$ cat tst.awk
BEGIN {
    FPAT = "([^,]*)|(\"[^\"]+\")"
    OFS = ","
}
{
    delete f
    for (i=1; i<=NF; i++) {
        split($i,t,/[[:space:]":]+/)
        f[t[2]] = t[3]
    }
}
f["fechaName"]==5 { for (i=1;i<=3;i++) print }

$ awk -f tst.awk file
"fechaName": "5","xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "444", "xxxxx": "John","xxxxx": "John",
"fechaName": "5","xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "444", "xxxxx": "John","xxxxx": "John",
"fechaName": "5","xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "444", "xxxxx": "John","xxxxx": "John",

wrt调试问题脚本,您写道:

wrt debugging the script in your question, you wrote:

{for(i=1;i<=3;i++) {match($2, /5"/)  ;{  print $0}}}

表示循环3次,调用match()但对该match()的结果不执行任何操作,并无条件地打印当前行".您可能打算写:

which says "loop 3 times, calling match() but doing nothing with the result of that match() and unconditionally printing the current line". You probably meant to write:

match($2, /5"/) { for(i=1;i<=3;i++) print }

但是,这种方法当然非常脆弱,因为它将对各种输入值产生错误的匹配,因此您绝对不应该使用它.

but of course that approach is extremely fragile as it will produce false matches for various input values and so you should definitely NOT use it.

这篇关于为什么我的Awk命令在没有for循环的情况下过滤我的文本,而在添加for循环时却没有呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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