从man grep
:
-o, --only-matching
Print only the matched (non‐empty) parts of a matching line, with each such part on a separate output line.
一个简单的问题:如何仅打印匹配行的匹配(非空)部分,并且每个此类部分都在同一输出行上?
从man grep
:
-o, --only-matching
Print only the matched (non‐empty) parts of a matching line, with each such part on a separate output line.
一个简单的问题:如何仅打印匹配行的匹配(非空)部分,并且每个此类部分都在同一输出行上?
我发现有时,mvn
输出中的 ANSI 转义字符会阻止我使用 grep 进行搜索:
mvn | grep -P "\[INFO\]"
我必须使用"\[.*INFO.*\]"
才能得到结果。
如何禁用处理 ANSI 转义字符?我认为有一些配置吗?
我想用 sed 过滤一些标准输出,但我不知道如何。标准输出示例是:
.
.
.
Model a. # This should be captured
.
.
.
Metrics results: # This should be captured
==================== # This should be captured
metric 1 # This should be captured
metric 2 # This should be captured
metric 3 # This should be captured
==================== # This should be captured
.
.
.
Model b # This should be captured
.
.
.
Metrics results: # This should be captured
==================== # This should be captured
metric 1 # This should be captured
metric 2 # This should be captured
metric 3 # This should be captured
==================== # This should be captured
.
.
.
其中.
表示包含任何随机字符的行。所以结果是
Model a
Metrics results:
====================
metric 1
metric 2
metric 3
====================
Model b
Metrics results:
====================
metric 1
metric 2
metric 3
====================
我可以使用什么 sed 命令?还将感谢有关如何了解 sed 的解释。
我想过滤文件中包含的所有行,mySearchString
然后将它们组合在一起并计算它们。
示例查找包含的所有行9791
AB-9791___Foo
AB-9791___Foo
DE-9791___Bar
AB-0001___Foo
使用 $ grep "9791" myFile.txt
给出了这个结果
AB-9791___Foo
AB-9791___Foo
DE-9791___Bar
// 0001 was filtered out
这个结果应该像这样分组和计数(如 SQL Group by Count
)
AB-9791___Foo 2
DE-9791___BAR 1
这个答案使用 perl,但我们的机器上没有安装 perl。
什么工具(grep、awk、sed或其他)可用于实现第二部分的分组和计数?
在我的测试文件Test_2.txt
中,这些行是写的
AB-9791___Foo
DE-9791___Bar
AB-0001___Foo
AB-9791___Foo
AB-9791___Foo
AB-9791___Foo
DE-9791___Bar
DE-9791___Bar
DE-9791___Bar
我复制并粘贴了每一AB-9791___Foo
行,所以它们应该是相同的。运行$ grep '9791' Test_grep_uniq_sort.txt | uniq -c
给出了这个结果
1 AB-9791___Foo
1 DE-9791___Bar // expected: 4 actual: 1, 2, 1
3 AB-9791___Foo // expected: 4 actual: 1, 3
2 DE-9791___Bar
1 DE-9791___Bar
运行$ sort Test_2.txt > Test_2_sort_0.txt
然后使用grep | uniq
onTest_2_sort_0.txt
几乎返回了预期的输出。
$ grep '9791' Test_2_sort_0.txt | uniq -c
4 AB-9791___Foo
1 DE-9791___Bar // this is due to a missing line break / line feed
3 DE-9791___Bar
手动添加换行符/换行符后一切正常
在 Linux 上,我得到了漂亮的颜色,使文件名和行号易于与文件内容区分开来:
我已经GREP_COLORS
在 Mac OS X 上设置了我的 GNU grep 安装(通过自制软件),但它被忽略了。我只是对匹配文本进行着色,这更加有限。
/Volumes/numerous/usr/local/homebrew/opt/grep/libexec/gnubin/grep --version
grep (GNU grep) 3.7
Packaged by Homebrew
Copyright (C) 2021 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Mike Haertel and others; see
<https://git.sv.gnu.org/cgit/grep.git/tree/AUTHORS>.
GREP_COLORS='ms=01;31:mc=01;31:sl=:cx=:fn=35:ln=32:bn=32:se=36'
使用--color=always
. 我不是在谈论突出比赛。我说的是组件着色。
当我检查我的 biostar 实现的结果以在 fasta 文件中搜索素数时,我看到了一个奇怪的结果。我最初有一个 70 列的文件,并将其转换为单行有 6077828 个字符的文件。
当我使用 grep 命令时
grep -o -P -b -n CAATCGCCGT fasta.txt
它显示了我的 Biostar 实现中未显示的两个匹配项。
3:3206721:CAATCGCCGT
3:4140348:CAATCGCCGT
我和 Kate 一起在原始文件上搜索了入门书,但没有找到。由于文本的 70 列划分,引物可能会分成两行。
然后我用 div 和 mod 将它们转换为行号和列号
然而,底漆并不存在。
grep 可以处理的最大行数有限制吗?如果是这样,当超过限制时,结果是否可靠到限制大小?