The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > OS Specific Forums > HP-UX
Google UNIX.COM


HP-UX HP-UX (Hewlett Packard UniX) is Hewlett-Packard's proprietary implementation of the Unix operating system, based on System V.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Count number of occurences of a word shikhakaul UNIX for Dummies Questions & Answers 7 11-30-2007 05:22 PM
grep and count no of occurences in every line of a file srikanthgr1 Shell Programming and Scripting 12 04-20-2007 06:16 AM
How to count no of occurences of a character in a string in UNIX kamesh83 UNIX for Advanced & Expert Users 11 03-17-2006 11:39 AM
count file(s) under a specific directory leemjesse Shell Programming and Scripting 6 03-23-2005 08:18 AM
Counting occurences of specific charachter in a file GMMike UNIX for Dummies Questions & Answers 3 10-13-2004 11:46 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 06-03-2005
Registered User
 

Join Date: Jun 2005
Posts: 26
count occurences of specific character in the file

For counting the occurences of specific character in the file
I am issuing the command
grep -o 'character' filename | wc -w
It works in other shells but not in HP-UX as there is no option -o for grep.
What do I do now?
Reply With Quote
Forum Sponsor
  #2  
Old 06-03-2005
Registered User
 

Join Date: Jun 2005
Posts: 26
Forgot to mention I have an alternate solution
cat filename | tr -d '\n' | tr -s '"' '\n' | wc -l
But I am looking for a grep solution
Is it possible to make some changes in grep command here?
Reply With Quote
  #3  
Old 06-03-2005
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,298
I use this, not necessarily the most efficient..
Code:
# charcnt count given char or chars in a file
# filename $1
# character or string $2
cnt=`grep "$2" $1 |
     awk -v CH="$2" '
          BEGIN {cnt =0}
          { while (y=index($0,CH))
            { cnt++
              y++
              $0=substr($0,y);
            }
          }
          END {print cnt }'`
echo "$2" "occurs $cnt times"
Reply With Quote
  #4  
Old 06-03-2005
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,249
awk '{c+=gsub(s,s)}END{print c}' s='character' filename
Reply With Quote
  #5  
Old 02-16-2006
Registered User
 

Join Date: Nov 2005
Posts: 8
If the character is \307

I am experiencing problem while executing the command :

awk '{c+=gsub(s,s)}END{print c}' s='character' filename

for character '\307'. could you please tell how to handle these type of characters.
Reply With Quote
  #6  
Old 02-16-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,298
try setting locale to univ.utf8
Reply With Quote
  #7  
Old 02-16-2006
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,249
Seems to work...
Code:
$ printf "123\307abc" > file1
$ awk '{c+=gsub(s,s)}END{print c}' s='\307' file1
1
Or try...
Code:
$ tr -dc '\307' < file1 | wc -c
1
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 04:44 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0