![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| HP-UX HP-UX (Hewlett Packard UniX) is Hewlett-Packard's proprietary implementation of the Unix operating system, based on System V. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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? |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
awk '{c+=gsub(s,s)}END{print c}' s='character' filename
|
|
#5
|
|||
|
|||
|
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
|
|||
|
|||
|
try setting locale to univ.utf8
|
|
#7
|
||||
|
||||
|
Seems to work...
Code:
$ printf "123\307abc" > file1
$ awk '{c+=gsub(s,s)}END{print c}' s='\307' file1
1
Code:
$ tr -dc '\307' < file1 | wc -c 1 |
||||
| Google The UNIX and Linux Forums |