我需要计算一个10位数字出现在文件中的实例总数。所有数字都有前导零,例如:
This is some text. 0000000001
返回:
1
如果同一个号码出现多次,则会再次计算,例如:
0000000001 This is some text.
0000000010 This is some more text.
0000000001 This is some other text.
返回:
3
有时候数字之间没有空格,但每个连续的10位数字符串都要计算在内:
00000000010000000010000000000100000000010000000001
返回:
5
如何确定文件中出现的10位数字的总数?
答案 0 :(得分:19)
试试这个:
grep -o '[0-9]\{10\}' inputfilename | wc -l
答案 1 :(得分:2)
最后一个要求 - 你需要计算每行多个数字 - 不包括grep,据我所知它只能计算每行数。
编辑:显然,Nate纠正了我的问题:)我正在寻找grep的-o
选项。
但是,您可以使用sed
轻松完成此操作:
$ cat mkt.sh
sed -r -e 's/[^0-9]/./g' -e 's/[0-9]{10}/num /g' -e 's/[0-9.]//g' $1
$ for i in *.txt; do echo --- $i; cat $i; echo --- number count; ./mkt.sh $i|wc -w; done
--- 1.txt
This is some text. 0000000001
--- number count
1
--- 2.txt
0000000001 This is some text.
0000000010 This is some more text.
0000000001 This is some other text.
--- number count
3
--- 3.txt
00000000010000000010000000000100000000010000000001
--- number count
5
--- 4.txt
1 2 3 4 5 6 6 7 9 0
11 22 33 44 55 66 77 88 99 00
123456789 0
--- number count
0
--- 5.txt
1.2.3.4.123
1234567890.123-AbceCMA-5553///q/\1231231230
--- number count
2
$
答案 2 :(得分:1)
"I need to count the total number of instances in which a 10-digit number appears within a file. All of the numbers have leading zeros"
所以我认为这可能更准确:
$ grep -o '0[0-9]\{9\}' filename | wc -l
答案 3 :(得分:1)
这可能对您有用:
cat <<! >test.txt
0000000001 This is some text.
0000000010 This is some more text.
0000000001 This is some other text.
00000000010000000010000000000100000000010000000001
1 a 2 b 3 c 4 d 5 e 6 f 7 g 8 h 9 i 0 j
12345 67890 12 34 56 78 90
!
sed 'y/X/ /;s/[0-9]\{10\}/\nX\n/g' test.txt | sed '/X/!d' | sed '$=;d'
8