Count number of times ONE punctuation mark occurs in a file
My "add keywords" script uses both carat ("^") and comma (",") as delimiters. The text files for input should have one of each, but often there are multiple occurrences of periods (".") in them, and I want to check the files before hand from the command line to make sure the period "." occurs no more than once.
Code:
grep -o . foo | wc -l Code:
cat foo |echo $x | tr -d -c '.' | wc -m I know I must be doing something wrong, but mu question is, what am I doing wrong? This is one of those instances which proves Google is entirely useless; if it weren't I'd have found a solution there and wouldn't be asking this question. Please help. Carver |
Gives the answer 3.
Code:
echo "this. and. that." | tr -cd "\." | wc -c |
Quote:
Code:
grep -o '\.' foo | wc -l |
If you guys don't mind a related question, the output of 'wc' baffles me.
For example running 'wc' without any options, Code:
$ echo "this. and. that." | grep -o '\.' | wc Where does the newline number come from in this example? Thanks, Dave [No sooner did I post this then it occurred to me that the newlines probably come from the three instances of grep finding the three periods. Sorry for the clutter.] |
@ Dave
The output from "grep -o" is (to quote) "Print the matched parts of a matching line, with each such part on a separate output line" so in this case you get three lines with "." on them. |
My 'tr' example will extract only matching characters and then use 'wc -c' to count the characters. If you use 'grep -o' you will end up with characters + '\n' on seperate lines and you will have to count with 'wc -l'.
|
Quote:
Carver |
All times are GMT -5. The time now is 11:02 PM. |