[ksh88 and awk] Number of fields with a value. | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

[ksh88 and awk] Number of fields with a value.

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-31-2013
ejdv ejdv is offline
Registered User
 
Join Date: Nov 2007
Last Activity: 11 November 2014, 5:11 AM EST
Posts: 81
Thanks: 5
Thanked 1 Time in 1 Post
[ksh88 and awk] Number of fields with a value.

Hi,

With:

Code:
# VALUES="one~two~~~"
# echo $VALUES | awk 'BEGIN {FS="~"} {print NF}'
5

I can determine the number of fields.

How to determine the number of fields with a value ?
In this case 2.

Thanks in advance,

ejdv
Sponsored Links
    #2  
Old 01-31-2013
PikK45 PikK45 is offline
Registered User
 
Join Date: Jul 2012
Last Activity: 26 November 2014, 5:10 AM EST
Location: Chennai
Posts: 567
Thanks: 43
Thanked 74 Times in 72 Posts
Use FS="~+" instead of FS="~"
Sponsored Links
    #3  
Old 01-31-2013
ejdv ejdv is offline
Registered User
 
Join Date: Nov 2007
Last Activity: 11 November 2014, 5:11 AM EST
Posts: 81
Thanks: 5
Thanked 1 Time in 1 Post
Quote:
Originally Posted by PikK45 View Post
Use FS="~+" instead of FS="~"
Thanks, but I get the same result:

Code:
# echo $VALUES
one~two~~~
# echo $VALUES | awk 'BEGIN {FS="~"} {print NF}'
5
# echo $VALUES | awk 'BEGIN {FS="~+"} {print NF}'
5

Ah, this works:


Code:
# echo $VALUES | nawk 'BEGIN {FS="~+"} {print NF-1}'
2

Forget sometimes that in some cases nawk is needed iso awk.
    #4  
Old 01-31-2013
PikK45 PikK45 is offline
Registered User
 
Join Date: Jul 2012
Last Activity: 26 November 2014, 5:10 AM EST
Location: Chennai
Posts: 567
Thanks: 43
Thanked 74 Times in 72 Posts
My results are:


Code:
echo "one~two~~~" | awk -F~ '{print NF}'
5
echo "one~two~~~" | awk -F~+ ' {print NF}'
3

---------- Post updated at 06:41 PM ---------- Previous update was at 06:39 PM ----------

Quote:
Originally Posted by ejdv View Post
Ah, this works:


Code:
# echo $VALUES | nawk 'BEGIN {FS="~+"} {print NF-1}'
2

Forget sometimes that in some cases nawk is needed iso awk.
Try things before you post anything. You ll feel silly after asking
Sponsored Links
    #5  
Old 01-31-2013
Franklin52's Avatar
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 28 November 2014, 6:46 AM EST
Location: The Netherlands
Posts: 7,720
Thanks: 123
Thanked 538 Times in 508 Posts
Quote:
Originally Posted by ejdv View Post
How to determine the number of fields with a value ?
In this case 2.

Code:
awk -F"~" '{for(i=1;i<=NF;i++){if($i!="")c++}}{print c}' file

Sponsored Links
    #6  
Old 01-31-2013
PikK45 PikK45 is offline
Registered User
 
Join Date: Jul 2012
Last Activity: 26 November 2014, 5:10 AM EST
Location: Chennai
Posts: 567
Thanks: 43
Thanked 74 Times in 72 Posts
Quote:
Originally Posted by Franklin52 View Post
Code:
awk -F"~" '{for(i=1;i<=NF;i++){if($i!="")c++}}{print c}' file

I believe this one is valid to your question I was in a hurry there
Sponsored Links
    #7  
Old 01-31-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 28 November 2014, 3:19 PM EST
Location: Amsterdam
Posts: 9,626
Thanks: 294
Thanked 2,459 Times in 2,205 Posts

Code:
awk '{print gsub(/[^~]+/,x)}'

Leave $0 intact:

Code:
awk '{print gsub(/[^~]+/,"&")}'



--
Quote:
Originally Posted by ejdv View Post
Ah, this works:


Code:
# echo $VALUES | nawk 'BEGIN {FS="~+"} {print NF-1}'
2

Nope:


Code:
$ echo "one~two" | awk 'BEGIN {FS="~+"} {print NF-1}'
1
$ echo "~one~two~" | awk 'BEGIN {FS="~+"} {print NF-1}'
3

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk split lines without knowing the number of fields a-priori esolvepolito Shell Programming and Scripting 3 08-22-2012 10:47 AM
How to (n)awk lines of CSV with certain number of fields? cs03dmj Shell Programming and Scripting 1 04-07-2010 10:57 AM
Number of fields handled by awk srivat79 Shell Programming and Scripting 3 09-11-2009 04:16 AM
count number of fields not using SED or AWK strasner UNIX for Dummies Questions & Answers 4 07-23-2009 08:44 AM
awk sed cut? to rearrange random number of fields into 3 fields axo959 Shell Programming and Scripting 4 04-29-2009 05:18 AM



All times are GMT -4. The time now is 04:22 PM.