grep - 如何grep一个特定的整数

标签 grep

我的文件中有一个数字列表,格式为: {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/

相关文章:

bash - grep 正则表达式中的 shell 变量

linux - 如何在 awk 中运行 grep?

regex - Linux 命令 - 查找长度为 3 个或 8 个字母的单词

regex - 查找名称匹配模式的目录并移动它们

linux - grep后如何过滤部分行

regex - grep 中交替使用的两个正则表达式

狂欢 : Piping Find into Grep

java - 使 Perl 风格的正则表达式解释器表现得像基本或扩展的正则表达式解释器

linux - 使用 "grep"在所有子目录中搜索特定类型的文件

grep - Pandas :grep之类的功能