0

So I've been trying the same problem for the last few days, and I'm at a formatting road block.

I have a program that will only run if its working on an equal number of columns. I know the total column count, and the number needed to add with a filler value of 0, but am not sure how to do this. Is there some time of range option with awk or sed for this?

Input:

A B C D E
A B C D E 1 1 1 1 

Output:

A B C D E 0 0 0 0
A B C D E 1 1 1 1

The the alphabet columns are always present (with different values), but this "fill in the blank" function is eluding me. I can't use R for this due to data file size.

ashah57
  • 627
  • 1
  • 7
  • 11

1 Answers1

0

One way using awk:

$ awk 'NF!=n{for(i=NF+1;i<=n;i++)$i=0}1' n=9 file
A B C D E 0 0 0 0
A B C D E 1 1 1 1

Just set n to the number of columns you want to pad upto.

Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
  • This appears to be effective. My interpretation of this is... If the number of fields in the current line is not equal to the maximum column count, do: for every NF+1 that is less than the max columns, set the next undefined column as equal to 0. At the end the function, print (1 = printing awk idiom? some kind of true value?) – ashah57 Jul 28 '13 at 18:23
  • Sorry! Of course I will. I was just reading through and trying to understand your one-liner. – ashah57 Jul 28 '13 at 18:29
  • @ashah57 yes you are correct in your understanding, it seems you added more to your comment after I wrote my first. – Chris Seymour Jul 28 '13 at 18:38
  • Yep, I didn't realize "shift+enter" == enter in the comment box. – ashah57 Jul 28 '13 at 23:44