Find out on which line in text file is matching word
Yes, its possible with the -n
option of grep
.
From man grep
:
-n, --line-number
Prefix each line of output with the 1-based line number within its input file.
For example, if you have a file named file.txt
having:
this is
foo test
and this is
bar test
Now the output of grep -n "test" file.txt
:
$ grep -n "test" file.txt
2:foo test
4:bar test
Here 2 and 4 indicates the line numbers where the pattern is found.
The grep
approach is the simplest but these will all print the line number of the line matching pat
:
Perl
perl -lne 'print $. if /pat/' file
awk
awk '/pat/{print NR}'
sed
sed -n '/pat/=' file