Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


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

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-31-2013
Registered User
 
Join Date: Nov 2007
Posts: 76
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
Registered User
 
Join Date: Jul 2012
Location: Chennai
Posts: 456
Thanks: 35
Thanked 57 Times in 55 Posts
Use FS="~+" instead of FS="~"
Sponsored Links
    #3  
Old 01-31-2013
Registered User
 
Join Date: Nov 2007
Posts: 76
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
Registered User
 
Join Date: Jul 2012
Location: Chennai
Posts: 456
Thanks: 35
Thanked 57 Times in 55 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
Moderator
 
Join Date: Feb 2007
Location: The Netherlands
Posts: 7,505
Thanks: 73
Thanked 474 Times in 453 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
Registered User
 
Join Date: Jul 2012
Location: Chennai
Posts: 456
Thanks: 35
Thanked 57 Times in 55 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
Moderator
 
Join Date: Nov 2008
Location: Amsterdam
Posts: 7,353
Thanks: 144
Thanked 1,756 Times in 1,593 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
Reply

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 09:47 AM
How to (n)awk lines of CSV with certain number of fields? cs03dmj Shell Programming and Scripting 1 04-07-2010 09:57 AM
Number of fields handled by awk srivat79 Shell Programming and Scripting 3 09-11-2009 03:16 AM
count number of fields not using SED or AWK strasner UNIX for Dummies Questions & Answers 4 07-23-2009 07:44 AM
awk sed cut? to rearrange random number of fields into 3 fields axo959 Shell Programming and Scripting 4 04-29-2009 04:18 AM



All times are GMT -4. The time now is 04:16 AM.