我的文件中有一个数字列表,格式为: {integer}\n 。所以一个可能的列表是:
3
12
53
23
18
32
1
4
我想使用 grep 来获取特定数字的计数,但是 grep -c "1"file 结果为 3,因为它还考虑了除 1 之外的 12、18。我该如何纠正这个问题?
虽然到目前为止所有的答案都是合乎逻辑的,而且我之前也想过并测试过,但实际上没有任何效果:
username@domain2:~/code/***/project/random/r2$ cat out.txt
2
16
11
1
13
2
1
16
16
9
username@domain2:~/code/***/project/random/r2$ grep -Pc "^1$" out.txt
0
username@domain2:~/code/***/project/random/r2$ grep -Pc ^1$ out.txt
0
username@domain2:~/code/***/project/random/r2$ grep -c ^1$ out.txt
0
username@domain2:~/code/***/project/random/r2$ grep -c "^1$" out.txt
0
username@domain2:~/code/***/project/random/r2$ grep -xc "^1$" out.txt
0
username@domain2:~/code/***/project/random/r2$ grep -xc "1" out.txt
0
最佳答案
使用-x
标志:
grep -xc 1 file
这就是它的意思:
-x, --line-regexp
Select only those matches that exactly match the whole line.
关于grep - 如何grep一个特定的整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5142401/