문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top