找到特定单词出现的行号,其中“grep” [英] Find the line number where a specific word appears with "grep"

查看:122
本文介绍了找到特定单词出现的行号,其中“grep”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

grep 能够提供指定单词出现的行号吗?

另外,使用 grep 搜索从某一行开始的单词向下?

使用 grep -n 来获取匹配的行号。



我不认为有一种让grep在特定行号上启动的方法。为此,请使用sed。例如,要从第10行开始,打印行号和行以匹配行,请使用:

  sed -n '10 ,$ {/ regex / {=;磷; }}'file 






,你可以使用

  grep -n'regex'| sed's / ^ \([0-9] \ + \):。* $ / \ 1 /'

或者您可以简单地使用sed:

  sed -n'/ regex / ='file 

结合两个sed命令,您会得到:

  sed -n '10,$ {/ regex / =}'文件


Is grep capable of providing the line number on which the specified word appears?

Also, is possible to use grep to search for a word starting from some certain line downward?

解决方案

Use grep -n to get the line number of a match.

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/'

Or you could simply use sed:

sed -n '/regex/=' file

Combining the two sed commands, you get:

sed -n '10,$ { /regex/= }' file

这篇关于找到特定单词出现的行号,其中“grep”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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