[Solved] Why does my Awk command filter my text without a for loop but not when a for loop is added?


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}

Look:

$ 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 debugging the script in your question, you wrote:

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

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.

8

solved Why does my Awk command filter my text without a for loop but not when a for loop is added?