使用“grep"查找特定单词出现的行号 [英] Find the line number where a specific word appears with "grep"
问题描述
grep
是否能够提供指定单词出现的行号?
Is grep
capable of providing the line number on which the specified word appears?
另外,是否可以使用 grep
来搜索从某行开始向下的单词?
Also, is possible to use grep
to search for a word starting from some certain line downward?
推荐答案
使用 grep -n
获取匹配的行号.
Use grep -n
to get the line number of a match.
我认为没有办法让 grep 从某个行号开始.为此,请使用 sed.例如,要从第 10 行开始并打印匹配行的行号和行,请使用:
I don't think there's a way to get grep to start on a certain line number. For that, use sed. For example, to start at line 10 and print the line number and line for matching lines, use:
sed -n '10,$ { /regex/ { =; p; } }' file
<小时>
要仅获取行号,您可以使用
To get only the line numbers, you could use
grep -n 'regex' | sed 's/^([0-9]+):.*$/1/'
或者你可以简单地使用 sed:
Or you could simply use sed:
sed -n '/regex/=' file
结合两个 sed 命令,你得到:
Combining the two sed commands, you get:
sed -n '10,$ { /regex/= }' file
这篇关于使用“grep"查找特定单词出现的行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!