Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

count number of fields in the string

Status
Not open for further replies.

pho01

Programmer
Mar 17, 2003
218
US
What shell/unix command can I use to count the number of fields in a string variable based on a string delimiter?

For example:
count_str="DB_ANSWER_DATA 52428800 50528256 96.375 10420224 5"

The output is varied, in this case, count_num variable would return 6. and the delimiter is ' '

Thanks,
 
using any combination of awk, cut, sed and wc should be able to give you this information.
 
Try..
[tt]
echo $count_str | awk -F ' ' '{print NF}' | read count_num
[/tt]
 
Try:

echo STRING | awk '{print NF}'

NF is the number of fields, if you want to change the field separator, try

echo STRING | awk '{FS=":"; print NF}'


--
Jack
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top