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

  Unix/Linux 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 Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-31-2013
ejdv ejdv is offline
Registered User
 
Join Date: Nov 2007
Last Activity: 4 December 2014, 7:52 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 Unix and Linux 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 Unix and Linux 01-31-2013
ejdv ejdv is offline
Registered User
 
Join Date: Nov 2007
Last Activity: 4 December 2014, 7:52 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 Unix and Linux 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 Unix or Linux Image Unix or Linux Image
Sponsored Links
    #5  
Old Unix and Linux 01-31-2013
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 6 March 2015, 3:59 AM EST
Location: The Netherlands
Posts: 7,727
Thanks: 128
Thanked 543 Times in 511 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 Unix and Linux 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 Unix or Linux Image I was in a hurry there Unix or Linux ImageUnix or Linux Image
Sponsored Links
    #7  
Old Unix and Linux 01-31-2013
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 6 March 2015, 4:03 PM EST
Location: Amsterdam
Posts: 9,981
Thanks: 321
Thanked 2,593 Times in 2,319 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image 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 08:07 PM.