提取正则表达式匹配 grep 的第一个位置
大家早上好,
我有一个包含多行的文本文件。我想找到其中的常规模式并使用 grep 打印其位置。
例如:
ARTGHFRHOPLIT
GFRTLOPLATHLG
TGHLKTGVARTHG
我想在文件中找到L[any_letter]T,并打印L的位置和三字母代码。在这种情况下,结果将是:
11 LIT
8 LAT
4 LKT
我在 grep 中编写了代码,但它没有返回我需要的内容。代码是:
grep -E -boe "L.T" file.txt
它返回:
11:LIT
21:LAT
30:LKT
任何帮助将不胜感激!
Good morning everyone,
I have a text file containing multiple lines. I want to find a regular pattern inside it and print its position using grep.
For example:
ARTGHFRHOPLIT
GFRTLOPLATHLG
TGHLKTGVARTHG
I want to find L[any_letter]T in the file and print the position of L and the three letter code. In this case it would results as:
11 LIT
8 LAT
4 LKT
I wrote a code in grep, but it doesn't return what I need. The code is:
grep -E -boe "L.T" file.txt
It returns:
11:LIT
21:LAT
30:LKT
Any help would be appreciated!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Awk 更适合这一点:
假设每行只有一个这样的匹配。
如果每行可以有多个重叠匹配项,则使用:
Awk suites this better:
This is assuming only one such match per line.
If there can be multiple overlapping matches per line then use:
对于显示的示例,请尝试执行以下
awk
代码。在 GNUawk
中编写和测试,应该可以在任何awk
中工作。说明:为上述代码添加详细说明。
With your shown samples, please try following
awk
code. Written and tested in GNUawk
, should work in anyawk
.Explanation: Adding detailed explanation for above code.
查看 @anubhava 的答案,您还可以对 RSTART + RLENGTH 求和并将其用作 substr 获取每行和每个单词有多个匹配项。
while 循环获取当前行,并且对于每次迭代,它都会通过将其设置为最后一个匹配之后直到字符串末尾的部分来更新其值。
请注意,如果您在正则表达式中使用
.
,它可以匹配任何字符。如果文件包含
输出是
Peeking at the answer of @anubhava you might also sum the RSTART + RLENGTH and use that as the start for the substr to get multiple matches per line and per word.
The while loop takes the current line, and for every iteration it updates its value by setting it to the part right after the last match till the end of the string.
Note that if you use the
.
in a regex it can match any character.If file contains
The output is