使用 grep 仅报告行号 [英] Use grep to report back only line numbers
问题描述
我有一个可能包含错误格式的文件(在本例中,出现了模式 \backslash
).我想使用 grep
仅返回发生这种情况的行号(例如,匹配就在这里,转到第 x 行并修复它).
I have a file that possibly contains bad formatting (in this case, the occurrence of the pattern \backslash
). I would like to use grep
to return only the line numbers where this occurs (as in, the match was here, go to line # x and fix it).
但是,似乎没有办法打印行号 (grep -n
) 而不是匹配或行本身.
However, there doesn't seem to be a way to print the line number (grep -n
) and not the match or line itself.
我可以使用另一个正则表达式来提取行号,但我想确保 grep 不能自己完成.我认为 grep -no
最接近,但仍然显示匹配.
I can use another regex to extract the line numbers, but I want to make sure grep cannot do it by itself. grep -no
comes closest, I think, but still displays the match.
推荐答案
尝试:
grep -n "text to find" file.ext | cut -f1 -d:
这篇关于使用 grep 仅报告行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!