Rami Alshoubaki
Rami Alshoubaki

Reputation: 67

Show Results' line numbers using grep

how i can show the result of my grep command with incremental numbers before them ,
i'm not talking about using -n or -nr to show where is my string located in that files.

i'm talking about something like this :

 grep foo *.*

result should be like this :

1-file12.txt:   ....foo.....
2-file52.txt:   ....foo.....
3-file87.txt:   ....foo...
.
.

Thanks

Upvotes: 1

Views: 113

Answers (3)

Mehdi
Mehdi

Reputation: 4318

It might be overdoing it but you can at least try it:

n=0
for item in $(grep foo *)
do
    n=$((n+1))
    echo "$n-$item"
done

In one line:

n=0;for item in $(grep foo *); do n=$((n+1)); echo "$n-$item"; done

Upvotes: 0

anubhava
anubhava

Reputation: 785236

This awk can handle it in single command:

awk '/foo/{printf "%d-%s:\t%s\n", (++i), FILENAME, $0}' *.*

Upvotes: 0

that other guy
that other guy

Reputation: 123480

Run it through nl, the dedicated "number lines" tool:

grep foo * | nl

This obviously works for all commands, and not just grep.

Upvotes: 4

Related Questions