Alex Rivera | Logout

Find the line number where a specific word appears with "grep"

Asked 2012-05-14T19:37:31.623
42

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?

Edit
Report

2 Answers

3

You can call tail +[line number] [file] and pipe it to grep -n which shows the line number:

tail +[line number] [file] | grep -n /regex/

The only problem with this method is the line numbers reported by grep -n will be [line number] - 1 less than the actual line number in [file].

answered 2012-05-14T20:00:34.643
2

Or You can use

   grep -n . file1 |tail -LineNumberToStartWith|grep regEx

This will take care of numbering the lines in the file

   grep -n . file1 

This will print the last-LineNumberToStartWith

   tail -LineNumberToStartWith

And finally it will grep your desired lines(which will include line number as in orignal file)

grep regEX
answered 2012-05-15T00:23:47.670

Your Answer