If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > count pattern occurrences in a line

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-21-09, 11:26
ymho ymho is offline
Registered User
 
Join Date: Jul 2006
Posts: 107
count pattern occurrences in a line

pls advise how to count pattern occurrences in a line.

#:#1#:#2#:#3#:#4
=> no of #:# : 4

i try
echo "#:#1#:#2#:#3#:#4" |sed 's/#:#/\n/g ' |grep -c ...

but doesnt work in solaris

thx
Reply With Quote
  #2 (permalink)  
Old 01-21-09, 11:43
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,517
This should work:
Code:
echo "#:#1#:#2#:#3#:#4" | awk '{ split($0,a,"#:#"); print length(a) - 1 }'
Reply With Quote
  #3 (permalink)  
Old 01-21-09, 11:51
ymho ymho is offline
Registered User
 
Join Date: Jul 2006
Posts: 107
good good good ... thx mike_bike_kite again ... but u seem love to use awk a lot ...
Reply With Quote
  #4 (permalink)  
Old 01-21-09, 12:25
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,517
whatever works
Reply With Quote
  #5 (permalink)  
Old 01-21-09, 19:48
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
Quote:
Originally Posted by ymho
pls advise how to count pattern occurrences in a line.

#:#1#:#2#:#3#:#4
=> no of #:# : 4
Actually there're 5 (five) fields in this record: '', '1', '2', '3', '4'

but if don't want to count the first 'empty' field, you can always substract '1':
Code:
 echo "#:#1#:#2#:#3#:#4" | awk -F'#:#' '{ print NF-1}'
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
Reply With Quote
  #6 (permalink)  
Old 01-22-09, 06:37
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,517
Yep - that's a better way of doing it
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On