Hello,
I was wondering if you can help me, I am tryin gto achieve a specif output and I am stuck, any help would be greatly appreciated.

# cat file
1,2,3,4,5,6
1,,3,4,5,6
1,2,,,,6


# cat file | awk -F, '{print $1,$2,$3,$4,$5,$6}' | awk '{printf("%-1s %2s %2s %2s %2s %2s\n", $1, $2, $3, $4, $5, $6)}'
1   2  3  4  5  6
1   3  4  5  6
1   2  6


# cat file | awk -F, '{printf("%-1s %2s %2s %2s %2s %2s\n", $1, $2, $3, $4, $5, $6)}'
1   2  3  4  5  6
1      3  4  5  6
1   2           6



NEED TO SEE:

This is what I need: If no value = "0" and then # awk -F, '{printf("%-1s %2s %2s %2s %2s %2s\n", $1, $2, $3, $4, $5, $6)}'

1  2  3  4  5  6
1  0  3  4  5  6
1  2  0  0  0  6
awk -F, '
{
 printf "%-1s %2s %2s %2s %2s %2s\n",
            $1 ? $1 : 0,   $2 ? $2 : 0,
            $3 ? $3 : 0,   $4 ? $4 : 0,
            $5 ? $5 : 0,   $6 ? $6 : 0
}' "$file"

Hi cfajohnson, I tried it and although it logically suppose to work I keep getting syntax error, I checked it and I changed it a couple of times but still nothing. Please advise if you can.
Many Thanks

Hello, me again, I was just wondering if there is anyone else that can help, I am still trying to get this working.

cat file | awk -F, '{printf("%-1s %2s %2s %2s %2s %2s\n", $1+0, $2+0, $3+0, $4+0, $5+0, $6+0)}'

Shibblez' solution explicitly converts the fields to numbers; an alternative is 'printf("%2d", $1)' which implicitly converts the var to a number. More than one way to skin a cat. :)

Shibblez' solution explicitly converts the fields to numbers; an alternative is 'printf("%2d", $1)' which implicitly converts the var to a number. More than one way to skin a cat. :)

I would like to say thank you to all of you, I have one additional question, what if the variable is not a number?

example:

word,1,2,word,3,word,word,4

With this in mind I know I can do $2+0, $3+0, etc

But if I do that to a word it converts every word in the column to "0"

Many Thanks for all your help.

From my late '80s copy of The AWK Programming Language, page 77:

function isnum(n) { return n ~ /[+-]?[0-9]+$/ }

I would expect that modern awk/nawk/gawk to have more complete libraries of such useful functions available.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.