We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,167 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

if variable not available then = 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
4
Contributors
7
Replies
1 Month
Discussion Span
1 Year Ago
Last Updated
8
Views
gcclinux
Newbie Poster
20 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
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"
cfajohnson
Junior Poster
196 posts since Dec 2008
Reputation Points: 25
Solved Threads: 23
Skill Endorsements: 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

gcclinux
Newbie Poster
20 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

gcclinux
Newbie Poster
20 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
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
Junior Poster in Training
73 posts since Oct 2010
Reputation Points: 15
Solved Threads: 6
Skill Endorsements: 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. :)

Fest3er
Posting Whiz in Training
243 posts since Aug 2007
Reputation Points: 51
Solved Threads: 35
Skill Endorsements: 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.

gcclinux
Newbie Poster
20 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

Fest3er
Posting Whiz in Training
243 posts since Aug 2007
Reputation Points: 51
Solved Threads: 35
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.3127 seconds using 2.69MB