Adding Fields to the file

Hi Don,

I got one problem while answering to this thread . So i came with NF>1 and NF==1 . After this i have not tested this again and just copy pasted the code.
Now from your code it looks like no need of this conditions.. My bad..

Thanks for above code.:slight_smile:
That's brilliant piece of coding..(Specially highlighted part in red)

Thanks...:slight_smile:

Thanks for the compliment, paum. But, after I looked at it again in red, I see that it is more complex than it needs to be. That red line should be replaced with:

        $i = $i != "" ? $i : (i == 2 || i == 3) ? "ND" : " "

(which drops an unneeded $i = ).

It will give the same results either way, but those 5 characters aren't needed the second time. :o

1 Like

Thanks Don, Pamu for all your efforts to make out the best effective pracrtise of coding. So i can use the final version by Don.. looks awesome and simple.:b:... really brilliant piece of coding .:slight_smile: thanks a lot!!!:slight_smile:

---------- Post updated at 04:40 PM ---------- Previous update was at 03:37 PM ----------

can anybody explain what the below line does?

$i = $i != "" ? $i : (i == 2 || i == 3) ? "ND" : " "

can i modify it to remove the condition for 2nd and 3rd field and default all additional fields to space apart from the existing one's and if an existing contains null it will be defaulted to null.

$i = $i != "" ? $i : (i == 2 || i == 3) ? "ND" : " "

Here it checks if $i not equals to null (""). If True then $i keep it as it is. If false then check i == 2 OR 3 if true then set $i="ND" and if false then set $i=" " .

If i got it your requirement right. You want all the values which is not present to null. and keep null value as it is.
try

$i = $i != "" ? $i:""

OR

awk -F '"' '
NR > 1{    for(i = 1; i <= NF; i += 2) gsub(/,/, "|", $i)
    print
}' OFS="" file | awk 'BEGIN{FS = OFS = "|"}
{    for(i = NF+1; i <= 20; i++)
        $i="";
    print
}'

Actually i wanted all existing values plus the remaining fields to make it 20. and in all if any value is null it should be defaulted to space. i think the below should do it :

 $i = $i != "" ? $i : " "

Let me know if i make sense..

Yes this will solve your purpose.. :slight_smile: