1,105,578 Community Members

if variable not available then = 0

Member Avatar
gcclinux
Newbie Poster
20 posts since Mar 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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
Member Avatar
cfajohnson
Junior Poster
199 posts since Dec 2008
Reputation Points: 10 [?]
Q&As Helped to Solve: 24 [?]
Skill Endorsements: 0 [?]
 
1
 
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"
Member Avatar
gcclinux
Newbie Poster
20 posts since Mar 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
gcclinux
Newbie Poster
20 posts since Mar 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
shibblez
Junior Poster in Training
77 posts since Oct 2010
Reputation Points: 2 [?]
Q&As Helped to Solve: 7 [?]
Skill Endorsements: 0 [?]
 
2
 
cat file | awk -F, '{printf("%-1s %2s %2s %2s %2s %2s\n", $1+0, $2+0, $3+0, $4+0, $5+0, $6+0)}'
Member Avatar
Fest3er
Posting Whiz in Training
242 posts since Aug 2007
Reputation Points: 39 [?]
Q&As Helped to Solve: 35 [?]
Skill Endorsements: 0 [?]
 
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. :)

Member Avatar
gcclinux
Newbie Poster
20 posts since Mar 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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. :)

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.

Member Avatar
Fest3er
Posting Whiz in Training
242 posts since Aug 2007
Reputation Points: 39 [?]
Q&As Helped to Solve: 35 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article