count occurences of specific character in the file


 
Thread Tools Search this Thread
Operating Systems HP-UX count occurences of specific character in the file
# 1  
Old 06-03-2005
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?
# 2  
Old 06-03-2005
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?
# 3  
Old 06-03-2005
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"

# 4  
Old 06-03-2005
awk '{c+=gsub(s,s)}END{print c}' s='character' filename
# 5  
Old 02-16-2006
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.
# 6  
Old 02-16-2006
try setting locale to univ.utf8
# 7  
Old 02-16-2006
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count specific character of a file in each line and delete this character in a specific position

I will appreciate if you help me here in this script in Solaris Enviroment. Scenario: i have 2 files : 1) /tmp/TRANSACTIONS_DAILY_20180730.txt: 201807300000000004 201807300000000005 201807300000000006 201807300000000007 201807300000000008 2)... (10 Replies)
Discussion started by: teokon90
10 Replies

2. UNIX for Dummies Questions & Answers

To count total of specific character in a file and save its value to a variable

Hi all, I have a file that contains characters. How do I get total of spesific character from that file and save the count to a variable for doing for calculation. data.txt 1 2 2 2 2 3 3 4 5 6 7 8 5 4 3 4 (5 Replies)
Discussion started by: weslyarfan
5 Replies

3. Shell Programming and Scripting

Count occurences of a character in a file by sorting results

Hello, I try to sort results of occurences in an array by using awk but I can't find the right command. that's why I'm asking your help ! :) Please see below the command that I run: awk '{ for ( i=1; i<=length; i++ ) arr++ }END{ for ( i in arr ) { print i, arr } }' dictionnary.txt ... (3 Replies)
Discussion started by: destin45
3 Replies

4. UNIX for Advanced & Expert Users

Count specific word or character per line

Hi, I need help regarding counting specific word or character per line and validate it against a specific number i.e 10. And if number of character equals the specific number then that line will be part of the output. Specific number = 6 Specific word or char = || Sample data:... (1 Reply)
Discussion started by: janzper
1 Replies

5. Shell Programming and Scripting

Count of matched pattern occurences by time in a log file

We have a log file, the format is similar to this: 08/04/2011 05:03:08 Connection Success 08/04/2011 05:13:18 Connection Success 08/04/2011 05:23:28 Connection Fail 08/04/2011 05:33:38 Connection Success 08/04/2011 06:14:18 Connection Success 08/04/2011 06:24:28 Connection Fail 08/04/2011... (6 Replies)
Discussion started by: clu
6 Replies

6. Shell Programming and Scripting

Count number of occurences of a character in a field defined by the character in another field

Hello, I have a text file with n lines in the following format (9 column fields): Example: contig00012 149606 G C 49 68 60 18 c$cccccacccccccccc^c I need to count the number of lower-case and upper-case occurences in column 9, respectively, of the... (3 Replies)
Discussion started by: s052866
3 Replies

7. UNIX for Dummies Questions & Answers

How to count the occurences of a specific word in a file in bash shell

Hello, I want to count the occurences of a specific word in a .txt file in bash shell. Can somebody help me pleaze?? Thanks!!! (2 Replies)
Discussion started by: mskart
2 Replies

8. Shell Programming and Scripting

Count specific character(s) very large file

I'm trying to count the number of 2 specific characters in a very large file. I'd like to avoid using gsub because its taking too long. I was thinking something like: awk '-F' { t += NF - 1 } END {print t}' infile > outfile which isn't working Any ideas would be great. (3 Replies)
Discussion started by: dcfargo
3 Replies

9. Shell Programming and Scripting

grep and count no of occurences in every line of a file

Hi Folks, I have a input file of the below format. ~~~OLKIT~OLKIT~1~~TBD~BEST PAGER & WIRELESS~4899 COMMON MARKET PLACE~~~DUBLIN~KS~43016~I~Y~DIRECT~D~~0 BPGRWRLS~~~OLKIT~OLKIT~1~~TBD~BEST PAGER & WIRELESS~4899 COMMON MARKET PLACE~~~DUBLIN~KS~43016~I~Y~DIRECT~D~~0... (12 Replies)
Discussion started by: srikanthgr1
12 Replies

10. UNIX for Advanced & Expert Users

How to count no of occurences of a character in a string in UNIX

i have a string like echo "a|b|c" . i want to count the | symbols in this string . how to do this .plz tell the command (11 Replies)
Discussion started by: kamesh83
11 Replies
Login or Register to Ask a Question