How to get line number from grep?

You should put -e at the end of the options list: grep -rne coll *


no need for -r & -e !

get line number of a pattern!

grep -n "pattern" file.txt

if you want to get only the line number as output add another grep command to it !

grep -n "pattern" file.txt | grep -Eo '^[^:]+'

To grep a pattern in a specific file, and get the matching lines:

grep -n <Pattern> <File> | awk -F: '{ print $1 }' | sort -u

or using cut as suggested by @wjandrea:

grep -n <Pattern> <File> | cut -f1 -d: | sort -u

where

  • <Pattern> is a quoted glob pattern (use option -E for regexp);
  • <File> is the file you are interested in;
  • the first pipe awk ... filters the line numbers in the output of grep (before : on each line);
  • the second pipe ensures that line numbers only appear once.