Number of intergers in a file using Command Line Interface

你说的曾经没有我的故事 提交于 2019-12-01 07:45:52

问题


How to count number of integers in a file using egrep?

I tried to solve it as a pattern finding problem. Actually, I am facing problem of how to represent range of characters [0-9] continuously which include "space" before the beginning and "space or dot" after the end. I think the latter can be solved by using \< and \> respectively. Also, It should not include dot in between otherwise it will not be an integer. I am unable to convert this logic into regular expression using available tools and techniques.

My name is 2322.
33 is my sister.
I am blessed with a son named 55.
Why are you so 69. Is everything 33.
66.88 is not an integer
55whereareyou?

The right answer should be 5 i.e. for 2322, 33, 55, 69 and 33.


回答1:


                    grep -Eo '(^| )([0-9]+[\.\?\=\:]?( |$))+' | wc -w
                          ^^    ^     ^       ^        ^   ^     ^
                          ||    |     |       |        |   |     |
E = extended regex--------+|    |     |       |        |   |     |
o = extract what found-----+    |     |       |        |   |     |
starts with new line or space---+     |       |        |   |     |
digits--------------------------------+       |        |   |     |
optional dot, question mark, etc.-------------+        |   |     |
ends with end line or space----------------------------+   |     |
repeat 1 time or more (to detect integers like "123 456")--+     |
count words------------------------------------------------------+

Note: 123. 123? 123: are also counted as integer

Test:

#!/bin/bash

exec 3<<EOF
My name is 2322.
33 is my sister.
I am blessed with a son named 55.
Why are you so 69. Is everything 33.
66.88 is not an integer
55whereareyou?
two integers 123 456.
how many tables in room 400? 50.
50? oh I thought it was 40.
23: It's late, 23:00 already
EOF

grep -Eo '(^| )([0-9]+[\.\?\=\:]?( |$))+' <&3 | \
  tee >(sleep 0.5; echo -n "integer counted: "; wc -w; )

Outputs:

 2322.
33 
 55.
 69. 
 33.
 123 456.
 400? 50.
50? 
 40.
23: 
integer counted: 12



回答2:


Based on the observation that you want 66.88 excluded, I'm guessing

grep -Ec '[0-9]\.?( |$)' file

which finds a digit, optionally followed by a dot, followed by either a space or end of line.

The -c option says to report the number of lines which contain a match (so not strictly the number of matches, if there are lines which contain multiple matches) and the -E option enables extended regular expression syntax, i.e. what was traditionally calned egrep (though the command name is now obsolescent).

If you need to count matches, the -o option prints each match on a separate line, which you can then pass to wc -l (or in lucky cases combine with grep -c, but check first; this doesn't work e.g. with GNU grep currently).




回答3:


On my ubuntu this code working fine

grep -P '((^)|(\s+))[-+]?\d+\.?((\s+)|($))' test


来源:https://stackoverflow.com/questions/48358662/number-of-intergers-in-a-file-using-command-line-interface

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!