使用grep只回报行号 [英] Use grep to report back only line numbers

查看:175
本文介绍了使用grep只回报行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可能包含错误格式的文件(在这种情况下,模式的出现<我希望使用 grep 来仅返回发生这种情况的行号(例如,匹配在这里,请到#行并修复它)。



但是,似乎没有办法打印行号( grep -n ),而不是匹配或线本身。

我可以使用另一个正则表达式来提取行号,但我想确保grep不能自行完成。 grep -no 最接近,我想,但仍显示匹配。

解决方案

试试这个:

  grep -ntext找到file.ext | cut -f1 -d:


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).

However, there doesn't seem to be a way to print the line number (grep -n) and not the match or line itself.

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.

解决方案

try this:

grep -n "text to find" file.ext |cut -f1 -d:

这篇关于使用grep只回报行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆