|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
[ksh88 and awk] Number of fields with a value.
Hi, With: Code:
# VALUES="one~two~~~"
# echo $VALUES | awk 'BEGIN {FS="~"} {print NF}'
5I 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
|
|||
|
|||
|
Use
FS="~+" instead of
FS="~"
|
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
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}'
5Ah, this works: Code:
# echo $VALUES | nawk 'BEGIN {FS="~+"} {print NF-1}'
2Forget sometimes that in some cases nawk is needed iso awk. |
|
#4
|
|||
|
|||
|
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:
|
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
Quote:
Code:
awk -F"~" '{for(i=1;i<=NF;i++){if($i!="")c++}}{print c}' file |
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
Quote:
I was in a hurry there ![]() ![]() |
| Sponsored Links | |
|
|
#7
|
||||
|
||||
|
Code:
awk '{print gsub(/[^~]+/,x)}'Leave $0 intact: Code:
awk '{print gsub(/[^~]+/,"&")}'-- Quote:
Code:
$ echo "one~two" | awk 'BEGIN {FS="~+"} {print NF-1}'
1
$ echo "~one~two~" | awk 'BEGIN {FS="~+"} {print NF-1}'
3 |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|