Use grep to report back only line numbers

1. try this:

grep -n "text to find" file.ext |cut -f1 -d:



2. If you're open to using AWK:

awk '/textstring/ {print FNR}' textfile

In this case, FNR is the line number. AWK is a great tool when you're looking at grep|cut, or any time you're looking to take grep output and manipulate it.


3. All of these answers require grep to generate the entire matching lines, then pipe it to another program. If your lines are very long, it might be more efficient to use just sed to output the line numbers:

sed -n '/pattern/=' filename


4. u're going to want the second field after the colon, not the first.

grep -n "text to find" file.txt | cut -f2 -d:


你可能感兴趣的:(Use grep to report back only line numbers)