Awk-sed - wc : how to count DOTS "."'s in a file.

Hi Experts ,

file:

EST 2013::.................................................................................................................................................................................................................................................cmihx021:/home/data1/

I want to count the number of DOTS (marked in red) , in the file.
Please advise,

Thanks..

Having received our help solving more than sixty other issues you have raised, what have your tried so far to get a solution to this problem?

I am still trying and thinking...about it. how to do it. awk no clue how to get to a filed which has no space.. etc. So still working on it. Thanks any way for the comment.

---------- Post updated at 11:06 PM ---------- Previous update was at 11:03 PM ----------

RESOLVED: It was easy ...

cat file |tr "." "\n" | wc -l
242

Thanks....

It is not solved by above code. if you have several lines, you calculate wrong.

try this:

grep -o "\." infile|wc -l
awk '{sum+=gsub(/\./,"")}END{print sum}' infile
1 Like

Or, building on what rveri had tried:

tr "\n." "x\n" < file | wc -l

The grep solution what I get is :

grep: illegal option -- o
$ grep -o "\." infile|wc -l
grep: illegal option -- o
usage: grep [-E|-F] [-c|-l|-q] [-bhinsvwx] -e pattern_list...
        [-f pattern_file...] [file...]
usage: grep [-E|-F] [-c|-l|-q] [-bhinsvwx] [-e pattern_list...]
        -f pattern_file... [file...]
usage: grep [-E|-F] [-c|-l|-q] [-bhinsvwx] pattern [file...]
0
$

The -o option available in some implementations of the grep utility is an extension to the requirements specified by the standards. If you would have told us what system you're using when you asked for help, most of the people here trying to help you find a way to get what you want would have avoided options that aren't available on your system.

Thanks, The system is hp-ux 11.23 , I think the grep solution is for gnu grep (for linux system).
However it is good to know.

The awk gsub worked awesome and like it.

awk '{a+=gsub(/\./,"")}END{print a}' file
241

---------- Post updated at 12:33 AM ---------- Previous update was at 12:32 AM ----------

Finally , Don your solution also worked , and with same result. Thanks a lot.

$ tr "\n." "x\n" < file | wc -l
241

---------- Post updated at 12:43 AM ---------- Previous update was at 12:33 AM ----------

rdcwayx , Thanks a lot, the awk solution mainly impressed me. I never thought gsub can be used such a way. Brilliant solution.! kudos!.

What I was trying to figure out is time taken, 1 dot , is 5 sec loop: So this is what I got. 20.08 total elapsed time.

$ awk '{a+=gsub(/\./,"")}END{printf "%.2f Min\n",(a*5)/60}' file
20.08 Min

It processes the input twice, but you could:-

wc -c file | read allchars filename
tr -d "." < file | wc -c | read nondots
((dots=$allchars-$nondots))

It might be more readable for a future review. It might be a bit slower for very large files because of the double read.

I hope that this helps
Robin
Liverpool/Blackburn
UK

tr -Cd '.' < file | wc -c
2 Likes

Just trying out different methods in case anyone is interested:

Perl:

perl -lne 'print $_ =~ tr/\.//' file

Python:

python -c 'print open("file").read().count(".")'
1 Like

All of the proposals so far fail if there are other dots in the line, e.g. file name extensions or like. You need to isolate the row of timing dots like e.g.

awk '{sub(/^[^:]*::/, ""); print match($0, /[^.]/)-1}' file
241
1 Like