Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to count a specific character in a file?

Status
Not open for further replies.

byrdfarmer

Vendor
Apr 12, 2004
13
0
0
US
Can someone help me with probably a dumb question. I have a big texy file, like 800,000 lines, and I want to count the number of pipes ("|") in the file.

Seems like a no brainer, but when I sat down to do it I just totally brain farted and cant figure out how.
 
Try something like this:
tr -dc '|' </path/to/inputfile | wc -c

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi there,

I have not used the -d option in tr. What does that do ?
One more way I can suggest is,

awk -F| '
BEGIN {v_count=0}
{ v_count = v_count + NF -1 }
END { print "Total number of pipes = " v_count "\n" }
' your_file_name

Cheers
 
Hi!

I just thought it was a funny problem so I pulled up this oneliner(bash or ksh):

echo $(( `cat your_file|wc -c` - `sed s/\|//g your_file|wc -c` ))

Anyone else ??

 
I would use the 'tr -dc' solution.

But here is another way:

Code:
perl -e 'print ((($file = join("", <>)) =~ s/.*?\|//g)."\n");'  your_file

Who's next ?

--------------------

Denis
 
nawk -v str="|" ' { cnt+=gsub(str, "x") }
END { printf("count is: %d\n", cnt) }' file


vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top