Count strings on single line?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count strings on single line?
# 1  
Old 05-21-2008
Count strings on single line?

I use grep -c often, but cannot for the life of me count the number of occurences of a string on the same line (or within a file):

Code:
$ cat myfile
hello457903485897hello
34329048hellojsdfkljlaskdjgh182390
$ grep -c
2
$

How do I count the number of occurences of "hello" in myfile (i.e. 3)?

Thanks in advance...
# 2  
Old 05-21-2008
one way
Code:
#!/bin/ksh
cnt_words()
{
    
	file=$1
	srch="$2"
	v=$(( (`cat $file | wc -c` - `sed "s/$srch//g" $file | wc -c`) / ${#srch} ))
	echo $v
}   

cnt_words myfile hello

FWIW -
And it is not UUOC I'm suppressing the filename in wc -c output.
# 3  
Old 05-21-2008
Thanks Jim - that certainly works, and is a rather innovative way of getting the result; I'm just amazed that there's not an easier way of doing it!
# 4  
Old 05-21-2008
With awk:

Code:
awk 'END{print _}{_+=NF-1}' FS="hello" input

Or perl:

Code:
perl -nle'++$c while /hello/g;print $c if eof' input

If you have GNU grep:

Code:
grep -o hello input|wc -l


Last edited by radoulov; 05-21-2008 at 01:02 PM..
# 5  
Old 05-22-2008
Hi,

Hope below code can help you. Why i use sed is to replace hello with '|'. In other words i do not know how to put a word such as 'hello' as the FS for AWK, if can do that, the code can be much easier.

BTW, pls anyone know how to consider a word as well as several words as the FS for AWK, please kindly share it here.

Thanks in advanced.

Code:
cat file | sed 's/hello/|/g' |
awk 'BEGIN{FS="|"}
{
n=n+NF-1
}
END{
print n
}'

# 6  
Old 05-22-2008
Code:
% print onehellotwoehlothree|awk -F"hello|ehlo" '$1=$1'
one two three

BTW, the FS could be even a regular expression:

Code:
% print onehellotwo|awk -F"h[^o]*o" '$1=$1'
one two

# 7  
Old 05-22-2008
Brilliant! So with that you can just sum the NFs for each line.

Code:
vnix$ awk -F "hello" '{ sum += NF - 1 } END { print sum }' <<HERE
> hello457903485897hello
> 34329048hellojsdfkljlaskdjgh182390
> HERE
3

Oh, and Jim: that's still a UUOC; by the definition of UUOC, if you can use redirection instead, the cat is Useless.

Code:
wc -c <$file


Last edited by era; 05-22-2008 at 05:11 AM.. Reason: UUOC remark
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count the pipes "|" in line and delete line if count greter then number.

Hello, I have been working on Awk/sed one liner which counts the number of occurrences of '|' in pipe separated lines of file and delete the line from files if count exceeds "17". i.e need to get records having exact 17 pipe separated fields(no more or less) currently i have below : awk... (1 Reply)
Discussion started by: ketanraut
1 Replies

2. Shell Programming and Scripting

Compare file1 header count with file2 line count

What I'm trying to accomplish. I receive a Header and Detail file for daily processing. The detail file comes first which holds data, the header is a receipt of the detail file and has the detail files record count. Before processing the detail file I would like to put a wrapper around another... (4 Replies)
Discussion started by: pone2332
4 Replies

3. Shell Programming and Scripting

Count the occurences of strings

I have some text files in a folder f1 with 10 columns. The first five columns of a file are shown below. aab abb 263-455 263 455 aab abb 263-455 263 455 aab abb 263-455 263 455 bbb abb 26-455 26 455 bbb abb 26-455 26 455 bbb aka 264-266 264 266 bga bga 230-232 230 ... (10 Replies)
Discussion started by: gomez
10 Replies

4. Shell Programming and Scripting

Count the number of strings

I have 500 text files in a folder. The data of the text files are shown below. USA Germany 23-12 USA Germany 23-12 USA Germany 23-12 France Germany 15-12 France Germany 15-12 France Italy 25-50 China China 30-32 China China 30-32 I would... (1 Reply)
Discussion started by: sahith
1 Replies

5. UNIX for Dummies Questions & Answers

How to search and count strings?

Hi, Is there a command to do a sensitive/in-sensitive search for a string on a line and print how many times that string appears? For example, if I have a line of text below: dog cat rat apple banana dog lion tiger dog Is there a command to search for dog that will print out 3 as a... (7 Replies)
Discussion started by: newbie_01
7 Replies

6. Shell Programming and Scripting

How do I count strings on each line?

Hi Im a very inexperienced bioinformatician I have a large DNA file with about 10000 lines of sequence and need to count the occurrence of TA for each line for example in the file TACGCGCGATA TATATATA GGCGCGTATA I would like to get an output like: 2 4 2 I have tried... (3 Replies)
Discussion started by: Manchesterpaul
3 Replies

7. Shell Programming and Scripting

Multiple lines in a single column to be merged as a single line for a record

Hi, I have a requirement with, No~Dt~Notes 1~2011/08/1~"aaa bbb ccc ddd eee fff ggg hhh" Single column alone got splitted into multiple lines. I require the output as No~Dt~Notes 1~2011/08/1~"aaa<>bbb<>ccc<>ddd<>eee<>fff<>ggg<>hhh" mean to say those new lines to be... (1 Reply)
Discussion started by: Bhuvaneswari
1 Replies

8. Shell Programming and Scripting

count identical strings print last row and count

I have a sorted file like: Apple 3 Apple 5 Apple 8 Banana 2 Banana 3 Grape 31 Orange 7 Orange 13 I'd like to search $1 and if $1 is not the same as $1 in the previous row print that row and print the number of times $1 was found. so the output would look like: Apple 8 3 Banana... (2 Replies)
Discussion started by: dcfargo
2 Replies

9. Shell Programming and Scripting

How to count unique strings

How do I count the total number of unique strings from a file using Perl? Any help is appreciated.. (6 Replies)
Discussion started by: my_Perl
6 Replies

10. Shell Programming and Scripting

Looking for a single line to count how many times one character occurs in a word...

I've been looking on the internet, and haven't found anything simple enough to use in my code. All I want to do is count how many times "-" occurs in a string of characters (as a package name). It seems it should be very simple, and shouldn't require more than one line to accomplish. And this is... (2 Replies)
Discussion started by: Shingoshi
2 Replies
Login or Register to Ask a Question